blob: c12961aa78cc46d761b3634151edc76a73ba6078 [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 Moolenaard6051b52016-02-28 15:49:03 +01007744 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 */
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 Moolenaard6051b52016-02-28 15:49:03 +01007765 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007766 if (job->jv_channel != NULL)
7767 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007768 /* The link from the channel to the job doesn't count as a reference,
7769 * thus don't decrement the refcount of the job. The reference from
7770 * the job to the channel does count the refrence, decrement it and
7771 * NULL the reference. We don't set ch_job_killed, unreferencing the
7772 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007773 job->jv_channel->ch_job = NULL;
7774 channel_unref(job->jv_channel);
7775 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007776# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007777 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007778
7779 if (job->jv_next != NULL)
7780 job->jv_next->jv_prev = job->jv_prev;
7781 if (job->jv_prev == NULL)
7782 first_job = job->jv_next;
7783 else
7784 job->jv_prev->jv_next = job->jv_next;
7785
7786 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007787 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007788 vim_free(job);
7789}
7790
7791 static void
7792job_unref(job_T *job)
7793{
7794 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007795 {
7796 /* Do not free the job when it has not ended yet and there is a
7797 * "stoponexit" flag or an exit callback. */
7798 if (job->jv_status != JOB_STARTED
7799 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007800 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007801 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007802 }
7803 else if (job->jv_channel != NULL)
7804 {
7805 /* Do remove the link to the channel, otherwise it hangs
7806 * around until Vim exits. See job_free() for refcount. */
7807 job->jv_channel->ch_job = NULL;
7808 channel_unref(job->jv_channel);
7809 job->jv_channel = NULL;
7810 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007811 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812}
7813
7814/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007815 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816 */
7817 static job_T *
7818job_alloc(void)
7819{
7820 job_T *job;
7821
7822 job = (job_T *)alloc_clear(sizeof(job_T));
7823 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 job->jv_stoponexit = vim_strsave((char_u *)"term");
7827
7828 if (first_job != NULL)
7829 {
7830 first_job->jv_prev = job;
7831 job->jv_next = first_job;
7832 }
7833 first_job = job;
7834 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007835 return job;
7836}
7837
Bram Moolenaar65edff82016-02-21 16:40:11 +01007838 static void
7839job_set_options(job_T *job, jobopt_T *opt)
7840{
7841 if (opt->jo_set & JO_STOPONEXIT)
7842 {
7843 vim_free(job->jv_stoponexit);
7844 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7845 job->jv_stoponexit = NULL;
7846 else
7847 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7848 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007849 if (opt->jo_set & JO_EXIT_CB)
7850 {
7851 vim_free(job->jv_exit_cb);
7852 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7853 job->jv_exit_cb = NULL;
7854 else
7855 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7856 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007857}
7858
7859/*
7860 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7861 */
7862 void
7863job_stop_on_exit()
7864{
7865 job_T *job;
7866
7867 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007868 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007869 mch_stop_job(job, job->jv_stoponexit);
7870}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007871#endif
7872
Bram Moolenaar17a13432016-01-24 14:22:10 +01007873 static char *
7874get_var_special_name(int nr)
7875{
7876 switch (nr)
7877 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007878 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007879 case VVAL_TRUE: return "v:true";
7880 case VVAL_NONE: return "v:none";
7881 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007882 }
7883 EMSG2(_(e_intern2), "get_var_special_name()");
7884 return "42";
7885}
7886
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 * Return a string with the string representation of a variable.
7889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007893 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 */
7895 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896echo_string(
7897 typval_T *tv,
7898 char_u **tofree,
7899 char_u *numbuf,
7900 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 static int recurse = 0;
7903 char_u *r = NULL;
7904
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007907 if (!did_echo_string_emsg)
7908 {
7909 /* Only give this message once for a recursive call to avoid
7910 * flooding the user with errors. And stop iterating over lists
7911 * and dicts. */
7912 did_echo_string_emsg = TRUE;
7913 EMSG(_("E724: variable nested too deep for displaying"));
7914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007916 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 }
7918 ++recurse;
7919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 switch (tv->v_type)
7921 {
7922 case VAR_FUNC:
7923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 r = tv->vval.v_string;
7925 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928 if (tv->vval.v_list == NULL)
7929 {
7930 *tofree = NULL;
7931 r = NULL;
7932 }
7933 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7934 {
7935 *tofree = NULL;
7936 r = (char_u *)"[...]";
7937 }
7938 else
7939 {
7940 tv->vval.v_list->lv_copyID = copyID;
7941 *tofree = list2string(tv, copyID);
7942 r = *tofree;
7943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007944 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007945
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947 if (tv->vval.v_dict == NULL)
7948 {
7949 *tofree = NULL;
7950 r = NULL;
7951 }
7952 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7953 {
7954 *tofree = NULL;
7955 r = (char_u *)"{...}";
7956 }
7957 else
7958 {
7959 tv->vval.v_dict->dv_copyID = copyID;
7960 *tofree = dict2string(tv, copyID);
7961 r = *tofree;
7962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 case VAR_STRING:
7966 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007967 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007969 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 *tofree = NULL;
7971 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976 *tofree = NULL;
7977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7978 r = numbuf;
7979 break;
7980#endif
7981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 case VAR_SPECIAL:
7983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987
Bram Moolenaar8502c702014-06-17 12:51:16 +02007988 if (--recurse == 0)
7989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991}
7992
7993/*
7994 * Return a string with the string representation of a variable.
7995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7996 * "numbuf" is used for a number.
7997 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007998 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 */
8000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001tv2string(
8002 typval_T *tv,
8003 char_u **tofree,
8004 char_u *numbuf,
8005 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006{
8007 switch (tv->v_type)
8008 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 case VAR_FUNC:
8010 *tofree = string_quote(tv->vval.v_string, TRUE);
8011 return *tofree;
8012 case VAR_STRING:
8013 *tofree = string_quote(tv->vval.v_string, FALSE);
8014 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015#ifdef FEAT_FLOAT
8016 case VAR_FLOAT:
8017 *tofree = NULL;
8018 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8019 return numbuf;
8020#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008022 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031}
8032
8033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 * Return string "str" in ' quotes, doubling ' characters.
8035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 */
8038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040{
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 char_u *p, *r, *s;
8043
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 len = (function ? 13 : 3);
8045 if (str != NULL)
8046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 for (p = str; *p != NUL; mb_ptr_adv(p))
8049 if (*p == '\'')
8050 ++len;
8051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 s = r = alloc(len);
8053 if (r != NULL)
8054 {
8055 if (function)
8056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 r += 10;
8059 }
8060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 if (str != NULL)
8063 for (p = str; *p != NUL; )
8064 {
8065 if (*p == '\'')
8066 *r++ = '\'';
8067 MB_COPY_CHAR(p, r);
8068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070 if (function)
8071 *r++ = ')';
8072 *r++ = NUL;
8073 }
8074 return s;
8075}
8076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078/*
8079 * Convert the string "text" to a floating point number.
8080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8081 * this always uses a decimal point.
8082 * Returns the length of the text that was consumed.
8083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085string2float(
8086 char_u *text,
8087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088{
8089 char *s = (char *)text;
8090 float_T f;
8091
8092 f = strtod(s, &s);
8093 *value = f;
8094 return (int)((char_u *)s - text);
8095}
8096#endif
8097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Get the value of an environment variable.
8100 * "arg" is pointing to the '$'. It is advanced to after the name.
8101 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 */
8104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008105get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 char_u *string = NULL;
8108 int len;
8109 int cc;
8110 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008111 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 ++*arg;
8114 name = *arg;
8115 len = get_env_len(arg);
8116 if (evaluate)
8117 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008119 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008120
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008121 cc = name[len];
8122 name[len] = NUL;
8123 /* first try vim_getenv(), fast for normal environment vars */
8124 string = vim_getenv(name, &mustfree);
8125 if (string != NULL && *string != NUL)
8126 {
8127 if (!mustfree)
8128 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008130 else
8131 {
8132 if (mustfree)
8133 vim_free(string);
8134
8135 /* next try expanding things like $VIM and ${HOME} */
8136 string = expand_env_save(name - 1);
8137 if (string != NULL && *string == '$')
8138 {
8139 vim_free(string);
8140 string = NULL;
8141 }
8142 }
8143 name[len] = cc;
8144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->v_type = VAR_STRING;
8146 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148
8149 return OK;
8150}
8151
8152/*
8153 * Array with names and number of arguments of all internal functions
8154 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8155 */
8156static struct fst
8157{
8158 char *f_name; /* function name */
8159 char f_min_argc; /* minimal number of arguments */
8160 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008161 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008162 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163} functions[] =
8164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008170 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"append", 2, 2, f_append},
8173 {"argc", 0, 0, f_argc},
8174 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008175 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008176 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008177#ifdef FEAT_FLOAT
8178 {"asin", 1, 1, f_asin}, /* WJMc */
8179#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008181 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008182 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_false", 1, 2, f_assert_false},
8184 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008187 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008190 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"bufexists", 1, 1, f_bufexists},
8192 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8193 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8194 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8195 {"buflisted", 1, 1, f_buflisted},
8196 {"bufloaded", 1, 1, f_bufloaded},
8197 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008198 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"bufwinnr", 1, 1, f_bufwinnr},
8200 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008201 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008202 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204#ifdef FEAT_FLOAT
8205 {"ceil", 1, 1, f_ceil},
8206#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207#ifdef FEAT_CHANNEL
8208 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008209 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8210 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008211 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008212# ifdef FEAT_JOB
8213 {"ch_getjob", 1, 1, f_ch_getjob},
8214# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008215 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008216 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008217 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008218 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008219 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8221 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008222 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008226 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008228 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232 {"complete_add", 1, 1, f_complete_add},
8233 {"complete_check", 0, 0, f_complete_check},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008243 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008245 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 {"diff_filler", 1, 1, f_diff_filler},
8248 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008249 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008250 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"eventhandler", 0, 0, f_eventhandler},
8254 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008255 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257#ifdef FEAT_FLOAT
8258 {"exp", 1, 1, f_exp},
8259#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008260 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008261 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008262 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8264 {"filereadable", 1, 1, f_filereadable},
8265 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008267 {"finddir", 1, 3, f_finddir},
8268 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"float2nr", 1, 1, f_float2nr},
8271 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008274 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"fnamemodify", 2, 2, f_fnamemodify},
8276 {"foldclosed", 1, 1, f_foldclosed},
8277 {"foldclosedend", 1, 1, f_foldclosedend},
8278 {"foldlevel", 1, 1, f_foldlevel},
8279 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008280 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008283 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008285 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008286 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getchar", 0, 1, f_getchar},
8288 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008289 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getcmdline", 0, 0, f_getcmdline},
8291 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008292 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008293 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008294 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008295 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008297 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"getfsize", 1, 1, f_getfsize},
8299 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008300 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008302 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008303 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008304 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008305 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008307 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"gettabvar", 2, 3, f_gettabvar},
8310 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getwinposx", 0, 0, f_getwinposx},
8312 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008315 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008316 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008318 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008320 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8322 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8323 {"histadd", 2, 2, f_histadd},
8324 {"histdel", 1, 2, f_histdel},
8325 {"histget", 1, 2, f_histget},
8326 {"histnr", 1, 1, f_histnr},
8327 {"hlID", 1, 1, f_hlID},
8328 {"hlexists", 1, 1, f_hlexists},
8329 {"hostname", 0, 0, f_hostname},
8330 {"iconv", 3, 3, f_iconv},
8331 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008333 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008335 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"inputrestore", 0, 0, f_inputrestore},
8337 {"inputsave", 0, 0, f_inputsave},
8338 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008340 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008343#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8344 {"isnan", 1, 1, f_isnan},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008347#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008348# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008349 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008350# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008351 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008352 {"job_start", 1, 2, f_job_start},
8353 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008354 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008355#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008357 {"js_decode", 1, 1, f_js_decode},
8358 {"js_encode", 1, 1, f_js_encode},
8359 {"json_decode", 1, 1, f_json_decode},
8360 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"libcall", 3, 3, f_libcall},
8365 {"libcallnr", 3, 3, f_libcallnr},
8366 {"line", 1, 1, f_line},
8367 {"line2byte", 1, 1, f_line2byte},
8368 {"lispindent", 1, 1, f_lispindent},
8369 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372 {"log10", 1, 1, f_log10},
8373#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008374#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008375 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008376#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008377 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008378 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008379 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008380 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008381 {"matchadd", 2, 5, f_matchadd},
8382 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008384 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008385 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008387 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008388 {"max", 1, 1, f_max},
8389 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008390#ifdef vim_mkdir
8391 {"mkdir", 1, 3, f_mkdir},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008394#ifdef FEAT_MZSCHEME
8395 {"mzeval", 1, 1, f_mzeval},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008398 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008401#ifdef FEAT_PERL
8402 {"perleval", 1, 1, f_perleval},
8403#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"pow", 2, 2, f_pow},
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008410#ifdef FEAT_PYTHON3
8411 {"py3eval", 1, 1, f_py3eval},
8412#endif
8413#ifdef FEAT_PYTHON
8414 {"pyeval", 1, 1, f_pyeval},
8415#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008417 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008418 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008419 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008420 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"remote_expr", 2, 3, f_remote_expr},
8422 {"remote_foreground", 1, 1, f_remote_foreground},
8423 {"remote_peek", 1, 2, f_remote_peek},
8424 {"remote_read", 1, 1, f_remote_read},
8425 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008426 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008428 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008430 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008431#ifdef FEAT_FLOAT
8432 {"round", 1, 1, f_round},
8433#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008434 {"screenattr", 2, 2, f_screenattr},
8435 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008436 {"screencol", 0, 0, f_screencol},
8437 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008438 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008439 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"searchpair", 3, 7, f_searchpair},
8441 {"searchpairpos", 3, 7, f_searchpairpos},
8442 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {"server2client", 2, 2, f_server2client},
8444 {"serverlist", 0, 0, f_serverlist},
8445 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008446 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"setcmdpos", 1, 1, f_setcmdpos},
8448 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008449 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008450 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008451 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008452 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008454 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008455 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008457#ifdef FEAT_CRYPT
8458 {"sha256", 1, 1, f_sha256},
8459#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008460 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008461 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008463#ifdef FEAT_FLOAT
8464 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008465 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008467 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008468 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008469 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008470 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008471 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008472#ifdef FEAT_FLOAT
8473 {"sqrt", 1, 1, f_sqrt},
8474 {"str2float", 1, 1, f_str2float},
8475#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008476 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008477 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008478 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479#ifdef HAVE_STRFTIME
8480 {"strftime", 1, 2, f_strftime},
8481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"strlen", 1, 1, f_strlen},
8485 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008486 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008488 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008489 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"substitute", 4, 4, f_substitute},
8491 {"synID", 3, 3, f_synID},
8492 {"synIDattr", 2, 3, f_synIDattr},
8493 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008494 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008495 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008496 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008497 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008498 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008499 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008500 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008501 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008502 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008503#ifdef FEAT_FLOAT
8504 {"tan", 1, 1, f_tan},
8505 {"tanh", 1, 1, f_tanh},
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008508 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"tolower", 1, 1, f_tolower},
8510 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008511 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512#ifdef FEAT_FLOAT
8513 {"trunc", 1, 1, f_trunc},
8514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008516 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008517 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008518 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008519 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"virtcol", 1, 1, f_virtcol},
8521 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008522 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"winbufnr", 1, 1, f_winbufnr},
8524 {"wincol", 0, 0, f_wincol},
8525 {"winheight", 1, 1, f_winheight},
8526 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008527 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008529 {"winrestview", 1, 1, f_winrestview},
8530 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008532 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008533 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008534 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535};
8536
8537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8538
8539/*
8540 * Function given to ExpandGeneric() to obtain the list of internal
8541 * or user defined function names.
8542 */
8543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008544get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
8546 static int intidx = -1;
8547 char_u *name;
8548
8549 if (idx == 0)
8550 intidx = -1;
8551 if (intidx < 0)
8552 {
8553 name = get_user_func_name(xp, idx);
8554 if (name != NULL)
8555 return name;
8556 }
8557 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8558 {
8559 STRCPY(IObuff, functions[intidx].f_name);
8560 STRCAT(IObuff, "(");
8561 if (functions[intidx].f_max_argc == 0)
8562 STRCAT(IObuff, ")");
8563 return IObuff;
8564 }
8565
8566 return NULL;
8567}
8568
8569/*
8570 * Function given to ExpandGeneric() to obtain the list of internal or
8571 * user defined variable or function names.
8572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008574get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 static int intidx = -1;
8577 char_u *name;
8578
8579 if (idx == 0)
8580 intidx = -1;
8581 if (intidx < 0)
8582 {
8583 name = get_function_name(xp, idx);
8584 if (name != NULL)
8585 return name;
8586 }
8587 return get_user_var_name(xp, ++intidx);
8588}
8589
8590#endif /* FEAT_CMDL_COMPL */
8591
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008592#if defined(EBCDIC) || defined(PROTO)
8593/*
8594 * Compare struct fst by function name.
8595 */
8596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008597compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008598{
8599 struct fst *p1 = (struct fst *)s1;
8600 struct fst *p2 = (struct fst *)s2;
8601
8602 return STRCMP(p1->f_name, p2->f_name);
8603}
8604
8605/*
8606 * Sort the function table by function name.
8607 * The sorting of the table above is ASCII dependant.
8608 * On machines using EBCDIC we have to sort it.
8609 */
8610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008612{
8613 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8614
8615 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8616}
8617#endif
8618
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*
8621 * Find internal function in table above.
8622 * Return index, or -1 if not found
8623 */
8624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008625find_internal_func(
8626 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 int first = 0;
8629 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8630 int cmp;
8631 int x;
8632
8633 /*
8634 * Find the function name in the table. Binary search.
8635 */
8636 while (first <= last)
8637 {
8638 x = first + ((unsigned)(last - first) >> 1);
8639 cmp = STRCMP(name, functions[x].f_name);
8640 if (cmp < 0)
8641 last = x - 1;
8642 else if (cmp > 0)
8643 first = x + 1;
8644 else
8645 return x;
8646 }
8647 return -1;
8648}
8649
8650/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8652 * name it contains, otherwise return "name".
8653 */
8654 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008655deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656{
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 int cc;
8659
8660 cc = name[*lenp];
8661 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008662 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 {
8668 *lenp = 0;
8669 return (char_u *)""; /* just in case */
8670 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008671 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 }
8674
8675 return name;
8676}
8677
8678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 * Allocate a variable for the result of a function.
8680 * Return OK or FAIL.
8681 */
8682 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008683get_func_tv(
8684 char_u *name, /* name of the function */
8685 int len, /* length of "name" */
8686 typval_T *rettv,
8687 char_u **arg, /* argument, pointing to the '(' */
8688 linenr_T firstline, /* first line of range */
8689 linenr_T lastline, /* last line of range */
8690 int *doesrange, /* return: function handled range */
8691 int evaluate,
8692 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
8694 char_u *argp;
8695 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008696 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 int argcount = 0; /* number of arguments found */
8698
8699 /*
8700 * Get the arguments.
8701 */
8702 argp = *arg;
8703 while (argcount < MAX_FUNC_ARGS)
8704 {
8705 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8706 if (*argp == ')' || *argp == ',' || *argp == NUL)
8707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8709 {
8710 ret = FAIL;
8711 break;
8712 }
8713 ++argcount;
8714 if (*argp != ',')
8715 break;
8716 }
8717 if (*argp == ')')
8718 ++argp;
8719 else
8720 ret = FAIL;
8721
8722 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008724 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 {
8727 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008728 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008730 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
8733 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 *arg = skipwhite(argp);
8737 return ret;
8738}
8739
8740
8741/*
8742 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008743 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008744 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747call_func(
8748 char_u *funcname, /* name of the function */
8749 int len, /* length of "name" */
8750 typval_T *rettv, /* return value goes here */
8751 int argcount, /* number of "argvars" */
8752 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008753 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008754 linenr_T firstline, /* first line of range */
8755 linenr_T lastline, /* last line of range */
8756 int *doesrange, /* return: function handled range */
8757 int evaluate,
8758 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#define ERROR_UNKNOWN 0
8762#define ERROR_TOOMANY 1
8763#define ERROR_TOOFEW 2
8764#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008765#define ERROR_DICT 4
8766#define ERROR_NONE 5
8767#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 int error = ERROR_NONE;
8769 int i;
8770 int llen;
8771 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772#define FLEN_FIXED 40
8773 char_u fname_buf[FLEN_FIXED + 1];
8774 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008775 char_u *name;
8776
8777 /* Make a copy of the name, if it comes from a funcref variable it could
8778 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008779 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008780 if (name == NULL)
8781 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782
8783 /*
8784 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8785 * Change <SNR>123_name() to K_SNR 123_name().
8786 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 llen = eval_fname_script(name);
8789 if (llen > 0)
8790 {
8791 fname_buf[0] = K_SPECIAL;
8792 fname_buf[1] = KS_EXTRA;
8793 fname_buf[2] = (int)KE_SNR;
8794 i = 3;
8795 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8796 {
8797 if (current_SID <= 0)
8798 error = ERROR_SCRIPT;
8799 else
8800 {
8801 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8802 i = (int)STRLEN(fname_buf);
8803 }
8804 }
8805 if (i + STRLEN(name + llen) < FLEN_FIXED)
8806 {
8807 STRCPY(fname_buf + i, name + llen);
8808 fname = fname_buf;
8809 }
8810 else
8811 {
8812 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8813 if (fname == NULL)
8814 error = ERROR_OTHER;
8815 else
8816 {
8817 mch_memmove(fname, fname_buf, (size_t)i);
8818 STRCPY(fname + i, name + llen);
8819 }
8820 }
8821 }
8822 else
8823 fname = name;
8824
8825 *doesrange = FALSE;
8826
8827
8828 /* execute the function if no errors detected and executing */
8829 if (evaluate && error == ERROR_NONE)
8830 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008831 char_u *rfname = fname;
8832
8833 /* Ignore "g:" before a function name. */
8834 if (fname[0] == 'g' && fname[1] == ':')
8835 rfname = fname + 2;
8836
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008837 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8838 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 error = ERROR_UNKNOWN;
8840
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008841 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
8843 /*
8844 * User defined function.
8845 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008846 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008849 /* Trigger FuncUndefined event, may load the function. */
8850 if (fp == NULL
8851 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008855 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008856 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
8858#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008859 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008860 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008861 {
8862 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008863 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008864 }
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (fp != NULL)
8867 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008868 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008872 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008874 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 else
8877 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008878 int did_save_redo = FALSE;
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 /*
8881 * Call the user function.
8882 * Save and restore search patterns, script variables and
8883 * redo buffer.
8884 */
8885 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008886#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008887 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008888#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008889 {
8890 saveRedobuff();
8891 did_save_redo = TRUE;
8892 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008893 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008895 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008896 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8897 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8898 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008899 /* Function was unreferenced while being used, free it
8900 * now. */
8901 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008902 if (did_save_redo)
8903 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 restore_search_patterns();
8905 error = ERROR_NONE;
8906 }
8907 }
8908 }
8909 else
8910 {
8911 /*
8912 * Find the function name in the table, call its implementation.
8913 */
8914 i = find_internal_func(fname);
8915 if (i >= 0)
8916 {
8917 if (argcount < functions[i].f_min_argc)
8918 error = ERROR_TOOFEW;
8919 else if (argcount > functions[i].f_max_argc)
8920 error = ERROR_TOOMANY;
8921 else
8922 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008923 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 error = ERROR_NONE;
8926 }
8927 }
8928 }
8929 /*
8930 * The function call (or "FuncUndefined" autocommand sequence) might
8931 * have been aborted by an error, an interrupt, or an explicitly thrown
8932 * exception that has not been caught so far. This situation can be
8933 * tested for by calling aborting(). For an error in an internal
8934 * function or for the "E132" error in call_user_func(), however, the
8935 * throw point at which the "force_abort" flag (temporarily reset by
8936 * emsg()) is normally updated has not been reached yet. We need to
8937 * update that flag first to make aborting() reliable.
8938 */
8939 update_force_abort();
8940 }
8941 if (error == ERROR_NONE)
8942 ret = OK;
8943
8944 /*
8945 * Report an error unless the argument evaluation or function call has been
8946 * cancelled due to an aborting error, an interrupt, or an exception.
8947 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008948 if (!aborting())
8949 {
8950 switch (error)
8951 {
8952 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008953 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008954 break;
8955 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008956 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008957 break;
8958 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008959 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008960 name);
8961 break;
8962 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008963 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008964 name);
8965 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008966 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008967 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 name);
8969 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008970 }
8971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 if (fname != name && fname != fname_buf)
8974 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008975 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 return ret;
8978}
8979
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008980/*
8981 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008982 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983 */
8984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008985emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008986{
8987 char_u *p;
8988
8989 if (*name == K_SPECIAL)
8990 p = concat_str((char_u *)"<SNR>", name + 3);
8991 else
8992 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008993 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008994 if (p != name)
8995 vim_free(p);
8996}
8997
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008998/*
8999 * Return TRUE for a non-zero Number and a non-empty String.
9000 */
9001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009002non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009003{
9004 return ((argvars[0].v_type == VAR_NUMBER
9005 && argvars[0].vval.v_number != 0)
9006 || (argvars[0].v_type == VAR_STRING
9007 && argvars[0].vval.v_string != NULL
9008 && *argvars[0].vval.v_string != NUL));
9009}
9010
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011/*********************************************
9012 * Implementation of the built-in functions
9013 */
9014
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009015#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009016static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009017
9018/*
9019 * Get the float value of "argvars[0]" into "f".
9020 * Returns FAIL when the argument is not a Number or Float.
9021 */
9022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009024{
9025 if (argvars[0].v_type == VAR_FLOAT)
9026 {
9027 *f = argvars[0].vval.v_float;
9028 return OK;
9029 }
9030 if (argvars[0].v_type == VAR_NUMBER)
9031 {
9032 *f = (float_T)argvars[0].vval.v_number;
9033 return OK;
9034 }
9035 EMSG(_("E808: Number or Float required"));
9036 return FAIL;
9037}
9038
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009039/*
9040 * "abs(expr)" function
9041 */
9042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009043f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009044{
9045 if (argvars[0].v_type == VAR_FLOAT)
9046 {
9047 rettv->v_type = VAR_FLOAT;
9048 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9049 }
9050 else
9051 {
9052 varnumber_T n;
9053 int error = FALSE;
9054
9055 n = get_tv_number_chk(&argvars[0], &error);
9056 if (error)
9057 rettv->vval.v_number = -1;
9058 else if (n > 0)
9059 rettv->vval.v_number = n;
9060 else
9061 rettv->vval.v_number = -n;
9062 }
9063}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009064
9065/*
9066 * "acos()" function
9067 */
9068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009069f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009070{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009071 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009072
9073 rettv->v_type = VAR_FLOAT;
9074 if (get_float_arg(argvars, &f) == OK)
9075 rettv->vval.v_float = acos(f);
9076 else
9077 rettv->vval.v_float = 0.0;
9078}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009079#endif
9080
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009082 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 */
9084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009085f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009090 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009092 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009093 && !tv_check_lock(l->lv_lock,
9094 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009095 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009097 }
9098 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009099 EMSG(_(e_listreq));
9100}
9101
9102/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009103 * "alloc_fail(id, countdown, repeat)" function
9104 */
9105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009107{
9108 if (argvars[0].v_type != VAR_NUMBER
9109 || argvars[0].vval.v_number <= 0
9110 || argvars[1].v_type != VAR_NUMBER
9111 || argvars[1].vval.v_number < 0
9112 || argvars[2].v_type != VAR_NUMBER)
9113 EMSG(_(e_invarg));
9114 else
9115 {
9116 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009117 if (alloc_fail_id >= aid_last)
9118 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009119 alloc_fail_countdown = argvars[1].vval.v_number;
9120 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009121 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009122 }
9123}
9124
9125/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009126 * "and(expr, expr)" function
9127 */
9128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009129f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009130{
9131 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9132 & get_tv_number_chk(&argvars[1], NULL);
9133}
9134
9135/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009136 * "append(lnum, string/list)" function
9137 */
9138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009139f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140{
9141 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 list_T *l = NULL;
9144 listitem_T *li = NULL;
9145 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009146 long added = 0;
9147
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009148 /* When coming here from Insert mode, sync undo, so that this can be
9149 * undone separately from what was previously inserted. */
9150 if (u_sync_once == 2)
9151 {
9152 u_sync_once = 1; /* notify that u_sync() was called */
9153 u_sync(TRUE);
9154 }
9155
Bram Moolenaar0d660222005-01-07 21:51:51 +00009156 lnum = get_tv_lnum(argvars);
9157 if (lnum >= 0
9158 && lnum <= curbuf->b_ml.ml_line_count
9159 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009160 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009161 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009163 l = argvars[1].vval.v_list;
9164 if (l == NULL)
9165 return;
9166 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009168 for (;;)
9169 {
9170 if (l == NULL)
9171 tv = &argvars[1]; /* append a string */
9172 else if (li == NULL)
9173 break; /* end of list */
9174 else
9175 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 line = get_tv_string_chk(tv);
9177 if (line == NULL) /* type error */
9178 {
9179 rettv->vval.v_number = 1; /* Failed */
9180 break;
9181 }
9182 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009183 ++added;
9184 if (l == NULL)
9185 break;
9186 li = li->li_next;
9187 }
9188
9189 appended_lines_mark(lnum, added);
9190 if (curwin->w_cursor.lnum > lnum)
9191 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 else
9194 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195}
9196
9197/*
9198 * "argc()" function
9199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009201f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204}
9205
9206/*
9207 * "argidx()" function
9208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213}
9214
9215/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009216 * "arglistid()" function
9217 */
9218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009219f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009220{
9221 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009222
9223 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009224 wp = find_tabwin(&argvars[0], &argvars[1]);
9225 if (wp != NULL)
9226 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009227}
9228
9229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 * "argv(nr)" function
9231 */
9232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009233f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 int idx;
9236
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009237 if (argvars[0].v_type != VAR_UNKNOWN)
9238 {
9239 idx = get_tv_number_chk(&argvars[0], NULL);
9240 if (idx >= 0 && idx < ARGCOUNT)
9241 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9242 else
9243 rettv->vval.v_string = NULL;
9244 rettv->v_type = VAR_STRING;
9245 }
9246 else if (rettv_list_alloc(rettv) == OK)
9247 for (idx = 0; idx < ARGCOUNT; ++idx)
9248 list_append_string(rettv->vval.v_list,
9249 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250}
9251
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009252static void prepare_assert_error(garray_T*gap);
9253static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9254static void assert_error(garray_T *gap);
9255static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256
9257/*
9258 * Prepare "gap" for an assert error and add the sourcing position.
9259 */
9260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009261prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009262{
9263 char buf[NUMBUFLEN];
9264
9265 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009266 if (sourcing_name != NULL)
9267 {
9268 ga_concat(gap, sourcing_name);
9269 if (sourcing_lnum > 0)
9270 ga_concat(gap, (char_u *)" ");
9271 }
9272 if (sourcing_lnum > 0)
9273 {
9274 sprintf(buf, "line %ld", (long)sourcing_lnum);
9275 ga_concat(gap, (char_u *)buf);
9276 }
9277 if (sourcing_name != NULL || sourcing_lnum > 0)
9278 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009279}
9280
9281/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009282 * Append "str" to "gap", escaping unprintable characters.
9283 * Changes NL to \n, CR to \r, etc.
9284 */
9285 static void
9286ga_concat_esc(garray_T *gap, char_u *str)
9287{
9288 char_u *p;
9289 char_u buf[NUMBUFLEN];
9290
9291 for (p = str; *p != NUL; ++p)
9292 switch (*p)
9293 {
9294 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9295 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9296 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9297 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9298 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9299 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9300 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9301 default:
9302 if (*p < ' ')
9303 {
9304 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9305 ga_concat(gap, buf);
9306 }
9307 else
9308 ga_append(gap, *p);
9309 break;
9310 }
9311}
9312
9313/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009314 * Fill "gap" with information about an assert error.
9315 */
9316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009317fill_assert_error(
9318 garray_T *gap,
9319 typval_T *opt_msg_tv,
9320 char_u *exp_str,
9321 typval_T *exp_tv,
9322 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009323{
9324 char_u numbuf[NUMBUFLEN];
9325 char_u *tofree;
9326
9327 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9328 {
9329 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9330 vim_free(tofree);
9331 }
9332 else
9333 {
9334 ga_concat(gap, (char_u *)"Expected ");
9335 if (exp_str == NULL)
9336 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009337 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009338 vim_free(tofree);
9339 }
9340 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009341 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009342 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009343 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009344 vim_free(tofree);
9345 }
9346}
Bram Moolenaar43345542015-11-29 17:35:35 +01009347
9348/*
9349 * Add an assert error to v:errors.
9350 */
9351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009352assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009353{
9354 struct vimvar *vp = &vimvars[VV_ERRORS];
9355
9356 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9357 /* Make sure v:errors is a list. */
9358 set_vim_var_list(VV_ERRORS, list_alloc());
9359 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9360}
9361
Bram Moolenaar43345542015-11-29 17:35:35 +01009362/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009363 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009367{
9368 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009369
9370 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9371 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009372 prepare_assert_error(&ga);
9373 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9374 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009375 ga_clear(&ga);
9376 }
9377}
9378
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009379/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009380 * "assert_exception(string[, msg])" function
9381 */
9382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009383f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009384{
9385 garray_T ga;
9386 char *error;
9387
9388 error = (char *)get_tv_string_chk(&argvars[0]);
9389 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9390 {
9391 prepare_assert_error(&ga);
9392 ga_concat(&ga, (char_u *)"v:exception is not set");
9393 assert_error(&ga);
9394 ga_clear(&ga);
9395 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009396 else if (error != NULL
9397 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009398 {
9399 prepare_assert_error(&ga);
9400 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9401 &vimvars[VV_EXCEPTION].vv_tv);
9402 assert_error(&ga);
9403 ga_clear(&ga);
9404 }
9405}
9406
9407/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009408 * "assert_fails(cmd [, error])" function
9409 */
9410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009411f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009412{
9413 char_u *cmd = get_tv_string_chk(&argvars[0]);
9414 garray_T ga;
9415
9416 called_emsg = FALSE;
9417 suppress_errthrow = TRUE;
9418 emsg_silent = TRUE;
9419 do_cmdline_cmd(cmd);
9420 if (!called_emsg)
9421 {
9422 prepare_assert_error(&ga);
9423 ga_concat(&ga, (char_u *)"command did not fail: ");
9424 ga_concat(&ga, cmd);
9425 assert_error(&ga);
9426 ga_clear(&ga);
9427 }
9428 else if (argvars[1].v_type != VAR_UNKNOWN)
9429 {
9430 char_u buf[NUMBUFLEN];
9431 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9432
9433 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9434 {
9435 prepare_assert_error(&ga);
9436 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9437 &vimvars[VV_ERRMSG].vv_tv);
9438 assert_error(&ga);
9439 ga_clear(&ga);
9440 }
9441 }
9442
9443 called_emsg = FALSE;
9444 suppress_errthrow = FALSE;
9445 emsg_silent = FALSE;
9446 emsg_on_display = FALSE;
9447 set_vim_var_string(VV_ERRMSG, NULL, 0);
9448}
9449
9450/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009451 * Common for assert_true() and assert_false().
9452 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009455{
9456 int error = FALSE;
9457 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009458
Bram Moolenaar37127922016-02-06 20:29:28 +01009459 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009460 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009461 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009462 if (argvars[0].v_type != VAR_NUMBER
9463 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9464 || error)
9465 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009466 prepare_assert_error(&ga);
9467 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009468 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 NULL, &argvars[0]);
9470 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009471 ga_clear(&ga);
9472 }
9473}
9474
9475/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009476 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009477 */
9478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009479f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009480{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009481 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009482}
9483
9484/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009485 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009486 */
9487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009488f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009489{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009490 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009491}
9492
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009494/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009495 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009500 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009501
9502 rettv->v_type = VAR_FLOAT;
9503 if (get_float_arg(argvars, &f) == OK)
9504 rettv->vval.v_float = asin(f);
9505 else
9506 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009507}
9508
9509/*
9510 * "atan()" function
9511 */
9512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009514{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009515 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009516
9517 rettv->v_type = VAR_FLOAT;
9518 if (get_float_arg(argvars, &f) == OK)
9519 rettv->vval.v_float = atan(f);
9520 else
9521 rettv->vval.v_float = 0.0;
9522}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009523
9524/*
9525 * "atan2()" function
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009529{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009530 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009531
9532 rettv->v_type = VAR_FLOAT;
9533 if (get_float_arg(argvars, &fx) == OK
9534 && get_float_arg(&argvars[1], &fy) == OK)
9535 rettv->vval.v_float = atan2(fx, fy);
9536 else
9537 rettv->vval.v_float = 0.0;
9538}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009539#endif
9540
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541/*
9542 * "browse(save, title, initdir, default)" function
9543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009545f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547#ifdef FEAT_BROWSE
9548 int save;
9549 char_u *title;
9550 char_u *initdir;
9551 char_u *defname;
9552 char_u buf[NUMBUFLEN];
9553 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 save = get_tv_number_chk(&argvars[0], &error);
9557 title = get_tv_string_chk(&argvars[1]);
9558 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9559 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 if (error || title == NULL || initdir == NULL || defname == NULL)
9562 rettv->vval.v_string = NULL;
9563 else
9564 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565 do_browse(save ? BROWSE_SAVE : 0,
9566 title, defname, NULL, initdir, NULL, curbuf);
9567#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009569#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009571}
9572
9573/*
9574 * "browsedir(title, initdir)" function
9575 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009578{
9579#ifdef FEAT_BROWSE
9580 char_u *title;
9581 char_u *initdir;
9582 char_u buf[NUMBUFLEN];
9583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 title = get_tv_string_chk(&argvars[0]);
9585 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 if (title == NULL || initdir == NULL)
9588 rettv->vval.v_string = NULL;
9589 else
9590 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009591 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596}
9597
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009598static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600/*
9601 * Find a buffer by number or exact name.
9602 */
9603 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009604find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009608 if (avar->v_type == VAR_NUMBER)
9609 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009610 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009613 if (buf == NULL)
9614 {
9615 /* No full path name match, try a match with a URL or a "nofile"
9616 * buffer, these don't use the full path. */
9617 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9618 if (buf->b_fname != NULL
9619 && (path_with_url(buf->b_fname)
9620#ifdef FEAT_QUICKFIX
9621 || bt_nofile(buf)
9622#endif
9623 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009624 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009625 break;
9626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 }
9628 return buf;
9629}
9630
9631/*
9632 * "bufexists(expr)" function
9633 */
9634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
9641 * "buflisted(expr)" function
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 buf_T *buf;
9647
9648 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650}
9651
9652/*
9653 * "bufloaded(expr)" function
9654 */
9655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009656f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 buf_T *buf;
9659
9660 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009664static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666/*
9667 * Get buffer by number or pattern.
9668 */
9669 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009670get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 int save_magic;
9674 char_u *save_cpo;
9675 buf_T *buf;
9676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 if (tv->v_type == VAR_NUMBER)
9678 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009679 if (tv->v_type != VAR_STRING)
9680 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 if (name == NULL || *name == NUL)
9682 return curbuf;
9683 if (name[0] == '$' && name[1] == NUL)
9684 return lastbuf;
9685
9686 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9687 save_magic = p_magic;
9688 p_magic = TRUE;
9689 save_cpo = p_cpo;
9690 p_cpo = (char_u *)"";
9691
9692 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009693 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694
9695 p_magic = save_magic;
9696 p_cpo = save_cpo;
9697
9698 /* If not found, try expanding the name, like done for bufexists(). */
9699 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701
9702 return buf;
9703}
9704
9705/*
9706 * "bufname(expr)" function
9707 */
9708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009709f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 buf_T *buf;
9712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009713 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009715 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 --emsg_off;
9722}
9723
9724/*
9725 * "bufnr(expr)" function
9726 */
9727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009728f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729{
9730 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009731 int error = FALSE;
9732 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009734 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009736 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009737 --emsg_off;
9738
9739 /* If the buffer isn't found and the second argument is not zero create a
9740 * new buffer. */
9741 if (buf == NULL
9742 && argvars[1].v_type != VAR_UNKNOWN
9743 && get_tv_number_chk(&argvars[1], &error) != 0
9744 && !error
9745 && (name = get_tv_string_chk(&argvars[0])) != NULL
9746 && !error)
9747 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9748
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753}
9754
9755/*
9756 * "bufwinnr(nr)" function
9757 */
9758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761#ifdef FEAT_WINDOWS
9762 win_T *wp;
9763 int winnr = 0;
9764#endif
9765 buf_T *buf;
9766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009769 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#ifdef FEAT_WINDOWS
9771 for (wp = firstwin; wp; wp = wp->w_next)
9772 {
9773 ++winnr;
9774 if (wp->w_buffer == buf)
9775 break;
9776 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#endif
9781 --emsg_off;
9782}
9783
9784/*
9785 * "byte2line(byte)" function
9786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792#else
9793 long boff = 0;
9794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009795 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 (linenr_T)0, &boff);
9801#endif
9802}
9803
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009805byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806{
9807#ifdef FEAT_MBYTE
9808 char_u *t;
9809#endif
9810 char_u *str;
9811 long idx;
9812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 str = get_tv_string_chk(&argvars[0]);
9814 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817 return;
9818
9819#ifdef FEAT_MBYTE
9820 t = str;
9821 for ( ; idx > 0; idx--)
9822 {
9823 if (*t == NUL) /* EOL reached */
9824 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009825 if (enc_utf8 && comp)
9826 t += utf_ptr2len(t);
9827 else
9828 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009829 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009830 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009831#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009834#endif
9835}
9836
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009837/*
9838 * "byteidx()" function
9839 */
9840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009841f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009842{
9843 byteidx(argvars, rettv, FALSE);
9844}
9845
9846/*
9847 * "byteidxcomp()" function
9848 */
9849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009850f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009851{
9852 byteidx(argvars, rettv, TRUE);
9853}
9854
Bram Moolenaardb913952012-06-29 12:54:53 +02009855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009856func_call(
9857 char_u *name,
9858 typval_T *args,
9859 dict_T *selfdict,
9860 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009861{
9862 listitem_T *item;
9863 typval_T argv[MAX_FUNC_ARGS + 1];
9864 int argc = 0;
9865 int dummy;
9866 int r = 0;
9867
9868 for (item = args->vval.v_list->lv_first; item != NULL;
9869 item = item->li_next)
9870 {
9871 if (argc == MAX_FUNC_ARGS)
9872 {
9873 EMSG(_("E699: Too many arguments"));
9874 break;
9875 }
9876 /* Make a copy of each argument. This is needed to be able to set
9877 * v_lock to VAR_FIXED in the copy without changing the original list.
9878 */
9879 copy_tv(&item->li_tv, &argv[argc++]);
9880 }
9881
9882 if (item == NULL)
9883 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9884 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9885 &dummy, TRUE, selfdict);
9886
9887 /* Free the arguments. */
9888 while (argc > 0)
9889 clear_tv(&argv[--argc]);
9890
9891 return r;
9892}
9893
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009894/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895 * "call(func, arglist)" function
9896 */
9897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009898f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899{
9900 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009902
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009903 if (argvars[1].v_type != VAR_LIST)
9904 {
9905 EMSG(_(e_listreq));
9906 return;
9907 }
9908 if (argvars[1].vval.v_list == NULL)
9909 return;
9910
9911 if (argvars[0].v_type == VAR_FUNC)
9912 func = argvars[0].vval.v_string;
9913 else
9914 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 if (*func == NUL)
9916 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 if (argvars[2].v_type != VAR_UNKNOWN)
9919 {
9920 if (argvars[2].v_type != VAR_DICT)
9921 {
9922 EMSG(_(e_dictreq));
9923 return;
9924 }
9925 selfdict = argvars[2].vval.v_dict;
9926 }
9927
Bram Moolenaardb913952012-06-29 12:54:53 +02009928 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009929}
9930
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009931#ifdef FEAT_FLOAT
9932/*
9933 * "ceil({float})" function
9934 */
9935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009936f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009937{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009938 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009939
9940 rettv->v_type = VAR_FLOAT;
9941 if (get_float_arg(argvars, &f) == OK)
9942 rettv->vval.v_float = ceil(f);
9943 else
9944 rettv->vval.v_float = 0.0;
9945}
9946#endif
9947
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009948#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009949/*
9950 * Get a callback from "arg". It can be a Funcref or a function name.
9951 * When "arg" is zero return an empty string.
9952 * Return NULL for an invalid argument.
9953 */
9954 static char_u *
9955get_callback(typval_T *arg)
9956{
9957 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9958 return arg->vval.v_string;
9959 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9960 return (char_u *)"";
9961 EMSG(_("E999: Invalid callback argument"));
9962 return NULL;
9963}
9964
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009965 static int
9966handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9967{
9968 char_u *val = get_tv_string(item);
9969
9970 opt->jo_set |= jo;
9971 if (STRCMP(val, "nl") == 0)
9972 *modep = MODE_NL;
9973 else if (STRCMP(val, "raw") == 0)
9974 *modep = MODE_RAW;
9975 else if (STRCMP(val, "js") == 0)
9976 *modep = MODE_JS;
9977 else if (STRCMP(val, "json") == 0)
9978 *modep = MODE_JSON;
9979 else
9980 {
9981 EMSG2(_(e_invarg2), val);
9982 return FAIL;
9983 }
9984 return OK;
9985}
9986
Bram Moolenaar187db502016-02-27 14:44:26 +01009987 static int
9988handle_io(typval_T *item, int part, jobopt_T *opt)
9989{
9990 char_u *val = get_tv_string(item);
9991
9992 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9993 if (STRCMP(val, "null") == 0)
9994 opt->jo_io[part] = JIO_NULL;
9995 else if (STRCMP(val, "pipe") == 0)
9996 opt->jo_io[part] = JIO_PIPE;
9997 else if (STRCMP(val, "file") == 0)
9998 opt->jo_io[part] = JIO_FILE;
9999 else if (STRCMP(val, "buffer") == 0)
10000 opt->jo_io[part] = JIO_BUFFER;
10001 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10002 opt->jo_io[part] = JIO_OUT;
10003 else
10004 {
10005 EMSG2(_(e_invarg2), val);
10006 return FAIL;
10007 }
10008 return OK;
10009}
10010
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010011 static void
10012clear_job_options(jobopt_T *opt)
10013{
10014 vim_memset(opt, 0, sizeof(jobopt_T));
10015}
10016
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010017/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010018 * Get the PART_ number from the first character of an option name.
10019 */
10020 static int
10021part_from_char(int c)
10022{
10023 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10024}
10025
10026/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010027 * Get the option entries from the dict in "tv", parse them and put the result
10028 * in "opt".
10029 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010030 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010031 */
10032 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010033get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010034{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010035 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010036 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010037 dict_T *dict;
10038 int todo;
10039 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010040 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010041
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010042 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043 if (tv->v_type == VAR_UNKNOWN)
10044 return OK;
10045 if (tv->v_type != VAR_DICT)
10046 {
10047 EMSG(_(e_invarg));
10048 return FAIL;
10049 }
10050 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010051 if (dict == NULL)
10052 return OK;
10053
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010054 todo = (int)dict->dv_hashtab.ht_used;
10055 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010057 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010058 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010059
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010060 if (STRCMP(hi->hi_key, "mode") == 0)
10061 {
10062 if (!(supported & JO_MODE))
10063 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010064 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010065 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010066 }
10067 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10068 {
10069 if (!(supported & JO_IN_MODE))
10070 break;
10071 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10072 == FAIL)
10073 return FAIL;
10074 }
10075 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10076 {
10077 if (!(supported & JO_OUT_MODE))
10078 break;
10079 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10080 == FAIL)
10081 return FAIL;
10082 }
10083 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10084 {
10085 if (!(supported & JO_ERR_MODE))
10086 break;
10087 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10088 == FAIL)
10089 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010090 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010091 else if (STRCMP(hi->hi_key, "in-io") == 0
10092 || STRCMP(hi->hi_key, "out-io") == 0
10093 || STRCMP(hi->hi_key, "err-io") == 0)
10094 {
10095 if (!(supported & JO_OUT_IO))
10096 break;
10097 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10098 return FAIL;
10099 }
10100 else if (STRCMP(hi->hi_key, "in-name") == 0
10101 || STRCMP(hi->hi_key, "out-name") == 0
10102 || STRCMP(hi->hi_key, "err-name") == 0)
10103 {
10104 part = part_from_char(*hi->hi_key);
10105
10106 if (!(supported & JO_OUT_IO))
10107 break;
10108 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10109 opt->jo_io_name[part] =
10110 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10111 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010112 else if (STRCMP(hi->hi_key, "callback") == 0)
10113 {
10114 if (!(supported & JO_CALLBACK))
10115 break;
10116 opt->jo_set |= JO_CALLBACK;
10117 opt->jo_callback = get_callback(item);
10118 if (opt->jo_callback == NULL)
10119 {
10120 EMSG2(_(e_invarg2), "callback");
10121 return FAIL;
10122 }
10123 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010124 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10125 {
10126 if (!(supported & JO_OUT_CALLBACK))
10127 break;
10128 opt->jo_set |= JO_OUT_CALLBACK;
10129 opt->jo_out_cb = get_callback(item);
10130 if (opt->jo_out_cb == NULL)
10131 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010132 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010133 return FAIL;
10134 }
10135 }
10136 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10137 {
10138 if (!(supported & JO_ERR_CALLBACK))
10139 break;
10140 opt->jo_set |= JO_ERR_CALLBACK;
10141 opt->jo_err_cb = get_callback(item);
10142 if (opt->jo_err_cb == NULL)
10143 {
10144 EMSG2(_(e_invarg2), "err-cb");
10145 return FAIL;
10146 }
10147 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010148 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10149 {
10150 if (!(supported & JO_CLOSE_CALLBACK))
10151 break;
10152 opt->jo_set |= JO_CLOSE_CALLBACK;
10153 opt->jo_close_cb = get_callback(item);
10154 if (opt->jo_close_cb == NULL)
10155 {
10156 EMSG2(_(e_invarg2), "close-cb");
10157 return FAIL;
10158 }
10159 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010160 else if (STRCMP(hi->hi_key, "waittime") == 0)
10161 {
10162 if (!(supported & JO_WAITTIME))
10163 break;
10164 opt->jo_set |= JO_WAITTIME;
10165 opt->jo_waittime = get_tv_number(item);
10166 }
10167 else if (STRCMP(hi->hi_key, "timeout") == 0)
10168 {
10169 if (!(supported & JO_TIMEOUT))
10170 break;
10171 opt->jo_set |= JO_TIMEOUT;
10172 opt->jo_timeout = get_tv_number(item);
10173 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010174 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10175 {
10176 if (!(supported & JO_OUT_TIMEOUT))
10177 break;
10178 opt->jo_set |= JO_OUT_TIMEOUT;
10179 opt->jo_out_timeout = get_tv_number(item);
10180 }
10181 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10182 {
10183 if (!(supported & JO_ERR_TIMEOUT))
10184 break;
10185 opt->jo_set |= JO_ERR_TIMEOUT;
10186 opt->jo_err_timeout = get_tv_number(item);
10187 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010188 else if (STRCMP(hi->hi_key, "part") == 0)
10189 {
10190 if (!(supported & JO_PART))
10191 break;
10192 opt->jo_set |= JO_PART;
10193 val = get_tv_string(item);
10194 if (STRCMP(val, "err") == 0)
10195 opt->jo_part = PART_ERR;
10196 else
10197 {
10198 EMSG2(_(e_invarg2), val);
10199 return FAIL;
10200 }
10201 }
10202 else if (STRCMP(hi->hi_key, "id") == 0)
10203 {
10204 if (!(supported & JO_ID))
10205 break;
10206 opt->jo_set |= JO_ID;
10207 opt->jo_id = get_tv_number(item);
10208 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010209 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10210 {
10211 if (!(supported & JO_STOPONEXIT))
10212 break;
10213 opt->jo_set |= JO_STOPONEXIT;
10214 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10215 opt->jo_soe_buf);
10216 if (opt->jo_stoponexit == NULL)
10217 {
10218 EMSG2(_(e_invarg2), "stoponexit");
10219 return FAIL;
10220 }
10221 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010222 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10223 {
10224 if (!(supported & JO_EXIT_CB))
10225 break;
10226 opt->jo_set |= JO_EXIT_CB;
10227 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010228 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010229 {
10230 EMSG2(_(e_invarg2), "exit-cb");
10231 return FAIL;
10232 }
10233 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010234 else
10235 break;
10236 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010237 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010238 if (todo > 0)
10239 {
10240 EMSG2(_(e_invarg2), hi->hi_key);
10241 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010242 }
10243
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010244 return OK;
10245}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010246#endif
10247
10248#ifdef FEAT_CHANNEL
10249/*
10250 * Get the channel from the argument.
10251 * Returns NULL if the handle is invalid.
10252 */
10253 static channel_T *
10254get_channel_arg(typval_T *tv)
10255{
10256 channel_T *channel;
10257
10258 if (tv->v_type != VAR_CHANNEL)
10259 {
10260 EMSG2(_(e_invarg2), get_tv_string(tv));
10261 return NULL;
10262 }
10263 channel = tv->vval.v_channel;
10264
10265 if (channel == NULL || !channel_is_open(channel))
10266 {
10267 EMSG(_("E906: not an open channel"));
10268 return NULL;
10269 }
10270 return channel;
10271}
10272
10273/*
10274 * "ch_close()" function
10275 */
10276 static void
10277f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10278{
10279 channel_T *channel = get_channel_arg(&argvars[0]);
10280
10281 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010282 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010283 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010284 channel_clear(channel);
10285 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010286}
10287
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010288/*
10289 * "ch_getbufnr()" function
10290 */
10291 static void
10292f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10293{
10294 channel_T *channel = get_channel_arg(&argvars[0]);
10295
10296 rettv->vval.v_number = -1;
10297 if (channel != NULL)
10298 {
10299 char_u *what = get_tv_string(&argvars[1]);
10300 int part;
10301
10302 if (STRCMP(what, "err") == 0)
10303 part = PART_ERR;
10304 else if (STRCMP(what, "out") == 0)
10305 part = PART_OUT;
10306 else if (STRCMP(what, "in") == 0)
10307 part = PART_IN;
10308 else
10309 part = PART_SOCK;
10310 if (channel->ch_part[part].ch_buffer != NULL)
10311 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10312 }
10313}
10314
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010315# ifdef FEAT_JOB
10316/*
10317 * "ch_getjob()" function
10318 */
10319 static void
10320f_ch_getjob(typval_T *argvars, typval_T *rettv)
10321{
10322 channel_T *channel = get_channel_arg(&argvars[0]);
10323
10324 if (channel != NULL)
10325 {
10326 rettv->v_type = VAR_JOB;
10327 rettv->vval.v_job = channel->ch_job;
10328 if (channel->ch_job != NULL)
10329 ++channel->ch_job->jv_refcount;
10330 }
10331}
10332# endif
10333
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010334/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010335 * "ch_log()" function
10336 */
10337 static void
10338f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10339{
10340 char_u *msg = get_tv_string(&argvars[0]);
10341 channel_T *channel = NULL;
10342
10343 if (argvars[1].v_type != VAR_UNKNOWN)
10344 channel = get_channel_arg(&argvars[1]);
10345
10346 ch_log(channel, (char *)msg);
10347}
10348
10349/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010350 * "ch_logfile()" function
10351 */
10352 static void
10353f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10354{
10355 char_u *fname;
10356 char_u *opt = (char_u *)"";
10357 char_u buf[NUMBUFLEN];
10358 FILE *file = NULL;
10359
10360 fname = get_tv_string(&argvars[0]);
10361 if (argvars[1].v_type == VAR_STRING)
10362 opt = get_tv_string_buf(&argvars[1], buf);
10363 if (*fname != NUL)
10364 {
10365 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10366 if (file == NULL)
10367 {
10368 EMSG2(_(e_notopen), fname);
10369 return;
10370 }
10371 }
10372 ch_logfile(file);
10373}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010374
10375/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010376 * "ch_open()" function
10377 */
10378 static void
10379f_ch_open(typval_T *argvars, typval_T *rettv)
10380{
10381 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010382 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010383 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010384 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010385 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010386 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010387
10388 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010389 rettv->v_type = VAR_CHANNEL;
10390 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010391
10392 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010393 if (argvars[1].v_type != VAR_UNKNOWN
10394 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010395 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010396 EMSG(_(e_invarg));
10397 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010398 }
10399
10400 /* parse address */
10401 p = vim_strchr(address, ':');
10402 if (p == NULL)
10403 {
10404 EMSG2(_(e_invarg2), address);
10405 return;
10406 }
10407 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010408 port = strtol((char *)p, &rest, 10);
10409 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010410 {
10411 p[-1] = ':';
10412 EMSG2(_(e_invarg2), address);
10413 return;
10414 }
10415
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010416 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010417 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010418 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010419 opt.jo_timeout = 2000;
10420 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010421 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010422 return;
10423 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010424 {
10425 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010426 return;
10427 }
10428
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010429 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010430 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010431 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010432 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010433 opt.jo_set = JO_ALL;
10434 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010435 }
10436}
10437
10438/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010439 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010440 */
10441 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010442common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010443{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010444 channel_T *channel;
10445 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010446 jobopt_T opt;
10447 int mode;
10448 int timeout;
10449 int id = -1;
10450 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010451
10452 /* return an empty string by default */
10453 rettv->v_type = VAR_STRING;
10454 rettv->vval.v_string = NULL;
10455
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010456 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010457 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10458 == FAIL)
10459 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010460
Bram Moolenaar77073442016-02-13 23:23:53 +010010461 channel = get_channel_arg(&argvars[0]);
10462 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010463 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010464 if (opt.jo_set & JO_PART)
10465 part = opt.jo_part;
10466 else
10467 part = channel_part_read(channel);
10468 mode = channel_get_mode(channel, part);
10469 timeout = channel_get_timeout(channel, part);
10470 if (opt.jo_set & JO_TIMEOUT)
10471 timeout = opt.jo_timeout;
10472
10473 if (raw || mode == MODE_RAW || mode == MODE_NL)
10474 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10475 else
10476 {
10477 if (opt.jo_set & JO_ID)
10478 id = opt.jo_id;
10479 channel_read_json_block(channel, part, timeout, id, &listtv);
10480 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010481 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010482 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010483 vim_free(listtv);
10484 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010485 else
10486 {
10487 rettv->v_type = VAR_SPECIAL;
10488 rettv->vval.v_number = VVAL_NONE;
10489 }
10490 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010491 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010492}
10493
10494/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010495 * "ch_read()" function
10496 */
10497 static void
10498f_ch_read(typval_T *argvars, typval_T *rettv)
10499{
10500 common_channel_read(argvars, rettv, FALSE);
10501}
10502
10503/*
10504 * "ch_readraw()" function
10505 */
10506 static void
10507f_ch_readraw(typval_T *argvars, typval_T *rettv)
10508{
10509 common_channel_read(argvars, rettv, TRUE);
10510}
10511
10512/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010513 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010514 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010515 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010516 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010517 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010518 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010519send_common(
10520 typval_T *argvars,
10521 char_u *text,
10522 int id,
10523 int eval,
10524 char *fun,
10525 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010526{
Bram Moolenaar77073442016-02-13 23:23:53 +010010527 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010528 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010529 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010530
Bram Moolenaar77073442016-02-13 23:23:53 +010010531 channel = get_channel_arg(&argvars[0]);
10532 if (channel == NULL)
10533 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010534 part_send = channel_part_send(channel);
10535 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010536
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010537 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010538 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10539 return NULL;
10540
Bram Moolenaara07fec92016-02-05 21:04:08 +010010541 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010542 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10543 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010544 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010545 {
10546 if (eval)
10547 {
10548 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10549 return NULL;
10550 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010551 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010552 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010553
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010554 if (channel_send(channel, part_send, text, fun) == OK
10555 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010556 return channel;
10557 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010558}
10559
10560/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010561 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010562 */
10563 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010564ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010565{
10566 char_u *text;
10567 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010568 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010569 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010570 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010571 int part_send;
10572 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010573 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010574
10575 /* return an empty string by default */
10576 rettv->v_type = VAR_STRING;
10577 rettv->vval.v_string = NULL;
10578
Bram Moolenaar77073442016-02-13 23:23:53 +010010579 channel = get_channel_arg(&argvars[0]);
10580 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010581 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010582 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010583
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010584 ch_mode = channel_get_mode(channel, part_send);
10585 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010586 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010587 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010588 return;
10589 }
10590
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010591 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010592 text = json_encode_nr_expr(id, &argvars[1],
10593 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010594 if (text == NULL)
10595 return;
10596
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010597 channel = send_common(argvars, text, id, eval,
10598 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010599 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010600 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010601 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010602 /* TODO: timeout from options */
10603 timeout = channel_get_timeout(channel, part_read);
10604 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10605 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010606 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010607 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010608
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010609 /* Move the item from the list and then change the type to
10610 * avoid the value being freed. */
10611 *rettv = list->lv_last->li_tv;
10612 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010613 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010614 }
10615 }
10616}
10617
10618/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010619 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010620 */
10621 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010622f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10623{
10624 ch_expr_common(argvars, rettv, TRUE);
10625}
10626
10627/*
10628 * "ch_sendexpr()" function
10629 */
10630 static void
10631f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10632{
10633 ch_expr_common(argvars, rettv, FALSE);
10634}
10635
10636/*
10637 * common for "ch_evalraw()" and "ch_sendraw()"
10638 */
10639 static void
10640ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010641{
10642 char_u buf[NUMBUFLEN];
10643 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010644 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010645 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010646 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010647
10648 /* return an empty string by default */
10649 rettv->v_type = VAR_STRING;
10650 rettv->vval.v_string = NULL;
10651
10652 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010653 channel = send_common(argvars, text, 0, eval,
10654 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10655 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010656 {
10657 /* TODO: timeout from options */
10658 timeout = channel_get_timeout(channel, part_read);
10659 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10660 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010661}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010662
10663/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010664 * "ch_evalraw()" function
10665 */
10666 static void
10667f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10668{
10669 ch_raw_common(argvars, rettv, TRUE);
10670}
10671
10672/*
10673 * "ch_sendraw()" function
10674 */
10675 static void
10676f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10677{
10678 ch_raw_common(argvars, rettv, FALSE);
10679}
10680
10681/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010682 * "ch_setoptions()" function
10683 */
10684 static void
10685f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10686{
10687 channel_T *channel;
10688 jobopt_T opt;
10689
10690 channel = get_channel_arg(&argvars[0]);
10691 if (channel == NULL)
10692 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010693 clear_job_options(&opt);
10694 if (get_job_options(&argvars[1], &opt,
10695 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010696 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010697 channel_set_options(channel, &opt);
10698}
10699
10700/*
10701 * "ch_status()" function
10702 */
10703 static void
10704f_ch_status(typval_T *argvars, typval_T *rettv)
10705{
10706 /* return an empty string by default */
10707 rettv->v_type = VAR_STRING;
10708
10709 if (argvars[0].v_type != VAR_CHANNEL)
10710 {
10711 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10712 rettv->vval.v_string = NULL;
10713 }
10714 else
10715 rettv->vval.v_string = vim_strsave(
10716 (char_u *)channel_status(argvars[0].vval.v_channel));
10717}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010718#endif
10719
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010720/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010721 * "changenr()" function
10722 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010724f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010725{
10726 rettv->vval.v_number = curbuf->b_u_seq_cur;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "char2nr(string)" function
10731 */
10732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
10735#ifdef FEAT_MBYTE
10736 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010737 {
10738 int utf8 = 0;
10739
10740 if (argvars[1].v_type != VAR_UNKNOWN)
10741 utf8 = get_tv_number_chk(&argvars[1], NULL);
10742
10743 if (utf8)
10744 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10745 else
10746 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 else
10749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751}
10752
10753/*
10754 * "cindent(lnum)" function
10755 */
10756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010757f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758{
10759#ifdef FEAT_CINDENT
10760 pos_T pos;
10761 linenr_T lnum;
10762
10763 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010764 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10766 {
10767 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010768 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 curwin->w_cursor = pos;
10770 }
10771 else
10772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774}
10775
10776/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010777 * "clearmatches()" function
10778 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010780f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010781{
10782#ifdef FEAT_SEARCH_EXTRA
10783 clear_matches(curwin);
10784#endif
10785}
10786
10787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 * "col(string)" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
10793 colnr_T col = 0;
10794 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010795 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010797 fp = var2fpos(&argvars[0], FALSE, &fnum);
10798 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 {
10800 if (fp->col == MAXCOL)
10801 {
10802 /* '> can be MAXCOL, get the length of the line then */
10803 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010804 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 else
10806 col = MAXCOL;
10807 }
10808 else
10809 {
10810 col = fp->col + 1;
10811#ifdef FEAT_VIRTUALEDIT
10812 /* col(".") when the cursor is on the NUL at the end of the line
10813 * because of "coladd" can be seen as an extra column. */
10814 if (virtual_active() && fp == &curwin->w_cursor)
10815 {
10816 char_u *p = ml_get_cursor();
10817
10818 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10819 curwin->w_virtcol - curwin->w_cursor.coladd))
10820 {
10821# ifdef FEAT_MBYTE
10822 int l;
10823
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010824 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 col += l;
10826# else
10827 if (*p != NUL && p[1] == NUL)
10828 ++col;
10829# endif
10830 }
10831 }
10832#endif
10833 }
10834 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836}
10837
Bram Moolenaar572cb562005-08-05 21:35:02 +000010838#if defined(FEAT_INS_EXPAND)
10839/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010840 * "complete()" function
10841 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010843f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010844{
10845 int startcol;
10846
10847 if ((State & INSERT) == 0)
10848 {
10849 EMSG(_("E785: complete() can only be used in Insert mode"));
10850 return;
10851 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010852
10853 /* Check for undo allowed here, because if something was already inserted
10854 * the line was already saved for undo and this check isn't done. */
10855 if (!undo_allowed())
10856 return;
10857
Bram Moolenaarade00832006-03-10 21:46:58 +000010858 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10859 {
10860 EMSG(_(e_invarg));
10861 return;
10862 }
10863
10864 startcol = get_tv_number_chk(&argvars[0], NULL);
10865 if (startcol <= 0)
10866 return;
10867
10868 set_completion(startcol - 1, argvars[1].vval.v_list);
10869}
10870
10871/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010872 * "complete_add()" function
10873 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010876{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010877 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010878}
10879
10880/*
10881 * "complete_check()" function
10882 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010884f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010885{
10886 int saved = RedrawingDisabled;
10887
10888 RedrawingDisabled = 0;
10889 ins_compl_check_keys(0);
10890 rettv->vval.v_number = compl_interrupted;
10891 RedrawingDisabled = saved;
10892}
10893#endif
10894
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895/*
10896 * "confirm(message, buttons[, default [, type]])" function
10897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010899f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10902 char_u *message;
10903 char_u *buttons = NULL;
10904 char_u buf[NUMBUFLEN];
10905 char_u buf2[NUMBUFLEN];
10906 int def = 1;
10907 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908 char_u *typestr;
10909 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 message = get_tv_string_chk(&argvars[0]);
10912 if (message == NULL)
10913 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010914 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10917 if (buttons == NULL)
10918 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010919 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010922 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10925 if (typestr == NULL)
10926 error = TRUE;
10927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 switch (TOUPPER_ASC(*typestr))
10930 {
10931 case 'E': type = VIM_ERROR; break;
10932 case 'Q': type = VIM_QUESTION; break;
10933 case 'I': type = VIM_INFO; break;
10934 case 'W': type = VIM_WARNING; break;
10935 case 'G': type = VIM_GENERIC; break;
10936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 }
10938 }
10939 }
10940 }
10941
10942 if (buttons == NULL || *buttons == NUL)
10943 buttons = (char_u *)_("&Ok");
10944
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010945 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010947 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948#endif
10949}
10950
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951/*
10952 * "copy()" function
10953 */
10954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010955f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010957 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010960#ifdef FEAT_FLOAT
10961/*
10962 * "cos()" function
10963 */
10964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010965f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010966{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010967 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010968
10969 rettv->v_type = VAR_FLOAT;
10970 if (get_float_arg(argvars, &f) == OK)
10971 rettv->vval.v_float = cos(f);
10972 else
10973 rettv->vval.v_float = 0.0;
10974}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010975
10976/*
10977 * "cosh()" function
10978 */
10979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010980f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010981{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010982 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010983
10984 rettv->v_type = VAR_FLOAT;
10985 if (get_float_arg(argvars, &f) == OK)
10986 rettv->vval.v_float = cosh(f);
10987 else
10988 rettv->vval.v_float = 0.0;
10989}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010990#endif
10991
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010993 * "count()" function
10994 */
10995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010996f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010997{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010998 long n = 0;
10999 int ic = FALSE;
11000
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011002 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 listitem_T *li;
11004 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011006
Bram Moolenaare9a41262005-01-15 22:18:47 +000011007 if ((l = argvars[0].vval.v_list) != NULL)
11008 {
11009 li = l->lv_first;
11010 if (argvars[2].v_type != VAR_UNKNOWN)
11011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012 int error = FALSE;
11013
11014 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 if (argvars[3].v_type != VAR_UNKNOWN)
11016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 idx = get_tv_number_chk(&argvars[3], &error);
11018 if (!error)
11019 {
11020 li = list_find(l, idx);
11021 if (li == NULL)
11022 EMSGN(_(e_listidx), idx);
11023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 if (error)
11026 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011027 }
11028
11029 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011030 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 ++n;
11032 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 else if (argvars[0].v_type == VAR_DICT)
11035 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 int todo;
11037 dict_T *d;
11038 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011039
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011040 if ((d = argvars[0].vval.v_dict) != NULL)
11041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 int error = FALSE;
11043
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 if (argvars[2].v_type != VAR_UNKNOWN)
11045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011047 if (argvars[3].v_type != VAR_UNKNOWN)
11048 EMSG(_(e_invarg));
11049 }
11050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011051 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011053 {
11054 if (!HASHITEM_EMPTY(hi))
11055 {
11056 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011057 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011058 ++n;
11059 }
11060 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 }
11062 }
11063 else
11064 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011065 rettv->vval.v_number = n;
11066}
11067
11068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11070 *
11071 * Checks the existence of a cscope connection.
11072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011074f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075{
11076#ifdef FEAT_CSCOPE
11077 int num = 0;
11078 char_u *dbpath = NULL;
11079 char_u *prepend = NULL;
11080 char_u buf[NUMBUFLEN];
11081
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011082 if (argvars[0].v_type != VAR_UNKNOWN
11083 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 num = (int)get_tv_number(&argvars[0]);
11086 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 }
11090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092#endif
11093}
11094
11095/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011096 * "cursor(lnum, col)" function, or
11097 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011099 * Moves the cursor to the specified line and column.
11100 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011103f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104{
11105 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011106#ifdef FEAT_VIRTUALEDIT
11107 long coladd = 0;
11108#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011109 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011111 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011112 if (argvars[1].v_type == VAR_UNKNOWN)
11113 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011114 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011115 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011116
Bram Moolenaar493c1782014-05-28 14:34:46 +020011117 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011118 {
11119 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011120 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011121 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011122 line = pos.lnum;
11123 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011124#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011125 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011126#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011127 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011128 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011129 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011130 set_curswant = FALSE;
11131 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011132 }
11133 else
11134 {
11135 line = get_tv_lnum(argvars);
11136 col = get_tv_number_chk(&argvars[1], NULL);
11137#ifdef FEAT_VIRTUALEDIT
11138 if (argvars[2].v_type != VAR_UNKNOWN)
11139 coladd = get_tv_number_chk(&argvars[2], NULL);
11140#endif
11141 }
11142 if (line < 0 || col < 0
11143#ifdef FEAT_VIRTUALEDIT
11144 || coladd < 0
11145#endif
11146 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 if (line > 0)
11149 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 if (col > 0)
11151 curwin->w_cursor.col = col - 1;
11152#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011153 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154#endif
11155
11156 /* Make sure the cursor is in a valid position. */
11157 check_cursor();
11158#ifdef FEAT_MBYTE
11159 /* Correct cursor for multi-byte character. */
11160 if (has_mbyte)
11161 mb_adjust_cursor();
11162#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011163
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011164 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011165 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166}
11167
11168/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011169 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 */
11171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011172f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011174 int noref = 0;
11175
11176 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011178 if (noref < 0 || noref > 1)
11179 EMSG(_(e_invarg));
11180 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011181 {
11182 current_copyID += COPYID_INC;
11183 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185}
11186
11187/*
11188 * "delete()" function
11189 */
11190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011191f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011193 char_u nbuf[NUMBUFLEN];
11194 char_u *name;
11195 char_u *flags;
11196
11197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011199 return;
11200
11201 name = get_tv_string(&argvars[0]);
11202 if (name == NULL || *name == NUL)
11203 {
11204 EMSG(_(e_invarg));
11205 return;
11206 }
11207
11208 if (argvars[1].v_type != VAR_UNKNOWN)
11209 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011211 flags = (char_u *)"";
11212
11213 if (*flags == NUL)
11214 /* delete a file */
11215 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11216 else if (STRCMP(flags, "d") == 0)
11217 /* delete an empty directory */
11218 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11219 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011220 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011221 rettv->vval.v_number = delete_recursive(name);
11222 else
11223 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
11226/*
11227 * "did_filetype()" function
11228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011230f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231{
11232#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234#endif
11235}
11236
11237/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011238 * "diff_filler()" function
11239 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011241f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242{
11243#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011245#endif
11246}
11247
11248/*
11249 * "diff_hlID()" function
11250 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011252f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011253{
11254#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011256 static linenr_T prev_lnum = 0;
11257 static int changedtick = 0;
11258 static int fnum = 0;
11259 static int change_start = 0;
11260 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011261 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011262 int filler_lines;
11263 int col;
11264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 if (lnum < 0) /* ignore type error in {lnum} arg */
11266 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011267 if (lnum != prev_lnum
11268 || changedtick != curbuf->b_changedtick
11269 || fnum != curbuf->b_fnum)
11270 {
11271 /* New line, buffer, change: need to get the values. */
11272 filler_lines = diff_check(curwin, lnum);
11273 if (filler_lines < 0)
11274 {
11275 if (filler_lines == -1)
11276 {
11277 change_start = MAXCOL;
11278 change_end = -1;
11279 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11280 hlID = HLF_ADD; /* added line */
11281 else
11282 hlID = HLF_CHD; /* changed line */
11283 }
11284 else
11285 hlID = HLF_ADD; /* added line */
11286 }
11287 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011288 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011289 prev_lnum = lnum;
11290 changedtick = curbuf->b_changedtick;
11291 fnum = curbuf->b_fnum;
11292 }
11293
11294 if (hlID == HLF_CHD || hlID == HLF_TXD)
11295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011297 if (col >= change_start && col <= change_end)
11298 hlID = HLF_TXD; /* changed text */
11299 else
11300 hlID = HLF_CHD; /* changed line */
11301 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011302 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011303#endif
11304}
11305
11306/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011307 * "disable_char_avail_for_testing({expr})" function
11308 */
11309 static void
11310f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11311{
11312 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11313}
11314
11315/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011316 * "empty({expr})" function
11317 */
11318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011319f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011320{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011321 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011322
11323 switch (argvars[0].v_type)
11324 {
11325 case VAR_STRING:
11326 case VAR_FUNC:
11327 n = argvars[0].vval.v_string == NULL
11328 || *argvars[0].vval.v_string == NUL;
11329 break;
11330 case VAR_NUMBER:
11331 n = argvars[0].vval.v_number == 0;
11332 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011333 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011334#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011335 n = argvars[0].vval.v_float == 0.0;
11336 break;
11337#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011338 case VAR_LIST:
11339 n = argvars[0].vval.v_list == NULL
11340 || argvars[0].vval.v_list->lv_first == NULL;
11341 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 case VAR_DICT:
11343 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011344 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011346 case VAR_SPECIAL:
11347 n = argvars[0].vval.v_number != VVAL_TRUE;
11348 break;
11349
Bram Moolenaar835dc632016-02-07 14:27:38 +010011350 case VAR_JOB:
11351#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011352 n = argvars[0].vval.v_job == NULL
11353 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11354 break;
11355#endif
11356 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011357#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011358 n = argvars[0].vval.v_channel == NULL
11359 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011360 break;
11361#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011362 case VAR_UNKNOWN:
11363 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11364 n = TRUE;
11365 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011366 }
11367
11368 rettv->vval.v_number = n;
11369}
11370
11371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 * "escape({string}, {chars})" function
11373 */
11374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011375f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376{
11377 char_u buf[NUMBUFLEN];
11378
Bram Moolenaar758711c2005-02-02 23:11:38 +000011379 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11380 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
11384/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011385 * "eval()" function
11386 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011388f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011389{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011390 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 s = get_tv_string_chk(&argvars[0]);
11393 if (s != NULL)
11394 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011395
Bram Moolenaar615b9972015-01-14 17:15:05 +010011396 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11398 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011399 if (p != NULL && !aborting())
11400 EMSG2(_(e_invexpr2), p);
11401 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011403 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011405 else if (*s != NUL)
11406 EMSG(_(e_trailing));
11407}
11408
11409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 * "eventhandler()" function
11411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011413f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
11418/*
11419 * "executable()" function
11420 */
11421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011422f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011424 char_u *name = get_tv_string(&argvars[0]);
11425
11426 /* Check in $PATH and also check directly if there is a directory name. */
11427 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11428 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011429}
11430
11431/*
11432 * "exepath()" function
11433 */
11434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011435f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011436{
11437 char_u *p = NULL;
11438
Bram Moolenaarb5971142015-03-21 17:32:19 +010011439 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011440 rettv->v_type = VAR_STRING;
11441 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442}
11443
11444/*
11445 * "exists()" function
11446 */
11447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011448f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449{
11450 char_u *p;
11451 char_u *name;
11452 int n = FALSE;
11453 int len = 0;
11454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 if (*p == '$') /* environment variable */
11457 {
11458 /* first try "normal" environment variables (fast) */
11459 if (mch_getenv(p + 1) != NULL)
11460 n = TRUE;
11461 else
11462 {
11463 /* try expanding things like $VIM and ${HOME} */
11464 p = expand_env_save(p);
11465 if (p != NULL && *p != '$')
11466 n = TRUE;
11467 vim_free(p);
11468 }
11469 }
11470 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011473 if (*skipwhite(p) != NUL)
11474 n = FALSE; /* trailing garbage */
11475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 else if (*p == '*') /* internal or user defined function */
11477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 }
11480 else if (*p == ':')
11481 {
11482 n = cmd_exists(p + 1);
11483 }
11484 else if (*p == '#')
11485 {
11486#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011487 if (p[1] == '#')
11488 n = autocmd_supported(p + 2);
11489 else
11490 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491#endif
11492 }
11493 else /* internal variable */
11494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011495 char_u *tofree;
11496 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011498 /* get_name_len() takes care of expanding curly braces */
11499 name = p;
11500 len = get_name_len(&p, &tofree, TRUE, FALSE);
11501 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011503 if (tofree != NULL)
11504 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011505 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011506 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011508 /* handle d.key, l[idx], f(expr) */
11509 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11510 if (n)
11511 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011514 if (*p != NUL)
11515 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011517 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011523#ifdef FEAT_FLOAT
11524/*
11525 * "exp()" function
11526 */
11527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011528f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011529{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011530 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011531
11532 rettv->v_type = VAR_FLOAT;
11533 if (get_float_arg(argvars, &f) == OK)
11534 rettv->vval.v_float = exp(f);
11535 else
11536 rettv->vval.v_float = 0.0;
11537}
11538#endif
11539
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540/*
11541 * "expand()" function
11542 */
11543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011544f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546 char_u *s;
11547 int len;
11548 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011549 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011551 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011552 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011555 if (argvars[1].v_type != VAR_UNKNOWN
11556 && argvars[2].v_type != VAR_UNKNOWN
11557 && get_tv_number_chk(&argvars[2], &error)
11558 && !error)
11559 {
11560 rettv->v_type = VAR_LIST;
11561 rettv->vval.v_list = NULL;
11562 }
11563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 if (*s == '%' || *s == '#' || *s == '<')
11566 {
11567 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011568 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011570 if (rettv->v_type == VAR_LIST)
11571 {
11572 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11573 list_append_string(rettv->vval.v_list, result, -1);
11574 else
11575 vim_free(result);
11576 }
11577 else
11578 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 }
11580 else
11581 {
11582 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011583 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011584 if (argvars[1].v_type != VAR_UNKNOWN
11585 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011586 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011587 if (!error)
11588 {
11589 ExpandInit(&xpc);
11590 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011591 if (p_wic)
11592 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011593 if (rettv->v_type == VAR_STRING)
11594 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11595 options, WILD_ALL);
11596 else if (rettv_list_alloc(rettv) != FAIL)
11597 {
11598 int i;
11599
11600 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11601 for (i = 0; i < xpc.xp_numfiles; i++)
11602 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11603 ExpandCleanup(&xpc);
11604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 }
11606 else
11607 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
11609}
11610
11611/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011612 * Go over all entries in "d2" and add them to "d1".
11613 * When "action" is "error" then a duplicate key is an error.
11614 * When "action" is "force" then a duplicate key is overwritten.
11615 * Otherwise duplicate keys are ignored ("action" is "keep").
11616 */
11617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011618dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011619{
11620 dictitem_T *di1;
11621 hashitem_T *hi2;
11622 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011623 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011624
11625 todo = (int)d2->dv_hashtab.ht_used;
11626 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11627 {
11628 if (!HASHITEM_EMPTY(hi2))
11629 {
11630 --todo;
11631 di1 = dict_find(d1, hi2->hi_key, -1);
11632 if (d1->dv_scope != 0)
11633 {
11634 /* Disallow replacing a builtin function in l: and g:.
11635 * Check the key to be valid when adding to any
11636 * scope. */
11637 if (d1->dv_scope == VAR_DEF_SCOPE
11638 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11639 && var_check_func_name(hi2->hi_key,
11640 di1 == NULL))
11641 break;
11642 if (!valid_varname(hi2->hi_key))
11643 break;
11644 }
11645 if (di1 == NULL)
11646 {
11647 di1 = dictitem_copy(HI2DI(hi2));
11648 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11649 dictitem_free(di1);
11650 }
11651 else if (*action == 'e')
11652 {
11653 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11654 break;
11655 }
11656 else if (*action == 'f' && HI2DI(hi2) != di1)
11657 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011658 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11659 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011660 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011661 clear_tv(&di1->di_tv);
11662 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11663 }
11664 }
11665 }
11666}
11667
11668/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011669 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011670 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011671 */
11672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011673f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011674{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011675 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011676
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011678 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011679 list_T *l1, *l2;
11680 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011681 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011683
Bram Moolenaare9a41262005-01-15 22:18:47 +000011684 l1 = argvars[0].vval.v_list;
11685 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011686 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011687 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011688 {
11689 if (argvars[2].v_type != VAR_UNKNOWN)
11690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 before = get_tv_number_chk(&argvars[2], &error);
11692 if (error)
11693 return; /* type error; errmsg already given */
11694
Bram Moolenaar758711c2005-02-02 23:11:38 +000011695 if (before == l1->lv_len)
11696 item = NULL;
11697 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011698 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011699 item = list_find(l1, before);
11700 if (item == NULL)
11701 {
11702 EMSGN(_(e_listidx), before);
11703 return;
11704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 }
11706 }
11707 else
11708 item = NULL;
11709 list_extend(l1, l2, item);
11710
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 copy_tv(&argvars[0], rettv);
11712 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011714 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11715 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011716 dict_T *d1, *d2;
11717 char_u *action;
11718 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719
11720 d1 = argvars[0].vval.v_dict;
11721 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011722 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011723 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011724 {
11725 /* Check the third argument. */
11726 if (argvars[2].v_type != VAR_UNKNOWN)
11727 {
11728 static char *(av[]) = {"keep", "force", "error"};
11729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 action = get_tv_string_chk(&argvars[2]);
11731 if (action == NULL)
11732 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 for (i = 0; i < 3; ++i)
11734 if (STRCMP(action, av[i]) == 0)
11735 break;
11736 if (i == 3)
11737 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011738 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 return;
11740 }
11741 }
11742 else
11743 action = (char_u *)"force";
11744
Bram Moolenaara9922d62013-05-30 13:01:18 +020011745 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 copy_tv(&argvars[0], rettv);
11748 }
11749 }
11750 else
11751 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011752}
11753
11754/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011755 * "feedkeys()" function
11756 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011759{
11760 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011761 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011762 char_u *keys, *flags;
11763 char_u nbuf[NUMBUFLEN];
11764 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011765 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011766 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011767
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011768 /* This is not allowed in the sandbox. If the commands would still be
11769 * executed in the sandbox it would be OK, but it probably happens later,
11770 * when "sandbox" is no longer set. */
11771 if (check_secure())
11772 return;
11773
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011774 keys = get_tv_string(&argvars[0]);
11775 if (*keys != NUL)
11776 {
11777 if (argvars[1].v_type != VAR_UNKNOWN)
11778 {
11779 flags = get_tv_string_buf(&argvars[1], nbuf);
11780 for ( ; *flags != NUL; ++flags)
11781 {
11782 switch (*flags)
11783 {
11784 case 'n': remap = FALSE; break;
11785 case 'm': remap = TRUE; break;
11786 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011787 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011788 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011789 }
11790 }
11791 }
11792
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011793 /* Need to escape K_SPECIAL and CSI before putting the string in the
11794 * typeahead buffer. */
11795 keys_esc = vim_strsave_escape_csi(keys);
11796 if (keys_esc != NULL)
11797 {
11798 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011799 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011800 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011801 if (vgetc_busy)
11802 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011803 if (execute)
11804 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011805 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011806 }
11807}
11808
11809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 * "filereadable()" function
11811 */
11812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011813f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011815 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 char_u *p;
11817 int n;
11818
Bram Moolenaarc236c162008-07-13 17:41:49 +000011819#ifndef O_NONBLOCK
11820# define O_NONBLOCK 0
11821#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011823 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11824 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 {
11826 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011827 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 }
11829 else
11830 n = FALSE;
11831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833}
11834
11835/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011836 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 * rights to write into.
11838 */
11839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011840f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011842 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843}
11844
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011845 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011846findfilendir(
11847 typval_T *argvars UNUSED,
11848 typval_T *rettv,
11849 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011850{
11851#ifdef FEAT_SEARCHPATH
11852 char_u *fname;
11853 char_u *fresult = NULL;
11854 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11855 char_u *p;
11856 char_u pathbuf[NUMBUFLEN];
11857 int count = 1;
11858 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011859 int error = FALSE;
11860#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011861
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011862 rettv->vval.v_string = NULL;
11863 rettv->v_type = VAR_STRING;
11864
11865#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011868 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11871 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011872 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 else
11874 {
11875 if (*p != NUL)
11876 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011879 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011880 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011881 }
11882
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011883 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11884 error = TRUE;
11885
11886 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 do
11889 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011890 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011891 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 fresult = find_file_in_path_option(first ? fname : NULL,
11893 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011894 0, first, path,
11895 find_what,
11896 curbuf->b_ffname,
11897 find_what == FINDFILE_DIR
11898 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011900
11901 if (fresult != NULL && rettv->v_type == VAR_LIST)
11902 list_append_string(rettv->vval.v_list, fresult, -1);
11903
11904 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011906
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011907 if (rettv->v_type == VAR_STRING)
11908 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011909#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011910}
11911
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011912static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11913static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011914
11915/*
11916 * Implementation of map() and filter().
11917 */
11918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011919filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011920{
11921 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011922 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011923 listitem_T *li, *nli;
11924 list_T *l = NULL;
11925 dictitem_T *di;
11926 hashtab_T *ht;
11927 hashitem_T *hi;
11928 dict_T *d = NULL;
11929 typval_T save_val;
11930 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011931 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011932 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011933 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011934 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011935 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011936 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011937 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011938
Bram Moolenaare9a41262005-01-15 22:18:47 +000011939 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011940 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011941 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011942 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011943 return;
11944 }
11945 else if (argvars[0].v_type == VAR_DICT)
11946 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011947 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011948 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949 return;
11950 }
11951 else
11952 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011953 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011954 return;
11955 }
11956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 expr = get_tv_string_buf_chk(&argvars[1], buf);
11958 /* On type errors, the preceding call has already displayed an error
11959 * message. Avoid a misleading error message for an empty string that
11960 * was not passed as argument. */
11961 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 prepare_vimvar(VV_VAL, &save_val);
11964 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011965
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011966 /* We reset "did_emsg" to be able to detect whether an error
11967 * occurred during evaluation of the expression. */
11968 save_did_emsg = did_emsg;
11969 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011970
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011971 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 vimvars[VV_KEY].vv_type = VAR_STRING;
11975
11976 ht = &d->dv_hashtab;
11977 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011978 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 if (!HASHITEM_EMPTY(hi))
11982 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011983 int r;
11984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 --todo;
11986 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011987 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011988 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11989 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 break;
11991 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011992 r = filter_map_one(&di->di_tv, expr, map, &rem);
11993 clear_tv(&vimvars[VV_KEY].vv_tv);
11994 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 break;
11996 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011997 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011998 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11999 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012001 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 }
12004 }
12005 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 }
12007 else
12008 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012009 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 for (li = l->lv_first; li != NULL; li = nli)
12012 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012013 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012014 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012015 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012016 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012017 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012018 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012019 break;
12020 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012022 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012023 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012024 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012025
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012026 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012028
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012029 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031
12032 copy_tv(&argvars[0], rettv);
12033}
12034
12035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012036filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012037{
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012039 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012040 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012041
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012043 s = expr;
12044 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012045 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012046 if (*s != NUL) /* check for trailing chars after expr */
12047 {
12048 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012049 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012050 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012051 }
12052 if (map)
12053 {
12054 /* map(): replace the list item value */
12055 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012056 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012057 *tv = rettv;
12058 }
12059 else
12060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 int error = FALSE;
12062
Bram Moolenaare9a41262005-01-15 22:18:47 +000012063 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012064 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012065 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 /* On type error, nothing has been removed; return FAIL to stop the
12067 * loop. The error message was given by get_tv_number_chk(). */
12068 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012069 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012070 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012071 retval = OK;
12072theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012074 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012075}
12076
12077/*
12078 * "filter()" function
12079 */
12080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012081f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012082{
12083 filter_map(argvars, rettv, FALSE);
12084}
12085
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012086/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012087 * "finddir({fname}[, {path}[, {count}]])" function
12088 */
12089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012091{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012092 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093}
12094
12095/*
12096 * "findfile({fname}[, {path}[, {count}]])" function
12097 */
12098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012099f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012100{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012101 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012102}
12103
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012104#ifdef FEAT_FLOAT
12105/*
12106 * "float2nr({float})" function
12107 */
12108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012109f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012110{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012111 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012112
12113 if (get_float_arg(argvars, &f) == OK)
12114 {
12115 if (f < -0x7fffffff)
12116 rettv->vval.v_number = -0x7fffffff;
12117 else if (f > 0x7fffffff)
12118 rettv->vval.v_number = 0x7fffffff;
12119 else
12120 rettv->vval.v_number = (varnumber_T)f;
12121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012122}
12123
12124/*
12125 * "floor({float})" function
12126 */
12127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012128f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012129{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012130 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012131
12132 rettv->v_type = VAR_FLOAT;
12133 if (get_float_arg(argvars, &f) == OK)
12134 rettv->vval.v_float = floor(f);
12135 else
12136 rettv->vval.v_float = 0.0;
12137}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012138
12139/*
12140 * "fmod()" function
12141 */
12142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012143f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012144{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012145 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012146
12147 rettv->v_type = VAR_FLOAT;
12148 if (get_float_arg(argvars, &fx) == OK
12149 && get_float_arg(&argvars[1], &fy) == OK)
12150 rettv->vval.v_float = fmod(fx, fy);
12151 else
12152 rettv->vval.v_float = 0.0;
12153}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012154#endif
12155
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012157 * "fnameescape({string})" function
12158 */
12159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012161{
12162 rettv->vval.v_string = vim_strsave_fnameescape(
12163 get_tv_string(&argvars[0]), FALSE);
12164 rettv->v_type = VAR_STRING;
12165}
12166
12167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 * "fnamemodify({fname}, {mods})" function
12169 */
12170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
12173 char_u *fname;
12174 char_u *mods;
12175 int usedlen = 0;
12176 int len;
12177 char_u *fbuf = NULL;
12178 char_u buf[NUMBUFLEN];
12179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 fname = get_tv_string_chk(&argvars[0]);
12181 mods = get_tv_string_buf_chk(&argvars[1], buf);
12182 if (fname == NULL || mods == NULL)
12183 fname = NULL;
12184 else
12185 {
12186 len = (int)STRLEN(fname);
12187 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 vim_free(fbuf);
12196}
12197
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012198static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199
12200/*
12201 * "foldclosed()" function
12202 */
12203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012204foldclosed_both(
12205 typval_T *argvars UNUSED,
12206 typval_T *rettv,
12207 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208{
12209#ifdef FEAT_FOLDING
12210 linenr_T lnum;
12211 linenr_T first, last;
12212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012213 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12215 {
12216 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12217 {
12218 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222 return;
12223 }
12224 }
12225#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227}
12228
12229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012230 * "foldclosed()" function
12231 */
12232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012233f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012234{
12235 foldclosed_both(argvars, rettv, FALSE);
12236}
12237
12238/*
12239 * "foldclosedend()" function
12240 */
12241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012242f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012243{
12244 foldclosed_both(argvars, rettv, TRUE);
12245}
12246
12247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 * "foldlevel()" function
12249 */
12250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252{
12253#ifdef FEAT_FOLDING
12254 linenr_T lnum;
12255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260}
12261
12262/*
12263 * "foldtext()" function
12264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267{
12268#ifdef FEAT_FOLDING
12269 linenr_T lnum;
12270 char_u *s;
12271 char_u *r;
12272 int len;
12273 char *txt;
12274#endif
12275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012276 rettv->v_type = VAR_STRING;
12277 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012279 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12280 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12281 <= curbuf->b_ml.ml_line_count
12282 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 {
12284 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12286 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 {
12288 if (!linewhite(lnum))
12289 break;
12290 ++lnum;
12291 }
12292
12293 /* Find interesting text in this line. */
12294 s = skipwhite(ml_get(lnum));
12295 /* skip C comment-start */
12296 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012299 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012300 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012301 {
12302 s = skipwhite(ml_get(lnum + 1));
12303 if (*s == '*')
12304 s = skipwhite(s + 1);
12305 }
12306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 txt = _("+-%s%3ld lines: ");
12308 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012309 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 + 20 /* for %3ld */
12311 + STRLEN(s))); /* concatenated */
12312 if (r != NULL)
12313 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012314 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12315 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12316 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 len = (int)STRLEN(r);
12318 STRCAT(r, s);
12319 /* remove 'foldmarker' and 'commentstring' */
12320 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 }
12323 }
12324#endif
12325}
12326
12327/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012328 * "foldtextresult(lnum)" function
12329 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012331f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012332{
12333#ifdef FEAT_FOLDING
12334 linenr_T lnum;
12335 char_u *text;
12336 char_u buf[51];
12337 foldinfo_T foldinfo;
12338 int fold_count;
12339#endif
12340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->v_type = VAR_STRING;
12342 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012343#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012345 /* treat illegal types and illegal string values for {lnum} the same */
12346 if (lnum < 0)
12347 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012348 fold_count = foldedCount(curwin, lnum, &foldinfo);
12349 if (fold_count > 0)
12350 {
12351 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12352 &foldinfo, buf);
12353 if (text == buf)
12354 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012356 }
12357#endif
12358}
12359
12360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 * "foreground()" function
12362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#ifdef FEAT_GUI
12367 if (gui.in_use)
12368 gui_mch_set_foreground();
12369#else
12370# ifdef WIN32
12371 win32_set_foreground();
12372# endif
12373#endif
12374}
12375
12376/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012377 * "function()" function
12378 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012381{
12382 char_u *s;
12383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012385 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012386 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012387 /* Don't check an autoload name for existence here. */
12388 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012389 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012390 else
12391 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012392 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012393 {
12394 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012395 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012396
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012397 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12398 * also be called from another script. Using trans_function_name()
12399 * would also work, but some plugins depend on the name being
12400 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012401 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012402 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012403 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012404 if (rettv->vval.v_string != NULL)
12405 {
12406 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012407 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012408 }
12409 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012410 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012411 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012413 }
12414}
12415
12416/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012417 * "garbagecollect()" function
12418 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012420f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012421{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012422 /* This is postponed until we are back at the toplevel, because we may be
12423 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12424 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012425
12426 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12427 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012428}
12429
12430/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431 * "get()" function
12432 */
12433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012434f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435{
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 listitem_T *li;
12437 list_T *l;
12438 dictitem_T *di;
12439 dict_T *d;
12440 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012441
Bram Moolenaare9a41262005-01-15 22:18:47 +000012442 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012444 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 int error = FALSE;
12447
12448 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12449 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012450 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012453 else if (argvars[0].v_type == VAR_DICT)
12454 {
12455 if ((d = argvars[0].vval.v_dict) != NULL)
12456 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012457 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012458 if (di != NULL)
12459 tv = &di->di_tv;
12460 }
12461 }
12462 else
12463 EMSG2(_(e_listdictarg), "get()");
12464
12465 if (tv == NULL)
12466 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012467 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012468 copy_tv(&argvars[2], rettv);
12469 }
12470 else
12471 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012472}
12473
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012474static 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 +000012475
12476/*
12477 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012478 * Return a range (from start to end) of lines in rettv from the specified
12479 * buffer.
12480 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012481 */
12482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483get_buffer_lines(
12484 buf_T *buf,
12485 linenr_T start,
12486 linenr_T end,
12487 int retlist,
12488 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012489{
12490 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491
Bram Moolenaar959a1432013-12-14 12:17:38 +010012492 rettv->v_type = VAR_STRING;
12493 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012494 if (retlist && rettv_list_alloc(rettv) == FAIL)
12495 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012496
12497 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12498 return;
12499
12500 if (!retlist)
12501 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012502 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12503 p = ml_get_buf(buf, start, FALSE);
12504 else
12505 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012506 rettv->vval.v_string = vim_strsave(p);
12507 }
12508 else
12509 {
12510 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012511 return;
12512
12513 if (start < 1)
12514 start = 1;
12515 if (end > buf->b_ml.ml_line_count)
12516 end = buf->b_ml.ml_line_count;
12517 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012518 if (list_append_string(rettv->vval.v_list,
12519 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012520 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012521 }
12522}
12523
12524/*
12525 * "getbufline()" function
12526 */
12527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012528f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012529{
12530 linenr_T lnum;
12531 linenr_T end;
12532 buf_T *buf;
12533
12534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12535 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012536 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012537 --emsg_off;
12538
Bram Moolenaar661b1822005-07-28 22:36:45 +000012539 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012540 if (argvars[2].v_type == VAR_UNKNOWN)
12541 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012542 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012543 end = get_tv_lnum_buf(&argvars[2], buf);
12544
Bram Moolenaar342337a2005-07-21 21:11:17 +000012545 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012546}
12547
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548/*
12549 * "getbufvar()" function
12550 */
12551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012552f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553{
12554 buf_T *buf;
12555 buf_T *save_curbuf;
12556 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012558 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012560 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12561 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012563 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012565 rettv->v_type = VAR_STRING;
12566 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567
12568 if (buf != NULL && varname != NULL)
12569 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012570 /* set curbuf to be our buf, temporarily */
12571 save_curbuf = curbuf;
12572 curbuf = buf;
12573
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012575 {
12576 if (get_option_tv(&varname, rettv, TRUE) == OK)
12577 done = TRUE;
12578 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012579 else if (STRCMP(varname, "changedtick") == 0)
12580 {
12581 rettv->v_type = VAR_NUMBER;
12582 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012583 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012584 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 else
12586 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012587 /* Look up the variable. */
12588 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12589 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12590 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012591 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012593 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012594 done = TRUE;
12595 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012596 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012597
12598 /* restore previous notion of curbuf */
12599 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 }
12601
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012602 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12603 /* use the default value */
12604 copy_tv(&argvars[2], rettv);
12605
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606 --emsg_off;
12607}
12608
12609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 * "getchar()" function
12611 */
12612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012613f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614{
12615 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012618 /* Position the cursor. Needed after a message that ends in a space. */
12619 windgoto(msg_row, msg_col);
12620
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 ++no_mapping;
12622 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012623 for (;;)
12624 {
12625 if (argvars[0].v_type == VAR_UNKNOWN)
12626 /* getchar(): blocking wait. */
12627 n = safe_vgetc();
12628 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12629 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012630 n = vpeekc_any();
12631 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012632 /* illegal argument or getchar(0) and no char avail: return zero */
12633 n = 0;
12634 else
12635 /* getchar(0) and char avail: return char */
12636 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012637
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012638 if (n == K_IGNORE)
12639 continue;
12640 break;
12641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642 --no_mapping;
12643 --allow_keys;
12644
Bram Moolenaar219b8702006-11-01 14:32:36 +000012645 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12646 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12647 vimvars[VV_MOUSE_COL].vv_nr = 0;
12648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 if (IS_SPECIAL(n) || mod_mask != 0)
12651 {
12652 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12653 int i = 0;
12654
12655 /* Turn a special key into three bytes, plus modifier. */
12656 if (mod_mask != 0)
12657 {
12658 temp[i++] = K_SPECIAL;
12659 temp[i++] = KS_MODIFIER;
12660 temp[i++] = mod_mask;
12661 }
12662 if (IS_SPECIAL(n))
12663 {
12664 temp[i++] = K_SPECIAL;
12665 temp[i++] = K_SECOND(n);
12666 temp[i++] = K_THIRD(n);
12667 }
12668#ifdef FEAT_MBYTE
12669 else if (has_mbyte)
12670 i += (*mb_char2bytes)(n, temp + i);
12671#endif
12672 else
12673 temp[i++] = n;
12674 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->v_type = VAR_STRING;
12676 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012677
12678#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012679 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012680 {
12681 int row = mouse_row;
12682 int col = mouse_col;
12683 win_T *win;
12684 linenr_T lnum;
12685# ifdef FEAT_WINDOWS
12686 win_T *wp;
12687# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012688 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012689
12690 if (row >= 0 && col >= 0)
12691 {
12692 /* Find the window at the mouse coordinates and compute the
12693 * text position. */
12694 win = mouse_find_win(&row, &col);
12695 (void)mouse_comp_pos(win, &row, &col, &lnum);
12696# ifdef FEAT_WINDOWS
12697 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012698 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012699# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012700 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012701 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12702 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12703 }
12704 }
12705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 }
12707}
12708
12709/*
12710 * "getcharmod()" function
12711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012713f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012715 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716}
12717
12718/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012719 * "getcharsearch()" function
12720 */
12721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012722f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012723{
12724 if (rettv_dict_alloc(rettv) != FAIL)
12725 {
12726 dict_T *dict = rettv->vval.v_dict;
12727
12728 dict_add_nr_str(dict, "char", 0L, last_csearch());
12729 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12730 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12731 }
12732}
12733
12734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 * "getcmdline()" function
12736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012738f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->v_type = VAR_STRING;
12741 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742}
12743
12744/*
12745 * "getcmdpos()" function
12746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012748f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751}
12752
12753/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012754 * "getcmdtype()" function
12755 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012757f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012758{
12759 rettv->v_type = VAR_STRING;
12760 rettv->vval.v_string = alloc(2);
12761 if (rettv->vval.v_string != NULL)
12762 {
12763 rettv->vval.v_string[0] = get_cmdline_type();
12764 rettv->vval.v_string[1] = NUL;
12765 }
12766}
12767
12768/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012769 * "getcmdwintype()" function
12770 */
12771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012772f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012773{
12774 rettv->v_type = VAR_STRING;
12775 rettv->vval.v_string = NULL;
12776#ifdef FEAT_CMDWIN
12777 rettv->vval.v_string = alloc(2);
12778 if (rettv->vval.v_string != NULL)
12779 {
12780 rettv->vval.v_string[0] = cmdwin_type;
12781 rettv->vval.v_string[1] = NUL;
12782 }
12783#endif
12784}
12785
12786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 * "getcwd()" function
12788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012792 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012793 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012796 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012797
12798 wp = find_tabwin(&argvars[0], &argvars[1]);
12799 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012801 if (wp->w_localdir != NULL)
12802 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12803 else if(globaldir != NULL)
12804 rettv->vval.v_string = vim_strsave(globaldir);
12805 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012806 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012807 cwd = alloc(MAXPATHL);
12808 if (cwd != NULL)
12809 {
12810 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12811 rettv->vval.v_string = vim_strsave(cwd);
12812 vim_free(cwd);
12813 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012814 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012815#ifdef BACKSLASH_IN_FILENAME
12816 if (rettv->vval.v_string != NULL)
12817 slash_adjust(rettv->vval.v_string);
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820}
12821
12822/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012823 * "getfontname()" function
12824 */
12825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012826f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->v_type = VAR_STRING;
12829 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012830#ifdef FEAT_GUI
12831 if (gui.in_use)
12832 {
12833 GuiFont font;
12834 char_u *name = NULL;
12835
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012836 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012837 {
12838 /* Get the "Normal" font. Either the name saved by
12839 * hl_set_font_name() or from the font ID. */
12840 font = gui.norm_font;
12841 name = hl_get_font_name();
12842 }
12843 else
12844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012846 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12847 return;
12848 font = gui_mch_get_font(name, FALSE);
12849 if (font == NOFONT)
12850 return; /* Invalid font name, return empty string. */
12851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012853 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012854 gui_mch_free_font(font);
12855 }
12856#endif
12857}
12858
12859/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012860 * "getfperm({fname})" function
12861 */
12862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012863f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012864{
12865 char_u *fname;
12866 struct stat st;
12867 char_u *perm = NULL;
12868 char_u flags[] = "rwx";
12869 int i;
12870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012874 if (mch_stat((char *)fname, &st) >= 0)
12875 {
12876 perm = vim_strsave((char_u *)"---------");
12877 if (perm != NULL)
12878 {
12879 for (i = 0; i < 9; i++)
12880 {
12881 if (st.st_mode & (1 << (8 - i)))
12882 perm[i] = flags[i % 3];
12883 }
12884 }
12885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887}
12888
12889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 * "getfsize({fname})" function
12891 */
12892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012893f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894{
12895 char_u *fname;
12896 struct stat st;
12897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901
12902 if (mch_stat((char *)fname, &st) >= 0)
12903 {
12904 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012909
12910 /* non-perfect check for overflow */
12911 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12912 rettv->vval.v_number = -2;
12913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 }
12915 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917}
12918
12919/*
12920 * "getftime({fname})" function
12921 */
12922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012923f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924{
12925 char_u *fname;
12926 struct stat st;
12927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929
12930 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934}
12935
12936/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012937 * "getftype({fname})" function
12938 */
12939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012940f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012941{
12942 char_u *fname;
12943 struct stat st;
12944 char_u *type = NULL;
12945 char *t;
12946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012950 if (mch_lstat((char *)fname, &st) >= 0)
12951 {
12952#ifdef S_ISREG
12953 if (S_ISREG(st.st_mode))
12954 t = "file";
12955 else if (S_ISDIR(st.st_mode))
12956 t = "dir";
12957# ifdef S_ISLNK
12958 else if (S_ISLNK(st.st_mode))
12959 t = "link";
12960# endif
12961# ifdef S_ISBLK
12962 else if (S_ISBLK(st.st_mode))
12963 t = "bdev";
12964# endif
12965# ifdef S_ISCHR
12966 else if (S_ISCHR(st.st_mode))
12967 t = "cdev";
12968# endif
12969# ifdef S_ISFIFO
12970 else if (S_ISFIFO(st.st_mode))
12971 t = "fifo";
12972# endif
12973# ifdef S_ISSOCK
12974 else if (S_ISSOCK(st.st_mode))
12975 t = "fifo";
12976# endif
12977 else
12978 t = "other";
12979#else
12980# ifdef S_IFMT
12981 switch (st.st_mode & S_IFMT)
12982 {
12983 case S_IFREG: t = "file"; break;
12984 case S_IFDIR: t = "dir"; break;
12985# ifdef S_IFLNK
12986 case S_IFLNK: t = "link"; break;
12987# endif
12988# ifdef S_IFBLK
12989 case S_IFBLK: t = "bdev"; break;
12990# endif
12991# ifdef S_IFCHR
12992 case S_IFCHR: t = "cdev"; break;
12993# endif
12994# ifdef S_IFIFO
12995 case S_IFIFO: t = "fifo"; break;
12996# endif
12997# ifdef S_IFSOCK
12998 case S_IFSOCK: t = "socket"; break;
12999# endif
13000 default: t = "other";
13001 }
13002# else
13003 if (mch_isdir(fname))
13004 t = "dir";
13005 else
13006 t = "file";
13007# endif
13008#endif
13009 type = vim_strsave((char_u *)t);
13010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013011 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013012}
13013
13014/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013015 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013016 */
13017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013018f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019{
13020 linenr_T lnum;
13021 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013022 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013023
13024 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013025 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013026 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013027 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013028 retlist = FALSE;
13029 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013030 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013031 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013033 retlist = TRUE;
13034 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013035
Bram Moolenaar342337a2005-07-21 21:11:17 +000013036 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037}
13038
13039/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013040 * "getmatches()" function
13041 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013043f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013044{
13045#ifdef FEAT_SEARCH_EXTRA
13046 dict_T *dict;
13047 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013048 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013049
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013050 if (rettv_list_alloc(rettv) == OK)
13051 {
13052 while (cur != NULL)
13053 {
13054 dict = dict_alloc();
13055 if (dict == NULL)
13056 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013057 if (cur->match.regprog == NULL)
13058 {
13059 /* match added with matchaddpos() */
13060 for (i = 0; i < MAXPOSMATCH; ++i)
13061 {
13062 llpos_T *llpos;
13063 char buf[6];
13064 list_T *l;
13065
13066 llpos = &cur->pos.pos[i];
13067 if (llpos->lnum == 0)
13068 break;
13069 l = list_alloc();
13070 if (l == NULL)
13071 break;
13072 list_append_number(l, (varnumber_T)llpos->lnum);
13073 if (llpos->col > 0)
13074 {
13075 list_append_number(l, (varnumber_T)llpos->col);
13076 list_append_number(l, (varnumber_T)llpos->len);
13077 }
13078 sprintf(buf, "pos%d", i + 1);
13079 dict_add_list(dict, buf, l);
13080 }
13081 }
13082 else
13083 {
13084 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13085 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013086 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013087 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13088 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013089# ifdef FEAT_CONCEAL
13090 if (cur->conceal_char)
13091 {
13092 char_u buf[MB_MAXBYTES + 1];
13093
13094 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13095 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13096 }
13097# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013098 list_append_dict(rettv->vval.v_list, dict);
13099 cur = cur->next;
13100 }
13101 }
13102#endif
13103}
13104
13105/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013106 * "getpid()" function
13107 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013109f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013110{
13111 rettv->vval.v_number = mch_get_pid();
13112}
13113
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013114static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013115
13116/*
13117 * "getcurpos()" function
13118 */
13119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013120f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013121{
13122 getpos_both(argvars, rettv, TRUE);
13123}
13124
Bram Moolenaar18081e32008-02-20 19:11:07 +000013125/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013126 * "getpos(string)" function
13127 */
13128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013129f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013130{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013131 getpos_both(argvars, rettv, FALSE);
13132}
13133
13134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013135getpos_both(
13136 typval_T *argvars,
13137 typval_T *rettv,
13138 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013139{
Bram Moolenaara5525202006-03-02 22:52:09 +000013140 pos_T *fp;
13141 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013142 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013143
13144 if (rettv_list_alloc(rettv) == OK)
13145 {
13146 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013147 if (getcurpos)
13148 fp = &curwin->w_cursor;
13149 else
13150 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013151 if (fnum != -1)
13152 list_append_number(l, (varnumber_T)fnum);
13153 else
13154 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013155 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13156 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013157 list_append_number(l, (fp != NULL)
13158 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013159 : (varnumber_T)0);
13160 list_append_number(l,
13161#ifdef FEAT_VIRTUALEDIT
13162 (fp != NULL) ? (varnumber_T)fp->coladd :
13163#endif
13164 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013165 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013166 {
13167 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013168 list_append_number(l, curwin->w_curswant == MAXCOL ?
13169 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013170 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013171 }
13172 else
13173 rettv->vval.v_number = FALSE;
13174}
13175
13176/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013177 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013178 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013180f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013181{
13182#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013183 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013184#endif
13185
Bram Moolenaar2641f772005-03-25 21:58:17 +000013186#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013187 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013188 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013189 wp = NULL;
13190 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13191 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013192 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013193 if (wp == NULL)
13194 return;
13195 }
13196
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013197 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013198 }
13199#endif
13200}
13201
13202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 * "getreg()" function
13204 */
13205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013206f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207{
13208 char_u *strregname;
13209 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013210 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013211 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013214 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 strregname = get_tv_string_chk(&argvars[0]);
13217 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013221 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13222 return_list = get_tv_number_chk(&argvars[2], &error);
13223 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013226 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013227
13228 if (error)
13229 return;
13230
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 regname = (strregname == NULL ? '"' : *strregname);
13232 if (regname == 0)
13233 regname = '"';
13234
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013235 if (return_list)
13236 {
13237 rettv->v_type = VAR_LIST;
13238 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13239 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013240 if (rettv->vval.v_list != NULL)
13241 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013242 }
13243 else
13244 {
13245 rettv->v_type = VAR_STRING;
13246 rettv->vval.v_string = get_reg_contents(regname,
13247 arg2 ? GREG_EXPR_SRC : 0);
13248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249}
13250
13251/*
13252 * "getregtype()" function
13253 */
13254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013255f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256{
13257 char_u *strregname;
13258 int regname;
13259 char_u buf[NUMBUFLEN + 2];
13260 long reglen = 0;
13261
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 {
13264 strregname = get_tv_string_chk(&argvars[0]);
13265 if (strregname == NULL) /* type error; errmsg already given */
13266 {
13267 rettv->v_type = VAR_STRING;
13268 rettv->vval.v_string = NULL;
13269 return;
13270 }
13271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 else
13273 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013274 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275
13276 regname = (strregname == NULL ? '"' : *strregname);
13277 if (regname == 0)
13278 regname = '"';
13279
13280 buf[0] = NUL;
13281 buf[1] = NUL;
13282 switch (get_reg_type(regname, &reglen))
13283 {
13284 case MLINE: buf[0] = 'V'; break;
13285 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 case MBLOCK:
13287 buf[0] = Ctrl_V;
13288 sprintf((char *)buf + 1, "%ld", reglen + 1);
13289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 rettv->v_type = VAR_STRING;
13292 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293}
13294
13295/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013296 * "gettabvar()" function
13297 */
13298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013299f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013300{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013301 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013302 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013303 dictitem_T *v;
13304 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013305 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013306
13307 rettv->v_type = VAR_STRING;
13308 rettv->vval.v_string = NULL;
13309
13310 varname = get_tv_string_chk(&argvars[1]);
13311 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13312 if (tp != NULL && varname != NULL)
13313 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013314 /* Set tp to be our tabpage, temporarily. Also set the window to the
13315 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013316 if (switch_win(&oldcurwin, &oldtabpage,
13317 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013318 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013320 /* look up the variable */
13321 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13322 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13323 if (v != NULL)
13324 {
13325 copy_tv(&v->di_tv, rettv);
13326 done = TRUE;
13327 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013328 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013329
13330 /* restore previous notion of curwin */
13331 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013332 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013333
13334 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013335 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013336}
13337
13338/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013339 * "gettabwinvar()" function
13340 */
13341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013342f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013343{
13344 getwinvar(argvars, rettv, 1);
13345}
13346
13347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 * "getwinposx()" function
13349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013351f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013353 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354#ifdef FEAT_GUI
13355 if (gui.in_use)
13356 {
13357 int x, y;
13358
13359 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013360 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 }
13362#endif
13363}
13364
13365/*
13366 * "getwinposy()" function
13367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013369f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef FEAT_GUI
13373 if (gui.in_use)
13374 {
13375 int x, y;
13376
13377 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 }
13380#endif
13381}
13382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013383/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013384 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013385 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013386 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013387find_win_by_nr(
13388 typval_T *vp,
13389 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013390{
13391#ifdef FEAT_WINDOWS
13392 win_T *wp;
13393#endif
13394 int nr;
13395
13396 nr = get_tv_number_chk(vp, NULL);
13397
13398#ifdef FEAT_WINDOWS
13399 if (nr < 0)
13400 return NULL;
13401 if (nr == 0)
13402 return curwin;
13403
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013404 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13405 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013406 if (--nr <= 0)
13407 break;
13408 return wp;
13409#else
13410 if (nr == 0 || nr == 1)
13411 return curwin;
13412 return NULL;
13413#endif
13414}
13415
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013417 * Find window specified by "wvp" in tabpage "tvp".
13418 */
13419 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013420find_tabwin(
13421 typval_T *wvp, /* VAR_UNKNOWN for current window */
13422 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013423{
13424 win_T *wp = NULL;
13425 tabpage_T *tp = NULL;
13426 long n;
13427
13428 if (wvp->v_type != VAR_UNKNOWN)
13429 {
13430 if (tvp->v_type != VAR_UNKNOWN)
13431 {
13432 n = get_tv_number(tvp);
13433 if (n >= 0)
13434 tp = find_tabpage(n);
13435 }
13436 else
13437 tp = curtab;
13438
13439 if (tp != NULL)
13440 wp = find_win_by_nr(wvp, tp);
13441 }
13442 else
13443 wp = curwin;
13444
13445 return wp;
13446}
13447
13448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 * "getwinvar()" function
13450 */
13451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013452f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013454 getwinvar(argvars, rettv, 0);
13455}
13456
13457/*
13458 * getwinvar() and gettabwinvar()
13459 */
13460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013461getwinvar(
13462 typval_T *argvars,
13463 typval_T *rettv,
13464 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013465{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013466 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013469 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013470 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013471#ifdef FEAT_WINDOWS
13472 win_T *oldcurwin;
13473 tabpage_T *oldtabpage;
13474 int need_switch_win;
13475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013477#ifdef FEAT_WINDOWS
13478 if (off == 1)
13479 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13480 else
13481 tp = curtab;
13482#endif
13483 win = find_win_by_nr(&argvars[off], tp);
13484 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013485 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013487 rettv->v_type = VAR_STRING;
13488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489
13490 if (win != NULL && varname != NULL)
13491 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013492#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013493 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013494 * otherwise the window is not valid. Only do this when needed,
13495 * autocommands get blocked. */
13496 need_switch_win = !(tp == curtab && win == curwin);
13497 if (!need_switch_win
13498 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13499#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013500 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013501 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013502 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013503 if (get_option_tv(&varname, rettv, 1) == OK)
13504 done = TRUE;
13505 }
13506 else
13507 {
13508 /* Look up the variable. */
13509 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13510 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13511 varname, FALSE);
13512 if (v != NULL)
13513 {
13514 copy_tv(&v->di_tv, rettv);
13515 done = TRUE;
13516 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013519
Bram Moolenaarba117c22015-09-29 16:53:22 +020013520#ifdef FEAT_WINDOWS
13521 if (need_switch_win)
13522 /* restore previous notion of curwin */
13523 restore_win(oldcurwin, oldtabpage, TRUE);
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 }
13526
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013527 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13528 /* use the default return value */
13529 copy_tv(&argvars[off + 2], rettv);
13530
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 --emsg_off;
13532}
13533
13534/*
13535 * "glob()" function
13536 */
13537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013538f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013540 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013542 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013544 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013545 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013547 if (argvars[1].v_type != VAR_UNKNOWN)
13548 {
13549 if (get_tv_number_chk(&argvars[1], &error))
13550 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013551 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013552 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013553 if (get_tv_number_chk(&argvars[2], &error))
13554 {
13555 rettv->v_type = VAR_LIST;
13556 rettv->vval.v_list = NULL;
13557 }
13558 if (argvars[3].v_type != VAR_UNKNOWN
13559 && get_tv_number_chk(&argvars[3], &error))
13560 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013561 }
13562 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013563 if (!error)
13564 {
13565 ExpandInit(&xpc);
13566 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013567 if (p_wic)
13568 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013569 if (rettv->v_type == VAR_STRING)
13570 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013571 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013572 else if (rettv_list_alloc(rettv) != FAIL)
13573 {
13574 int i;
13575
13576 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13577 NULL, options, WILD_ALL_KEEP);
13578 for (i = 0; i < xpc.xp_numfiles; i++)
13579 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13580
13581 ExpandCleanup(&xpc);
13582 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013583 }
13584 else
13585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
13589 * "globpath()" function
13590 */
13591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013592f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013594 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013597 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013598 garray_T ga;
13599 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013601 /* When the optional second argument is non-zero, don't remove matches
13602 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013604 if (argvars[2].v_type != VAR_UNKNOWN)
13605 {
13606 if (get_tv_number_chk(&argvars[2], &error))
13607 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013608 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013609 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013610 if (get_tv_number_chk(&argvars[3], &error))
13611 {
13612 rettv->v_type = VAR_LIST;
13613 rettv->vval.v_list = NULL;
13614 }
13615 if (argvars[4].v_type != VAR_UNKNOWN
13616 && get_tv_number_chk(&argvars[4], &error))
13617 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013618 }
13619 }
13620 if (file != NULL && !error)
13621 {
13622 ga_init2(&ga, (int)sizeof(char_u *), 10);
13623 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13624 if (rettv->v_type == VAR_STRING)
13625 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13626 else if (rettv_list_alloc(rettv) != FAIL)
13627 for (i = 0; i < ga.ga_len; ++i)
13628 list_append_string(rettv->vval.v_list,
13629 ((char_u **)(ga.ga_data))[i], -1);
13630 ga_clear_strings(&ga);
13631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013633 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013637 * "glob2regpat()" function
13638 */
13639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013640f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013641{
13642 char_u *pat = get_tv_string_chk(&argvars[0]);
13643
13644 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013645 rettv->vval.v_string = (pat == NULL)
13646 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013647}
13648
13649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 * "has()" function
13651 */
13652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013653f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654{
13655 int i;
13656 char_u *name;
13657 int n = FALSE;
13658 static char *(has_list[]) =
13659 {
13660#ifdef AMIGA
13661 "amiga",
13662# ifdef FEAT_ARP
13663 "arp",
13664# endif
13665#endif
13666#ifdef __BEOS__
13667 "beos",
13668#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013669#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 "mac",
13671#endif
13672#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013673 "macunix", /* built with 'darwin' enabled */
13674#endif
13675#if defined(__APPLE__) && __APPLE__ == 1
13676 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678#ifdef __QNX__
13679 "qnx",
13680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681#ifdef UNIX
13682 "unix",
13683#endif
13684#ifdef VMS
13685 "vms",
13686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687#ifdef WIN32
13688 "win32",
13689#endif
13690#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13691 "win32unix",
13692#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013693#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 "win64",
13695#endif
13696#ifdef EBCDIC
13697 "ebcdic",
13698#endif
13699#ifndef CASE_INSENSITIVE_FILENAME
13700 "fname_case",
13701#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013702#ifdef HAVE_ACL
13703 "acl",
13704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705#ifdef FEAT_ARABIC
13706 "arabic",
13707#endif
13708#ifdef FEAT_AUTOCMD
13709 "autocmd",
13710#endif
13711#ifdef FEAT_BEVAL
13712 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013713# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13714 "balloon_multiline",
13715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#endif
13717#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13718 "builtin_terms",
13719# ifdef ALL_BUILTIN_TCAPS
13720 "all_builtin_terms",
13721# endif
13722#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013723#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13724 || defined(FEAT_GUI_W32) \
13725 || defined(FEAT_GUI_MOTIF))
13726 "browsefilter",
13727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728#ifdef FEAT_BYTEOFF
13729 "byte_offset",
13730#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013731#ifdef FEAT_CHANNEL
13732 "channel",
13733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734#ifdef FEAT_CINDENT
13735 "cindent",
13736#endif
13737#ifdef FEAT_CLIENTSERVER
13738 "clientserver",
13739#endif
13740#ifdef FEAT_CLIPBOARD
13741 "clipboard",
13742#endif
13743#ifdef FEAT_CMDL_COMPL
13744 "cmdline_compl",
13745#endif
13746#ifdef FEAT_CMDHIST
13747 "cmdline_hist",
13748#endif
13749#ifdef FEAT_COMMENTS
13750 "comments",
13751#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013752#ifdef FEAT_CONCEAL
13753 "conceal",
13754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef FEAT_CRYPT
13756 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013757 "crypt-blowfish",
13758 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759#endif
13760#ifdef FEAT_CSCOPE
13761 "cscope",
13762#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013763#ifdef FEAT_CURSORBIND
13764 "cursorbind",
13765#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013766#ifdef CURSOR_SHAPE
13767 "cursorshape",
13768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769#ifdef DEBUG
13770 "debug",
13771#endif
13772#ifdef FEAT_CON_DIALOG
13773 "dialog_con",
13774#endif
13775#ifdef FEAT_GUI_DIALOG
13776 "dialog_gui",
13777#endif
13778#ifdef FEAT_DIFF
13779 "diff",
13780#endif
13781#ifdef FEAT_DIGRAPHS
13782 "digraphs",
13783#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013784#ifdef FEAT_DIRECTX
13785 "directx",
13786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#ifdef FEAT_DND
13788 "dnd",
13789#endif
13790#ifdef FEAT_EMACS_TAGS
13791 "emacs_tags",
13792#endif
13793 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013794 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795#ifdef FEAT_SEARCH_EXTRA
13796 "extra_search",
13797#endif
13798#ifdef FEAT_FKMAP
13799 "farsi",
13800#endif
13801#ifdef FEAT_SEARCHPATH
13802 "file_in_path",
13803#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013804#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013805 "filterpipe",
13806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807#ifdef FEAT_FIND_ID
13808 "find_in_path",
13809#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013810#ifdef FEAT_FLOAT
13811 "float",
13812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813#ifdef FEAT_FOLDING
13814 "folding",
13815#endif
13816#ifdef FEAT_FOOTER
13817 "footer",
13818#endif
13819#if !defined(USE_SYSTEM) && defined(UNIX)
13820 "fork",
13821#endif
13822#ifdef FEAT_GETTEXT
13823 "gettext",
13824#endif
13825#ifdef FEAT_GUI
13826 "gui",
13827#endif
13828#ifdef FEAT_GUI_ATHENA
13829# ifdef FEAT_GUI_NEXTAW
13830 "gui_neXtaw",
13831# else
13832 "gui_athena",
13833# endif
13834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835#ifdef FEAT_GUI_GTK
13836 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013837# ifdef USE_GTK3
13838 "gui_gtk3",
13839# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013843#ifdef FEAT_GUI_GNOME
13844 "gui_gnome",
13845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846#ifdef FEAT_GUI_MAC
13847 "gui_mac",
13848#endif
13849#ifdef FEAT_GUI_MOTIF
13850 "gui_motif",
13851#endif
13852#ifdef FEAT_GUI_PHOTON
13853 "gui_photon",
13854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855#ifdef FEAT_GUI_W32
13856 "gui_win32",
13857#endif
13858#ifdef FEAT_HANGULIN
13859 "hangul_input",
13860#endif
13861#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13862 "iconv",
13863#endif
13864#ifdef FEAT_INS_EXPAND
13865 "insert_expand",
13866#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013867#ifdef FEAT_JOB
13868 "job",
13869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870#ifdef FEAT_JUMPLIST
13871 "jumplist",
13872#endif
13873#ifdef FEAT_KEYMAP
13874 "keymap",
13875#endif
13876#ifdef FEAT_LANGMAP
13877 "langmap",
13878#endif
13879#ifdef FEAT_LIBCALL
13880 "libcall",
13881#endif
13882#ifdef FEAT_LINEBREAK
13883 "linebreak",
13884#endif
13885#ifdef FEAT_LISP
13886 "lispindent",
13887#endif
13888#ifdef FEAT_LISTCMDS
13889 "listcmds",
13890#endif
13891#ifdef FEAT_LOCALMAP
13892 "localmap",
13893#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013894#ifdef FEAT_LUA
13895# ifndef DYNAMIC_LUA
13896 "lua",
13897# endif
13898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899#ifdef FEAT_MENU
13900 "menu",
13901#endif
13902#ifdef FEAT_SESSION
13903 "mksession",
13904#endif
13905#ifdef FEAT_MODIFY_FNAME
13906 "modify_fname",
13907#endif
13908#ifdef FEAT_MOUSE
13909 "mouse",
13910#endif
13911#ifdef FEAT_MOUSESHAPE
13912 "mouseshape",
13913#endif
13914#if defined(UNIX) || defined(VMS)
13915# ifdef FEAT_MOUSE_DEC
13916 "mouse_dec",
13917# endif
13918# ifdef FEAT_MOUSE_GPM
13919 "mouse_gpm",
13920# endif
13921# ifdef FEAT_MOUSE_JSB
13922 "mouse_jsbterm",
13923# endif
13924# ifdef FEAT_MOUSE_NET
13925 "mouse_netterm",
13926# endif
13927# ifdef FEAT_MOUSE_PTERM
13928 "mouse_pterm",
13929# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013930# ifdef FEAT_MOUSE_SGR
13931 "mouse_sgr",
13932# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013933# ifdef FEAT_SYSMOUSE
13934 "mouse_sysmouse",
13935# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013936# ifdef FEAT_MOUSE_URXVT
13937 "mouse_urxvt",
13938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939# ifdef FEAT_MOUSE_XTERM
13940 "mouse_xterm",
13941# endif
13942#endif
13943#ifdef FEAT_MBYTE
13944 "multi_byte",
13945#endif
13946#ifdef FEAT_MBYTE_IME
13947 "multi_byte_ime",
13948#endif
13949#ifdef FEAT_MULTI_LANG
13950 "multi_lang",
13951#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013952#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013953#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013954 "mzscheme",
13955#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#ifdef FEAT_OLE
13958 "ole",
13959#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013960 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961#ifdef FEAT_PATH_EXTRA
13962 "path_extra",
13963#endif
13964#ifdef FEAT_PERL
13965#ifndef DYNAMIC_PERL
13966 "perl",
13967#endif
13968#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013969#ifdef FEAT_PERSISTENT_UNDO
13970 "persistent_undo",
13971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972#ifdef FEAT_PYTHON
13973#ifndef DYNAMIC_PYTHON
13974 "python",
13975#endif
13976#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013977#ifdef FEAT_PYTHON3
13978#ifndef DYNAMIC_PYTHON3
13979 "python3",
13980#endif
13981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982#ifdef FEAT_POSTSCRIPT
13983 "postscript",
13984#endif
13985#ifdef FEAT_PRINTER
13986 "printer",
13987#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013988#ifdef FEAT_PROFILE
13989 "profile",
13990#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013991#ifdef FEAT_RELTIME
13992 "reltime",
13993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994#ifdef FEAT_QUICKFIX
13995 "quickfix",
13996#endif
13997#ifdef FEAT_RIGHTLEFT
13998 "rightleft",
13999#endif
14000#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14001 "ruby",
14002#endif
14003#ifdef FEAT_SCROLLBIND
14004 "scrollbind",
14005#endif
14006#ifdef FEAT_CMDL_INFO
14007 "showcmd",
14008 "cmdline_info",
14009#endif
14010#ifdef FEAT_SIGNS
14011 "signs",
14012#endif
14013#ifdef FEAT_SMARTINDENT
14014 "smartindent",
14015#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014016#ifdef STARTUPTIME
14017 "startuptime",
14018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019#ifdef FEAT_STL_OPT
14020 "statusline",
14021#endif
14022#ifdef FEAT_SUN_WORKSHOP
14023 "sun_workshop",
14024#endif
14025#ifdef FEAT_NETBEANS_INTG
14026 "netbeans_intg",
14027#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014028#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014029 "spell",
14030#endif
14031#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 "syntax",
14033#endif
14034#if defined(USE_SYSTEM) || !defined(UNIX)
14035 "system",
14036#endif
14037#ifdef FEAT_TAG_BINS
14038 "tag_binary",
14039#endif
14040#ifdef FEAT_TAG_OLDSTATIC
14041 "tag_old_static",
14042#endif
14043#ifdef FEAT_TAG_ANYWHITE
14044 "tag_any_white",
14045#endif
14046#ifdef FEAT_TCL
14047# ifndef DYNAMIC_TCL
14048 "tcl",
14049# endif
14050#endif
14051#ifdef TERMINFO
14052 "terminfo",
14053#endif
14054#ifdef FEAT_TERMRESPONSE
14055 "termresponse",
14056#endif
14057#ifdef FEAT_TEXTOBJ
14058 "textobjects",
14059#endif
14060#ifdef HAVE_TGETENT
14061 "tgetent",
14062#endif
14063#ifdef FEAT_TITLE
14064 "title",
14065#endif
14066#ifdef FEAT_TOOLBAR
14067 "toolbar",
14068#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014069#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14070 "unnamedplus",
14071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072#ifdef FEAT_USR_CMDS
14073 "user-commands", /* was accidentally included in 5.4 */
14074 "user_commands",
14075#endif
14076#ifdef FEAT_VIMINFO
14077 "viminfo",
14078#endif
14079#ifdef FEAT_VERTSPLIT
14080 "vertsplit",
14081#endif
14082#ifdef FEAT_VIRTUALEDIT
14083 "virtualedit",
14084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086#ifdef FEAT_VISUALEXTRA
14087 "visualextra",
14088#endif
14089#ifdef FEAT_VREPLACE
14090 "vreplace",
14091#endif
14092#ifdef FEAT_WILDIGN
14093 "wildignore",
14094#endif
14095#ifdef FEAT_WILDMENU
14096 "wildmenu",
14097#endif
14098#ifdef FEAT_WINDOWS
14099 "windows",
14100#endif
14101#ifdef FEAT_WAK
14102 "winaltkeys",
14103#endif
14104#ifdef FEAT_WRITEBACKUP
14105 "writebackup",
14106#endif
14107#ifdef FEAT_XIM
14108 "xim",
14109#endif
14110#ifdef FEAT_XFONTSET
14111 "xfontset",
14112#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014113#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014114 "xpm",
14115 "xpm_w32", /* for backward compatibility */
14116#else
14117# if defined(HAVE_XPM)
14118 "xpm",
14119# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#ifdef USE_XSMP
14122 "xsmp",
14123#endif
14124#ifdef USE_XSMP_INTERACT
14125 "xsmp_interact",
14126#endif
14127#ifdef FEAT_XCLIPBOARD
14128 "xterm_clipboard",
14129#endif
14130#ifdef FEAT_XTERM_SAVE
14131 "xterm_save",
14132#endif
14133#if defined(UNIX) && defined(FEAT_X11)
14134 "X11",
14135#endif
14136 NULL
14137 };
14138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 for (i = 0; has_list[i] != NULL; ++i)
14141 if (STRICMP(name, has_list[i]) == 0)
14142 {
14143 n = TRUE;
14144 break;
14145 }
14146
14147 if (n == FALSE)
14148 {
14149 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014150 {
14151 if (name[5] == '-'
14152 && STRLEN(name) > 11
14153 && vim_isdigit(name[6])
14154 && vim_isdigit(name[8])
14155 && vim_isdigit(name[10]))
14156 {
14157 int major = atoi((char *)name + 6);
14158 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014159
14160 /* Expect "patch-9.9.01234". */
14161 n = (major < VIM_VERSION_MAJOR
14162 || (major == VIM_VERSION_MAJOR
14163 && (minor < VIM_VERSION_MINOR
14164 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014165 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014166 }
14167 else
14168 n = has_patch(atoi((char *)name + 5));
14169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170 else if (STRICMP(name, "vim_starting") == 0)
14171 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014172#ifdef FEAT_MBYTE
14173 else if (STRICMP(name, "multi_byte_encoding") == 0)
14174 n = has_mbyte;
14175#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014176#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14177 else if (STRICMP(name, "balloon_multiline") == 0)
14178 n = multiline_balloon_available();
14179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180#ifdef DYNAMIC_TCL
14181 else if (STRICMP(name, "tcl") == 0)
14182 n = tcl_enabled(FALSE);
14183#endif
14184#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14185 else if (STRICMP(name, "iconv") == 0)
14186 n = iconv_enabled(FALSE);
14187#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014188#ifdef DYNAMIC_LUA
14189 else if (STRICMP(name, "lua") == 0)
14190 n = lua_enabled(FALSE);
14191#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014192#ifdef DYNAMIC_MZSCHEME
14193 else if (STRICMP(name, "mzscheme") == 0)
14194 n = mzscheme_enabled(FALSE);
14195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196#ifdef DYNAMIC_RUBY
14197 else if (STRICMP(name, "ruby") == 0)
14198 n = ruby_enabled(FALSE);
14199#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014200#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#ifdef DYNAMIC_PYTHON
14202 else if (STRICMP(name, "python") == 0)
14203 n = python_enabled(FALSE);
14204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014205#endif
14206#ifdef FEAT_PYTHON3
14207#ifdef DYNAMIC_PYTHON3
14208 else if (STRICMP(name, "python3") == 0)
14209 n = python3_enabled(FALSE);
14210#endif
14211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212#ifdef DYNAMIC_PERL
14213 else if (STRICMP(name, "perl") == 0)
14214 n = perl_enabled(FALSE);
14215#endif
14216#ifdef FEAT_GUI
14217 else if (STRICMP(name, "gui_running") == 0)
14218 n = (gui.in_use || gui.starting);
14219# ifdef FEAT_GUI_W32
14220 else if (STRICMP(name, "gui_win32s") == 0)
14221 n = gui_is_win32s();
14222# endif
14223# ifdef FEAT_BROWSE
14224 else if (STRICMP(name, "browse") == 0)
14225 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14226# endif
14227#endif
14228#ifdef FEAT_SYN_HL
14229 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014230 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231#endif
14232#if defined(WIN3264)
14233 else if (STRICMP(name, "win95") == 0)
14234 n = mch_windows95();
14235#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014236#ifdef FEAT_NETBEANS_INTG
14237 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014238 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 }
14241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
14245/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246 * "has_key()" function
14247 */
14248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014249f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014251 if (argvars[0].v_type != VAR_DICT)
14252 {
14253 EMSG(_(e_dictreq));
14254 return;
14255 }
14256 if (argvars[0].vval.v_dict == NULL)
14257 return;
14258
14259 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261}
14262
14263/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014264 * "haslocaldir()" function
14265 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014267f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014268{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014269 win_T *wp = NULL;
14270
14271 wp = find_tabwin(&argvars[0], &argvars[1]);
14272 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014273}
14274
14275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 * "hasmapto()" function
14277 */
14278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
14281 char_u *name;
14282 char_u *mode;
14283 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014284 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014287 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 mode = (char_u *)"nvo";
14289 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014292 if (argvars[2].v_type != VAR_UNKNOWN)
14293 abbr = get_tv_number(&argvars[2]);
14294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
Bram Moolenaar2c932302006-03-18 21:42:09 +000014296 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300}
14301
14302/*
14303 * "histadd()" function
14304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307{
14308#ifdef FEAT_CMDHIST
14309 int histype;
14310 char_u *str;
14311 char_u buf[NUMBUFLEN];
14312#endif
14313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014314 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 if (check_restricted() || check_secure())
14316 return;
14317#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14319 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 if (histype >= 0)
14321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 if (*str != NUL)
14324 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014325 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 return;
14329 }
14330 }
14331#endif
14332}
14333
14334/*
14335 * "histdel()" function
14336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339{
14340#ifdef FEAT_CMDHIST
14341 int n;
14342 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14346 if (str == NULL)
14347 n = 0;
14348 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 else
14356 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 get_tv_string_buf(&argvars[1], buf));
14359 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360#endif
14361}
14362
14363/*
14364 * "histget()" function
14365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014367f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368{
14369#ifdef FEAT_CMDHIST
14370 int type;
14371 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14375 if (str == NULL)
14376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 {
14379 type = get_histtype(str);
14380 if (argvars[1].v_type == VAR_UNKNOWN)
14381 idx = get_history_idx(type);
14382 else
14383 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14384 /* -1 on type error */
14385 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391}
14392
14393/*
14394 * "histnr()" function
14395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014397f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398{
14399 int i;
14400
14401#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402 char_u *history = get_tv_string_chk(&argvars[0]);
14403
14404 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 if (i >= HIST_CMD && i < HIST_COUNT)
14406 i = get_history_idx(i);
14407 else
14408#endif
14409 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411}
14412
14413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 * "highlightID(name)" function
14415 */
14416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014417f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420}
14421
14422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014423 * "highlight_exists()" function
14424 */
14425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014427{
14428 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14429}
14430
14431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 * "hostname()" function
14433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
14437 char_u hostname[256];
14438
14439 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 rettv->v_type = VAR_STRING;
14441 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442}
14443
14444/*
14445 * iconv() function
14446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014448f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
14450#ifdef FEAT_MBYTE
14451 char_u buf1[NUMBUFLEN];
14452 char_u buf2[NUMBUFLEN];
14453 char_u *from, *to, *str;
14454 vimconv_T vimconv;
14455#endif
14456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->v_type = VAR_STRING;
14458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459
14460#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 str = get_tv_string(&argvars[0]);
14462 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14463 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 vimconv.vc_type = CONV_NONE;
14465 convert_setup(&vimconv, from, to);
14466
14467 /* If the encodings are equal, no conversion needed. */
14468 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014471 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472
14473 convert_setup(&vimconv, NULL, NULL);
14474 vim_free(from);
14475 vim_free(to);
14476#endif
14477}
14478
14479/*
14480 * "indent()" function
14481 */
14482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 linenr_T lnum;
14486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492}
14493
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014494/*
14495 * "index()" function
14496 */
14497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014498f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014499{
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 list_T *l;
14501 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014502 long idx = 0;
14503 int ic = FALSE;
14504
14505 rettv->vval.v_number = -1;
14506 if (argvars[0].v_type != VAR_LIST)
14507 {
14508 EMSG(_(e_listreq));
14509 return;
14510 }
14511 l = argvars[0].vval.v_list;
14512 if (l != NULL)
14513 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014514 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 int error = FALSE;
14518
Bram Moolenaar758711c2005-02-02 23:11:38 +000014519 /* Start at specified item. Use the cached index that list_find()
14520 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014522 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 ic = get_tv_number_chk(&argvars[3], &error);
14525 if (error)
14526 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014528
Bram Moolenaar758711c2005-02-02 23:11:38 +000014529 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014530 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014531 {
14532 rettv->vval.v_number = idx;
14533 break;
14534 }
14535 }
14536}
14537
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538static int inputsecret_flag = 0;
14539
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014540static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014541
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014543 * This function is used by f_input() and f_inputdialog() functions. The third
14544 * argument to f_input() specifies the type of completion to use at the
14545 * prompt. The third argument to f_inputdialog() specifies the value to return
14546 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 */
14548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014549get_user_input(
14550 typval_T *argvars,
14551 typval_T *rettv,
14552 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 char_u *p = NULL;
14556 int c;
14557 char_u buf[NUMBUFLEN];
14558 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014560 int xp_type = EXPAND_NOTHING;
14561 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565
14566#ifdef NO_CONSOLE_INPUT
14567 /* While starting up, there is no place to enter text. */
14568 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570#endif
14571
14572 cmd_silent = FALSE; /* Want to see the prompt. */
14573 if (prompt != NULL)
14574 {
14575 /* Only the part of the message after the last NL is considered as
14576 * prompt for the command line */
14577 p = vim_strrchr(prompt, '\n');
14578 if (p == NULL)
14579 p = prompt;
14580 else
14581 {
14582 ++p;
14583 c = *p;
14584 *p = NUL;
14585 msg_start();
14586 msg_clr_eos();
14587 msg_puts_attr(prompt, echo_attr);
14588 msg_didout = FALSE;
14589 msg_starthere();
14590 *p = c;
14591 }
14592 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 if (argvars[1].v_type != VAR_UNKNOWN)
14595 {
14596 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14597 if (defstr != NULL)
14598 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014600 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014601 {
14602 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014603 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014604 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014605
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014606 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014608
Bram Moolenaar4463f292005-09-25 22:20:24 +000014609 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14610 if (xp_name == NULL)
14611 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014612
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014613 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014614
Bram Moolenaar4463f292005-09-25 22:20:24 +000014615 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14616 &xp_arg) == FAIL)
14617 return;
14618 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014619 }
14620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014622 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014623 int save_ex_normal_busy = ex_normal_busy;
14624 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014626 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14627 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014628 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014629 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014630 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014631 && argvars[1].v_type != VAR_UNKNOWN
14632 && argvars[2].v_type != VAR_UNKNOWN)
14633 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14634 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014635
14636 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 /* since the user typed this, no need to wait for return */
14639 need_wait_return = FALSE;
14640 msg_didout = FALSE;
14641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 cmd_silent = cmd_silent_save;
14643}
14644
14645/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014646 * "input()" function
14647 * Also handles inputsecret() when inputsecret is set.
14648 */
14649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014650f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014651{
14652 get_user_input(argvars, rettv, FALSE);
14653}
14654
14655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 * "inputdialog()" function
14657 */
14658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014659f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660{
14661#if defined(FEAT_GUI_TEXTDIALOG)
14662 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14663 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14664 {
14665 char_u *message;
14666 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 message = get_tv_string_chk(&argvars[0]);
14670 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014671 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014672 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 else
14674 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 if (message != NULL && defstr != NULL
14676 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014677 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 else
14680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 if (message != NULL && defstr != NULL
14682 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014683 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->vval.v_string = vim_strsave(
14685 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014689 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 }
14691 else
14692#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014693 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694}
14695
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014696/*
14697 * "inputlist()" function
14698 */
14699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014700f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014701{
14702 listitem_T *li;
14703 int selected;
14704 int mouse_used;
14705
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014706#ifdef NO_CONSOLE_INPUT
14707 /* While starting up, there is no place to enter text. */
14708 if (no_console_input())
14709 return;
14710#endif
14711 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14712 {
14713 EMSG2(_(e_listarg), "inputlist()");
14714 return;
14715 }
14716
14717 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014718 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014719 lines_left = Rows; /* avoid more prompt */
14720 msg_scroll = TRUE;
14721 msg_clr_eos();
14722
14723 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14724 {
14725 msg_puts(get_tv_string(&li->li_tv));
14726 msg_putchar('\n');
14727 }
14728
14729 /* Ask for choice. */
14730 selected = prompt_for_number(&mouse_used);
14731 if (mouse_used)
14732 selected -= lines_left;
14733
14734 rettv->vval.v_number = selected;
14735}
14736
14737
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14739
14740/*
14741 * "inputrestore()" function
14742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014744f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745{
14746 if (ga_userinput.ga_len > 0)
14747 {
14748 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14750 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014751 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 }
14753 else if (p_verbose > 1)
14754 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014755 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 }
14758}
14759
14760/*
14761 * "inputsave()" function
14762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014764f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014766 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 if (ga_grow(&ga_userinput, 1) == OK)
14768 {
14769 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14770 + ga_userinput.ga_len);
14771 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014772 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 }
14774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776}
14777
14778/*
14779 * "inputsecret()" function
14780 */
14781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014782f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783{
14784 ++cmdline_star;
14785 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 --cmdline_star;
14788 --inputsecret_flag;
14789}
14790
14791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014792 * "insert()" function
14793 */
14794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014795f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014796{
14797 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014798 listitem_T *item;
14799 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801
14802 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014803 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014804 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014805 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014806 {
14807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 before = get_tv_number_chk(&argvars[2], &error);
14809 if (error)
14810 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014811
Bram Moolenaar758711c2005-02-02 23:11:38 +000014812 if (before == l->lv_len)
14813 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814 else
14815 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014816 item = list_find(l, before);
14817 if (item == NULL)
14818 {
14819 EMSGN(_(e_listidx), before);
14820 l = NULL;
14821 }
14822 }
14823 if (l != NULL)
14824 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014825 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014826 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014827 }
14828 }
14829}
14830
14831/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014832 * "invert(expr)" function
14833 */
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014836{
14837 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14838}
14839
14840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 * "isdirectory()" function
14842 */
14843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014844f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847}
14848
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014849/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014850 * "islocked()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014854{
14855 lval_T lv;
14856 char_u *end;
14857 dictitem_T *di;
14858
14859 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014860 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14861 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014862 if (end != NULL && lv.ll_name != NULL)
14863 {
14864 if (*end != NUL)
14865 EMSG(_(e_trailing));
14866 else
14867 {
14868 if (lv.ll_tv == NULL)
14869 {
14870 if (check_changedtick(lv.ll_name))
14871 rettv->vval.v_number = 1; /* always locked */
14872 else
14873 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014874 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014875 if (di != NULL)
14876 {
14877 /* Consider a variable locked when:
14878 * 1. the variable itself is locked
14879 * 2. the value of the variable is locked.
14880 * 3. the List or Dict value is locked.
14881 */
14882 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14883 || tv_islocked(&di->di_tv));
14884 }
14885 }
14886 }
14887 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014888 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014889 else if (lv.ll_newkey != NULL)
14890 EMSG2(_(e_dictkey), lv.ll_newkey);
14891 else if (lv.ll_list != NULL)
14892 /* List item. */
14893 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14894 else
14895 /* Dictionary item. */
14896 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14897 }
14898 }
14899
14900 clear_lval(&lv);
14901}
14902
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014903#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14904/*
14905 * "isnan()" function
14906 */
14907 static void
14908f_isnan(typval_T *argvars, typval_T *rettv)
14909{
14910 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14911 && isnan(argvars[0].vval.v_float);
14912}
14913#endif
14914
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014915static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014916
14917/*
14918 * Turn a dict into a list:
14919 * "what" == 0: list of keys
14920 * "what" == 1: list of values
14921 * "what" == 2: list of items
14922 */
14923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014924dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925{
Bram Moolenaar33570922005-01-25 22:26:29 +000014926 list_T *l2;
14927 dictitem_T *di;
14928 hashitem_T *hi;
14929 listitem_T *li;
14930 listitem_T *li2;
14931 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014932 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934 if (argvars[0].v_type != VAR_DICT)
14935 {
14936 EMSG(_(e_dictreq));
14937 return;
14938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014939 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014940 return;
14941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014942 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014945 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014946 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014948 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 --todo;
14951 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014952
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014953 li = listitem_alloc();
14954 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014956 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014957
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014958 if (what == 0)
14959 {
14960 /* keys() */
14961 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014962 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014963 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14964 }
14965 else if (what == 1)
14966 {
14967 /* values() */
14968 copy_tv(&di->di_tv, &li->li_tv);
14969 }
14970 else
14971 {
14972 /* items() */
14973 l2 = list_alloc();
14974 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014975 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014976 li->li_tv.vval.v_list = l2;
14977 if (l2 == NULL)
14978 break;
14979 ++l2->lv_refcount;
14980
14981 li2 = listitem_alloc();
14982 if (li2 == NULL)
14983 break;
14984 list_append(l2, li2);
14985 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014986 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014987 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14988
14989 li2 = listitem_alloc();
14990 if (li2 == NULL)
14991 break;
14992 list_append(l2, li2);
14993 copy_tv(&di->di_tv, &li2->li_tv);
14994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014995 }
14996 }
14997}
14998
14999/*
15000 * "items(dict)" function
15001 */
15002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015003f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004{
15005 dict_list(argvars, rettv, 2);
15006}
15007
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015008#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015009/*
15010 * Get the job from the argument.
15011 * Returns NULL if the job is invalid.
15012 */
15013 static job_T *
15014get_job_arg(typval_T *tv)
15015{
15016 job_T *job;
15017
15018 if (tv->v_type != VAR_JOB)
15019 {
15020 EMSG2(_(e_invarg2), get_tv_string(tv));
15021 return NULL;
15022 }
15023 job = tv->vval.v_job;
15024
15025 if (job == NULL)
15026 EMSG(_("E916: not a valid job"));
15027 return job;
15028}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015029
15030# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015031/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015032 * "job_getchannel()" function
15033 */
15034 static void
15035f_job_getchannel(typval_T *argvars, typval_T *rettv)
15036{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015037 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015038
Bram Moolenaar65edff82016-02-21 16:40:11 +010015039 if (job != NULL)
15040 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015041 rettv->v_type = VAR_CHANNEL;
15042 rettv->vval.v_channel = job->jv_channel;
15043 if (job->jv_channel != NULL)
15044 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015045 }
15046}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015047# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015048
15049/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015050 * "job_setoptions()" function
15051 */
15052 static void
15053f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15054{
15055 job_T *job = get_job_arg(&argvars[0]);
15056 jobopt_T opt;
15057
15058 if (job == NULL)
15059 return;
15060 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015061 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015062 return;
15063 job_set_options(job, &opt);
15064}
15065
15066/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015067 * "job_start()" function
15068 */
15069 static void
15070f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15071{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015072 job_T *job;
15073 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015074#if defined(UNIX)
15075# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015076 char **argv = NULL;
15077 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015078#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015079 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015081 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015082
15083 rettv->v_type = VAR_JOB;
15084 job = job_alloc();
15085 rettv->vval.v_job = job;
15086 if (job == NULL)
15087 return;
15088
15089 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015090
15091 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015092 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015093 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015094 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015095 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015096 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015097 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015098 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015099
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015101 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015102#endif
15103
15104 if (argvars[0].v_type == VAR_STRING)
15105 {
15106 /* Command is a string. */
15107 cmd = argvars[0].vval.v_string;
15108#ifdef USE_ARGV
15109 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15110 return;
15111 argv[argc] = NULL;
15112#endif
15113 }
15114 else if (argvars[0].v_type != VAR_LIST
15115 || argvars[0].vval.v_list == NULL
15116 || argvars[0].vval.v_list->lv_len < 1)
15117 {
15118 EMSG(_(e_invarg));
15119 return;
15120 }
15121 else
15122 {
15123 list_T *l = argvars[0].vval.v_list;
15124 listitem_T *li;
15125 char_u *s;
15126
15127#ifdef USE_ARGV
15128 /* Pass argv[] to mch_call_shell(). */
15129 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15130 if (argv == NULL)
15131 return;
15132#endif
15133 for (li = l->lv_first; li != NULL; li = li->li_next)
15134 {
15135 s = get_tv_string_chk(&li->li_tv);
15136 if (s == NULL)
15137 goto theend;
15138#ifdef USE_ARGV
15139 argv[argc++] = (char *)s;
15140#else
15141 if (li != l->lv_first)
15142 {
15143 s = vim_strsave_shellescape(s, FALSE, TRUE);
15144 if (s == NULL)
15145 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015146 ga_concat(&ga, s);
15147 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015148 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015149 else
15150 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015151 if (li->li_next != NULL)
15152 ga_append(&ga, ' ');
15153#endif
15154 }
15155#ifdef USE_ARGV
15156 argv[argc] = NULL;
15157#else
15158 cmd = ga.ga_data;
15159#endif
15160 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015161
Bram Moolenaar835dc632016-02-07 14:27:38 +010015162#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015163# ifdef FEAT_CHANNEL
15164 if (ch_log_active())
15165 {
15166 garray_T ga;
15167 int i;
15168
15169 ga_init2(&ga, (int)sizeof(char), 200);
15170 for (i = 0; i < argc; ++i)
15171 {
15172 if (i > 0)
15173 ga_concat(&ga, (char_u *)" ");
15174 ga_concat(&ga, (char_u *)argv[i]);
15175 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015176 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015177 ga_clear(&ga);
15178 }
15179# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015180 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015181#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015182# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015183 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015184# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015185 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015186#endif
15187
15188theend:
15189#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015190 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015191#else
15192 vim_free(ga.ga_data);
15193#endif
15194}
15195
15196/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015197 * Get the status of "job" and invoke the exit callback when needed.
15198 * The returned string is not allocated.
15199 */
15200 static char *
15201job_status(job_T *job)
15202{
15203 char *result;
15204
15205 if (job->jv_status == JOB_ENDED)
15206 /* No need to check, dead is dead. */
15207 result = "dead";
15208 else if (job->jv_status == JOB_FAILED)
15209 result = "fail";
15210 else
15211 {
15212 result = mch_job_status(job);
15213# ifdef FEAT_CHANNEL
15214 if (job->jv_status == JOB_ENDED)
15215 ch_log(job->jv_channel, "Job ended");
15216# endif
15217 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15218 {
15219 typval_T argv[3];
15220 typval_T rettv;
15221 int dummy;
15222
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015223 /* invoke the exit callback; make sure the refcount is > 0 */
15224 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015225 argv[0].v_type = VAR_JOB;
15226 argv[0].vval.v_job = job;
15227 argv[1].v_type = VAR_NUMBER;
15228 argv[1].vval.v_number = job->jv_exitval;
15229 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15230 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15231 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015232 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015233 }
15234 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15235 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015236 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015237 * freed. Careful: caller must not use "job" after this! */
15238 job_free(job);
15239 }
15240 }
15241 return result;
15242}
15243
15244/*
15245 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15246 */
15247 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015248job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015249{
15250 static time_t last_check = 0;
15251 time_t now;
15252 job_T *job;
15253 job_T *next;
15254
15255 /* Only do this once in 10 seconds. */
15256 now = time(NULL);
15257 if (last_check + 10 < now)
15258 {
15259 last_check = now;
15260 for (job = first_job; job != NULL; job = next)
15261 {
15262 next = job->jv_next;
15263 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15264 job_status(job); /* may free "job" */
15265 }
15266 }
15267}
15268
15269/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015270 * "job_status()" function
15271 */
15272 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015273f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015274{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015275 job_T *job = get_job_arg(&argvars[0]);
15276 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015277
Bram Moolenaar65edff82016-02-21 16:40:11 +010015278 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015279 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015280 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015281 rettv->v_type = VAR_STRING;
15282 rettv->vval.v_string = vim_strsave((char_u *)result);
15283 }
15284}
15285
15286/*
15287 * "job_stop()" function
15288 */
15289 static void
15290f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15291{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015292 job_T *job = get_job_arg(&argvars[0]);
15293
15294 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015295 {
15296 char_u *arg;
15297
15298 if (argvars[1].v_type == VAR_UNKNOWN)
15299 arg = (char_u *)"";
15300 else
15301 {
15302 arg = get_tv_string_chk(&argvars[1]);
15303 if (arg == NULL)
15304 {
15305 EMSG(_(e_invarg));
15306 return;
15307 }
15308 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015309# ifdef FEAT_CHANNEL
15310 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15311# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015312 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015313 rettv->vval.v_number = 0;
15314 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015315 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015316 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015317 /* Assume that "hup" does not kill the job. */
15318 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15319 job->jv_channel->ch_job_killed = TRUE;
15320 }
15321 /* We don't try freeing the job, obviously the caller still has a
15322 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015323 }
15324}
15325#endif
15326
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015328 * "join()" function
15329 */
15330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015331f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015332{
15333 garray_T ga;
15334 char_u *sep;
15335
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015336 if (argvars[0].v_type != VAR_LIST)
15337 {
15338 EMSG(_(e_listreq));
15339 return;
15340 }
15341 if (argvars[0].vval.v_list == NULL)
15342 return;
15343 if (argvars[1].v_type == VAR_UNKNOWN)
15344 sep = (char_u *)" ";
15345 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015347
15348 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015349
15350 if (sep != NULL)
15351 {
15352 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015353 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015354 ga_append(&ga, NUL);
15355 rettv->vval.v_string = (char_u *)ga.ga_data;
15356 }
15357 else
15358 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015359}
15360
15361/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015362 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015363 */
15364 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015365f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015366{
15367 js_read_T reader;
15368
15369 reader.js_buf = get_tv_string(&argvars[0]);
15370 reader.js_fill = NULL;
15371 reader.js_used = 0;
15372 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15373 EMSG(_(e_invarg));
15374}
15375
15376/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015377 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015378 */
15379 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015380f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015381{
15382 rettv->v_type = VAR_STRING;
15383 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15384}
15385
15386/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015387 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015388 */
15389 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015390f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015391{
15392 js_read_T reader;
15393
15394 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015395 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015396 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015397 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015398 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015399}
15400
15401/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015402 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015403 */
15404 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015405f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015406{
15407 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015408 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015409}
15410
15411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015412 * "keys()" function
15413 */
15414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015415f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416{
15417 dict_list(argvars, rettv, 0);
15418}
15419
15420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 * "last_buffer_nr()" function.
15422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015424f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425{
15426 int n = 0;
15427 buf_T *buf;
15428
15429 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15430 if (n < buf->b_fnum)
15431 n = buf->b_fnum;
15432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015434}
15435
15436/*
15437 * "len()" function
15438 */
15439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015440f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015441{
15442 switch (argvars[0].v_type)
15443 {
15444 case VAR_STRING:
15445 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446 rettv->vval.v_number = (varnumber_T)STRLEN(
15447 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448 break;
15449 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015451 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015452 case VAR_DICT:
15453 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15454 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015455 case VAR_UNKNOWN:
15456 case VAR_SPECIAL:
15457 case VAR_FLOAT:
15458 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015459 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015460 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015461 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 break;
15463 }
15464}
15465
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015466static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015467
15468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015469libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015470{
15471#ifdef FEAT_LIBCALL
15472 char_u *string_in;
15473 char_u **string_result;
15474 int nr_result;
15475#endif
15476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015478 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015480
15481 if (check_restricted() || check_secure())
15482 return;
15483
15484#ifdef FEAT_LIBCALL
15485 /* The first two args must be strings, otherwise its meaningless */
15486 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15487 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015488 string_in = NULL;
15489 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015490 string_in = argvars[2].vval.v_string;
15491 if (type == VAR_NUMBER)
15492 string_result = NULL;
15493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015495 if (mch_libcall(argvars[0].vval.v_string,
15496 argvars[1].vval.v_string,
15497 string_in,
15498 argvars[2].vval.v_number,
15499 string_result,
15500 &nr_result) == OK
15501 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015502 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015503 }
15504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505}
15506
15507/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015508 * "libcall()" function
15509 */
15510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512{
15513 libcall_common(argvars, rettv, VAR_STRING);
15514}
15515
15516/*
15517 * "libcallnr()" function
15518 */
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015521{
15522 libcall_common(argvars, rettv, VAR_NUMBER);
15523}
15524
15525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 * "line(string)" function
15527 */
15528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015529f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530{
15531 linenr_T lnum = 0;
15532 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015533 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015535 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 if (fp != NULL)
15537 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
15542 * "line2byte(lnum)" function
15543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015545f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546{
15547#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549#else
15550 linenr_T lnum;
15551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15557 if (rettv->vval.v_number >= 0)
15558 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559#endif
15560}
15561
15562/*
15563 * "lispindent(lnum)" function
15564 */
15565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567{
15568#ifdef FEAT_LISP
15569 pos_T pos;
15570 linenr_T lnum;
15571
15572 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15575 {
15576 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 curwin->w_cursor = pos;
15579 }
15580 else
15581#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583}
15584
15585/*
15586 * "localtime()" function
15587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015589f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592}
15593
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015594static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595
15596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015597get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598{
15599 char_u *keys;
15600 char_u *which;
15601 char_u buf[NUMBUFLEN];
15602 char_u *keys_buf = NULL;
15603 char_u *rhs;
15604 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015605 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015606 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015607 mapblock_T *mp;
15608 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609
15610 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015611 rettv->v_type = VAR_STRING;
15612 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 if (*keys == NUL)
15616 return;
15617
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015618 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015622 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015623 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015624 if (argvars[3].v_type != VAR_UNKNOWN)
15625 get_dict = get_tv_number(&argvars[3]);
15626 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 else
15629 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015630 if (which == NULL)
15631 return;
15632
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 mode = get_map_mode(&which, 0);
15634
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015635 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015636 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015638
15639 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015641 /* Return a string. */
15642 if (rhs != NULL)
15643 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644
Bram Moolenaarbd743252010-10-20 21:23:33 +020015645 }
15646 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15647 {
15648 /* Return a dictionary. */
15649 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15650 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15651 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652
Bram Moolenaarbd743252010-10-20 21:23:33 +020015653 dict_add_nr_str(dict, "lhs", 0L, lhs);
15654 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15655 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15656 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15657 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15658 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15659 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015660 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015661 dict_add_nr_str(dict, "mode", 0L, mapmode);
15662
15663 vim_free(lhs);
15664 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 }
15666}
15667
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015668#ifdef FEAT_FLOAT
15669/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015670 * "log()" function
15671 */
15672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015673f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015674{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015675 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015676
15677 rettv->v_type = VAR_FLOAT;
15678 if (get_float_arg(argvars, &f) == OK)
15679 rettv->vval.v_float = log(f);
15680 else
15681 rettv->vval.v_float = 0.0;
15682}
15683
15684/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015685 * "log10()" function
15686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015689{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015690 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015691
15692 rettv->v_type = VAR_FLOAT;
15693 if (get_float_arg(argvars, &f) == OK)
15694 rettv->vval.v_float = log10(f);
15695 else
15696 rettv->vval.v_float = 0.0;
15697}
15698#endif
15699
Bram Moolenaar1dced572012-04-05 16:54:08 +020015700#ifdef FEAT_LUA
15701/*
15702 * "luaeval()" function
15703 */
15704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015706{
15707 char_u *str;
15708 char_u buf[NUMBUFLEN];
15709
15710 str = get_tv_string_buf(&argvars[0], buf);
15711 do_luaeval(str, argvars + 1, rettv);
15712}
15713#endif
15714
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015716 * "map()" function
15717 */
15718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015719f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015720{
15721 filter_map(argvars, rettv, TRUE);
15722}
15723
15724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 */
15727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015728f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731}
15732
15733/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 */
15736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015737f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740}
15741
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015742static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743
15744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015745find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015747 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015748 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015749 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 char_u *pat;
15751 regmatch_T regmatch;
15752 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015753 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 char_u *save_cpo;
15755 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015756 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015757 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015758 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 list_T *l = NULL;
15760 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015761 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015762 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
15764 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15765 save_cpo = p_cpo;
15766 p_cpo = (char_u *)"";
15767
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015768 rettv->vval.v_number = -1;
15769 if (type == 3)
15770 {
15771 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015772 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015773 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015774 }
15775 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015777 rettv->v_type = VAR_STRING;
15778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015781 if (argvars[0].v_type == VAR_LIST)
15782 {
15783 if ((l = argvars[0].vval.v_list) == NULL)
15784 goto theend;
15785 li = l->lv_first;
15786 }
15787 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015788 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015789 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015790 len = (long)STRLEN(str);
15791 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015793 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15794 if (pat == NULL)
15795 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015797 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015799 int error = FALSE;
15800
15801 start = get_tv_number_chk(&argvars[2], &error);
15802 if (error)
15803 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015804 if (l != NULL)
15805 {
15806 li = list_find(l, start);
15807 if (li == NULL)
15808 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015809 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015810 }
15811 else
15812 {
15813 if (start < 0)
15814 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015815 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015816 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015817 /* When "count" argument is there ignore matches before "start",
15818 * otherwise skip part of the string. Differs when pattern is "^"
15819 * or "\<". */
15820 if (argvars[3].v_type != VAR_UNKNOWN)
15821 startcol = start;
15822 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015823 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015824 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015825 len -= start;
15826 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015828
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015829 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015830 nth = get_tv_number_chk(&argvars[3], &error);
15831 if (error)
15832 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 }
15834
15835 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15836 if (regmatch.regprog != NULL)
15837 {
15838 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015839
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015840 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015841 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015842 if (l != NULL)
15843 {
15844 if (li == NULL)
15845 {
15846 match = FALSE;
15847 break;
15848 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015850 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015851 if (str == NULL)
15852 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015853 }
15854
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015855 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015856
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015857 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015858 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015859 if (l == NULL && !match)
15860 break;
15861
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015862 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015863 if (l != NULL)
15864 {
15865 li = li->li_next;
15866 ++idx;
15867 }
15868 else
15869 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015870#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015871 startcol = (colnr_T)(regmatch.startp[0]
15872 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015873#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015874 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015875#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015876 if (startcol > (colnr_T)len
15877 || str + startcol <= regmatch.startp[0])
15878 {
15879 match = FALSE;
15880 break;
15881 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015882 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015883 }
15884
15885 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015889 int i;
15890
15891 /* return list with matched string and submatches */
15892 for (i = 0; i < NSUBEXP; ++i)
15893 {
15894 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015895 {
15896 if (list_append_string(rettv->vval.v_list,
15897 (char_u *)"", 0) == FAIL)
15898 break;
15899 }
15900 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015901 regmatch.startp[i],
15902 (int)(regmatch.endp[i] - regmatch.startp[i]))
15903 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015904 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015905 }
15906 }
15907 else if (type == 2)
15908 {
15909 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015910 if (l != NULL)
15911 copy_tv(&li->li_tv, rettv);
15912 else
15913 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015915 }
15916 else if (l != NULL)
15917 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 else
15919 {
15920 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015921 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 (varnumber_T)(regmatch.startp[0] - str);
15923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015924 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015926 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 }
15928 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015929 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 }
15931
15932theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015933 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934 p_cpo = save_cpo;
15935}
15936
15937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015938 * "match()" function
15939 */
15940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942{
15943 find_some_match(argvars, rettv, 1);
15944}
15945
15946/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015947 * "matchadd()" function
15948 */
15949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015950f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015951{
15952#ifdef FEAT_SEARCH_EXTRA
15953 char_u buf[NUMBUFLEN];
15954 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15955 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15956 int prio = 10; /* default priority */
15957 int id = -1;
15958 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015959 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015960
15961 rettv->vval.v_number = -1;
15962
15963 if (grp == NULL || pat == NULL)
15964 return;
15965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015966 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015967 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015968 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015969 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015970 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015971 if (argvars[4].v_type != VAR_UNKNOWN)
15972 {
15973 if (argvars[4].v_type != VAR_DICT)
15974 {
15975 EMSG(_(e_dictreq));
15976 return;
15977 }
15978 if (dict_find(argvars[4].vval.v_dict,
15979 (char_u *)"conceal", -1) != NULL)
15980 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15981 (char_u *)"conceal", FALSE);
15982 }
15983 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015984 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015985 if (error == TRUE)
15986 return;
15987 if (id >= 1 && id <= 3)
15988 {
15989 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15990 return;
15991 }
15992
Bram Moolenaar6561d522015-07-21 15:48:27 +020015993 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15994 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015995#endif
15996}
15997
15998/*
15999 * "matchaddpos()" function
16000 */
16001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016002f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016003{
16004#ifdef FEAT_SEARCH_EXTRA
16005 char_u buf[NUMBUFLEN];
16006 char_u *group;
16007 int prio = 10;
16008 int id = -1;
16009 int error = FALSE;
16010 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016011 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016012
16013 rettv->vval.v_number = -1;
16014
16015 group = get_tv_string_buf_chk(&argvars[0], buf);
16016 if (group == NULL)
16017 return;
16018
16019 if (argvars[1].v_type != VAR_LIST)
16020 {
16021 EMSG2(_(e_listarg), "matchaddpos()");
16022 return;
16023 }
16024 l = argvars[1].vval.v_list;
16025 if (l == NULL)
16026 return;
16027
16028 if (argvars[2].v_type != VAR_UNKNOWN)
16029 {
16030 prio = get_tv_number_chk(&argvars[2], &error);
16031 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016032 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016033 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016034 if (argvars[4].v_type != VAR_UNKNOWN)
16035 {
16036 if (argvars[4].v_type != VAR_DICT)
16037 {
16038 EMSG(_(e_dictreq));
16039 return;
16040 }
16041 if (dict_find(argvars[4].vval.v_dict,
16042 (char_u *)"conceal", -1) != NULL)
16043 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16044 (char_u *)"conceal", FALSE);
16045 }
16046 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016047 }
16048 if (error == TRUE)
16049 return;
16050
16051 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16052 if (id == 1 || id == 2)
16053 {
16054 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16055 return;
16056 }
16057
Bram Moolenaar6561d522015-07-21 15:48:27 +020016058 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16059 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016060#endif
16061}
16062
16063/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016064 * "matcharg()" function
16065 */
16066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016067f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016068{
16069 if (rettv_list_alloc(rettv) == OK)
16070 {
16071#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016072 int id = get_tv_number(&argvars[0]);
16073 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016074
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016075 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016076 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016077 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16078 {
16079 list_append_string(rettv->vval.v_list,
16080 syn_id2name(m->hlg_id), -1);
16081 list_append_string(rettv->vval.v_list, m->pattern, -1);
16082 }
16083 else
16084 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016085 list_append_string(rettv->vval.v_list, NULL, -1);
16086 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016087 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016088 }
16089#endif
16090 }
16091}
16092
16093/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016094 * "matchdelete()" function
16095 */
16096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016097f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016098{
16099#ifdef FEAT_SEARCH_EXTRA
16100 rettv->vval.v_number = match_delete(curwin,
16101 (int)get_tv_number(&argvars[0]), TRUE);
16102#endif
16103}
16104
16105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106 * "matchend()" function
16107 */
16108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016109f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110{
16111 find_some_match(argvars, rettv, 0);
16112}
16113
16114/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115 * "matchlist()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016119{
16120 find_some_match(argvars, rettv, 3);
16121}
16122
16123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124 * "matchstr()" function
16125 */
16126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016127f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016128{
16129 find_some_match(argvars, rettv, 2);
16130}
16131
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016132static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016133
16134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016135max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016136{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016137 long n = 0;
16138 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016139 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016140
16141 if (argvars[0].v_type == VAR_LIST)
16142 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 list_T *l;
16144 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016145
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016146 l = argvars[0].vval.v_list;
16147 if (l != NULL)
16148 {
16149 li = l->lv_first;
16150 if (li != NULL)
16151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016153 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016154 {
16155 li = li->li_next;
16156 if (li == NULL)
16157 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016159 if (domax ? i > n : i < n)
16160 n = i;
16161 }
16162 }
16163 }
16164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016165 else if (argvars[0].v_type == VAR_DICT)
16166 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016168 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016170 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016171
16172 d = argvars[0].vval.v_dict;
16173 if (d != NULL)
16174 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016175 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016176 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016178 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016180 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016181 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016182 if (first)
16183 {
16184 n = i;
16185 first = FALSE;
16186 }
16187 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016188 n = i;
16189 }
16190 }
16191 }
16192 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016193 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016194 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016195 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016196}
16197
16198/*
16199 * "max()" function
16200 */
16201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016202f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016203{
16204 max_min(argvars, rettv, TRUE);
16205}
16206
16207/*
16208 * "min()" function
16209 */
16210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016211f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016212{
16213 max_min(argvars, rettv, FALSE);
16214}
16215
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016216static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016217
16218/*
16219 * Create the directory in which "dir" is located, and higher levels when
16220 * needed.
16221 */
16222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016223mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016224{
16225 char_u *p;
16226 char_u *updir;
16227 int r = FAIL;
16228
16229 /* Get end of directory name in "dir".
16230 * We're done when it's "/" or "c:/". */
16231 p = gettail_sep(dir);
16232 if (p <= get_past_head(dir))
16233 return OK;
16234
16235 /* If the directory exists we're done. Otherwise: create it.*/
16236 updir = vim_strnsave(dir, (int)(p - dir));
16237 if (updir == NULL)
16238 return FAIL;
16239 if (mch_isdir(updir))
16240 r = OK;
16241 else if (mkdir_recurse(updir, prot) == OK)
16242 r = vim_mkdir_emsg(updir, prot);
16243 vim_free(updir);
16244 return r;
16245}
16246
16247#ifdef vim_mkdir
16248/*
16249 * "mkdir()" function
16250 */
16251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016252f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016253{
16254 char_u *dir;
16255 char_u buf[NUMBUFLEN];
16256 int prot = 0755;
16257
16258 rettv->vval.v_number = FAIL;
16259 if (check_restricted() || check_secure())
16260 return;
16261
16262 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016263 if (*dir == NUL)
16264 rettv->vval.v_number = FAIL;
16265 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016266 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016267 if (*gettail(dir) == NUL)
16268 /* remove trailing slashes */
16269 *gettail_sep(dir) = NUL;
16270
16271 if (argvars[1].v_type != VAR_UNKNOWN)
16272 {
16273 if (argvars[2].v_type != VAR_UNKNOWN)
16274 prot = get_tv_number_chk(&argvars[2], NULL);
16275 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16276 mkdir_recurse(dir, prot);
16277 }
16278 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016279 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016280}
16281#endif
16282
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 * "mode()" function
16285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016287f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016289 char_u buf[3];
16290
16291 buf[1] = NUL;
16292 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 if (VIsual_active)
16295 {
16296 if (VIsual_select)
16297 buf[0] = VIsual_mode + 's' - 'v';
16298 else
16299 buf[0] = VIsual_mode;
16300 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016301 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016302 || State == CONFIRM)
16303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016305 if (State == ASKMORE)
16306 buf[1] = 'm';
16307 else if (State == CONFIRM)
16308 buf[1] = '?';
16309 }
16310 else if (State == EXTERNCMD)
16311 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 else if (State & INSERT)
16313 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016314#ifdef FEAT_VREPLACE
16315 if (State & VREPLACE_FLAG)
16316 {
16317 buf[0] = 'R';
16318 buf[1] = 'v';
16319 }
16320 else
16321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 if (State & REPLACE_FLAG)
16323 buf[0] = 'R';
16324 else
16325 buf[0] = 'i';
16326 }
16327 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016330 if (exmode_active)
16331 buf[1] = 'v';
16332 }
16333 else if (exmode_active)
16334 {
16335 buf[0] = 'c';
16336 buf[1] = 'e';
16337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016341 if (finish_op)
16342 buf[1] = 'o';
16343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016345 /* Clear out the minor mode when the argument is not a non-zero number or
16346 * non-empty string. */
16347 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016348 buf[1] = NUL;
16349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 rettv->vval.v_string = vim_strsave(buf);
16351 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352}
16353
Bram Moolenaar429fa852013-04-15 12:27:36 +020016354#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016355/*
16356 * "mzeval()" function
16357 */
16358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016359f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016360{
16361 char_u *str;
16362 char_u buf[NUMBUFLEN];
16363
16364 str = get_tv_string_buf(&argvars[0], buf);
16365 do_mzeval(str, rettv);
16366}
Bram Moolenaar75676462013-01-30 14:55:42 +010016367
16368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016370{
16371 typval_T argvars[3];
16372
16373 argvars[0].v_type = VAR_STRING;
16374 argvars[0].vval.v_string = name;
16375 copy_tv(args, &argvars[1]);
16376 argvars[2].v_type = VAR_UNKNOWN;
16377 f_call(argvars, rettv);
16378 clear_tv(&argvars[1]);
16379}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016380#endif
16381
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 * "nextnonblank()" function
16384 */
16385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016387{
16388 linenr_T lnum;
16389
16390 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393 {
16394 lnum = 0;
16395 break;
16396 }
16397 if (*skipwhite(ml_get(lnum)) != NUL)
16398 break;
16399 }
16400 rettv->vval.v_number = lnum;
16401}
16402
16403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404 * "nr2char()" function
16405 */
16406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408{
16409 char_u buf[NUMBUFLEN];
16410
16411#ifdef FEAT_MBYTE
16412 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016413 {
16414 int utf8 = 0;
16415
16416 if (argvars[1].v_type != VAR_UNKNOWN)
16417 utf8 = get_tv_number_chk(&argvars[1], NULL);
16418 if (utf8)
16419 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16420 else
16421 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423 else
16424#endif
16425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016426 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 buf[1] = NUL;
16428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 rettv->v_type = VAR_STRING;
16430 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016431}
16432
16433/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016434 * "or(expr, expr)" function
16435 */
16436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016437f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016438{
16439 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16440 | get_tv_number_chk(&argvars[1], NULL);
16441}
16442
16443/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016444 * "pathshorten()" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016448{
16449 char_u *p;
16450
16451 rettv->v_type = VAR_STRING;
16452 p = get_tv_string_chk(&argvars[0]);
16453 if (p == NULL)
16454 rettv->vval.v_string = NULL;
16455 else
16456 {
16457 p = vim_strsave(p);
16458 rettv->vval.v_string = p;
16459 if (p != NULL)
16460 shorten_dir(p);
16461 }
16462}
16463
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016464#ifdef FEAT_PERL
16465/*
16466 * "perleval()" function
16467 */
16468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016470{
16471 char_u *str;
16472 char_u buf[NUMBUFLEN];
16473
16474 str = get_tv_string_buf(&argvars[0], buf);
16475 do_perleval(str, rettv);
16476}
16477#endif
16478
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016479#ifdef FEAT_FLOAT
16480/*
16481 * "pow()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016485{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016486 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016487
16488 rettv->v_type = VAR_FLOAT;
16489 if (get_float_arg(argvars, &fx) == OK
16490 && get_float_arg(&argvars[1], &fy) == OK)
16491 rettv->vval.v_float = pow(fx, fy);
16492 else
16493 rettv->vval.v_float = 0.0;
16494}
16495#endif
16496
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016497/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 * "prevnonblank()" function
16499 */
16500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016501f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502{
16503 linenr_T lnum;
16504
16505 lnum = get_tv_lnum(argvars);
16506 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16507 lnum = 0;
16508 else
16509 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16510 --lnum;
16511 rettv->vval.v_number = lnum;
16512}
16513
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016514/* This dummy va_list is here because:
16515 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16516 * - locally in the function results in a "used before set" warning
16517 * - using va_start() to initialize it gives "function with fixed args" error */
16518static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016519
Bram Moolenaar8c711452005-01-14 21:53:12 +000016520/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016521 * "printf()" function
16522 */
16523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016524f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016525{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016526 char_u buf[NUMBUFLEN];
16527 int len;
16528 char_u *s;
16529 int saved_did_emsg = did_emsg;
16530 char *fmt;
16531
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016532 rettv->v_type = VAR_STRING;
16533 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016534
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016535 /* Get the required length, allocate the buffer and do it for real. */
16536 did_emsg = FALSE;
16537 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16538 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16539 if (!did_emsg)
16540 {
16541 s = alloc(len + 1);
16542 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016543 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016544 rettv->vval.v_string = s;
16545 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016546 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016547 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016548 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016549}
16550
16551/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016552 * "pumvisible()" function
16553 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016555f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557#ifdef FEAT_INS_EXPAND
16558 if (pum_visible())
16559 rettv->vval.v_number = 1;
16560#endif
16561}
16562
Bram Moolenaardb913952012-06-29 12:54:53 +020016563#ifdef FEAT_PYTHON3
16564/*
16565 * "py3eval()" function
16566 */
16567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016569{
16570 char_u *str;
16571 char_u buf[NUMBUFLEN];
16572
16573 str = get_tv_string_buf(&argvars[0], buf);
16574 do_py3eval(str, rettv);
16575}
16576#endif
16577
16578#ifdef FEAT_PYTHON
16579/*
16580 * "pyeval()" function
16581 */
16582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016583f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016584{
16585 char_u *str;
16586 char_u buf[NUMBUFLEN];
16587
16588 str = get_tv_string_buf(&argvars[0], buf);
16589 do_pyeval(str, rettv);
16590}
16591#endif
16592
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016594 * "range()" function
16595 */
16596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016598{
16599 long start;
16600 long end;
16601 long stride = 1;
16602 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016603 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606 if (argvars[1].v_type == VAR_UNKNOWN)
16607 {
16608 end = start - 1;
16609 start = 0;
16610 }
16611 else
16612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016614 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016615 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016616 }
16617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016618 if (error)
16619 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016620 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016621 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016622 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016623 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016624 else
16625 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016626 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016627 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016628 if (list_append_number(rettv->vval.v_list,
16629 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016630 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016631 }
16632}
16633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016634/*
16635 * "readfile()" function
16636 */
16637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016638f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639{
16640 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016641 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 char_u *fname;
16643 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016644 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16645 int io_size = sizeof(buf);
16646 int readlen; /* size of last fread() */
16647 char_u *prev = NULL; /* previously read bytes, if any */
16648 long prevlen = 0; /* length of data in prev */
16649 long prevsize = 0; /* size of prev buffer */
16650 long maxline = MAXLNUM;
16651 long cnt = 0;
16652 char_u *p; /* position in buf */
16653 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016654
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016655 if (argvars[1].v_type != VAR_UNKNOWN)
16656 {
16657 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16658 binary = TRUE;
16659 if (argvars[2].v_type != VAR_UNKNOWN)
16660 maxline = get_tv_number(&argvars[2]);
16661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016665
16666 /* Always open the file in binary mode, library functions have a mind of
16667 * their own about CR-LF conversion. */
16668 fname = get_tv_string(&argvars[0]);
16669 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16670 {
16671 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16672 return;
16673 }
16674
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016675 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016676 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016677 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016678
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016679 /* This for loop processes what was read, but is also entered at end
16680 * of file so that either:
16681 * - an incomplete line gets written
16682 * - a "binary" file gets an empty line at the end if it ends in a
16683 * newline. */
16684 for (p = buf, start = buf;
16685 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16686 ++p)
16687 {
16688 if (*p == '\n' || readlen <= 0)
16689 {
16690 listitem_T *li;
16691 char_u *s = NULL;
16692 long_u len = p - start;
16693
16694 /* Finished a line. Remove CRs before NL. */
16695 if (readlen > 0 && !binary)
16696 {
16697 while (len > 0 && start[len - 1] == '\r')
16698 --len;
16699 /* removal may cross back to the "prev" string */
16700 if (len == 0)
16701 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16702 --prevlen;
16703 }
16704 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016705 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016706 else
16707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 /* Change "prev" buffer to be the right size. This way
16709 * the bytes are only copied once, and very long lines are
16710 * allocated only once. */
16711 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016712 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016713 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016714 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016715 prev = NULL; /* the list will own the string */
16716 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016717 }
16718 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016719 if (s == NULL)
16720 {
16721 do_outofmem_msg((long_u) prevlen + len + 1);
16722 failed = TRUE;
16723 break;
16724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016726 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016727 {
16728 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016729 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016730 break;
16731 }
16732 li->li_tv.v_type = VAR_STRING;
16733 li->li_tv.v_lock = 0;
16734 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016735 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016736
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016737 start = p + 1; /* step over newline */
16738 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016739 break;
16740 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016741 else if (*p == NUL)
16742 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016743#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016744 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16745 * when finding the BF and check the previous two bytes. */
16746 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016747 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016748 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16749 * + 1, these may be in the "prev" string. */
16750 char_u back1 = p >= buf + 1 ? p[-1]
16751 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16752 char_u back2 = p >= buf + 2 ? p[-2]
16753 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16754 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016755
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016756 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016758 char_u *dest = p - 2;
16759
16760 /* Usually a BOM is at the beginning of a file, and so at
16761 * the beginning of a line; then we can just step over it.
16762 */
16763 if (start == dest)
16764 start = p + 1;
16765 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016766 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016767 /* have to shuffle buf to close gap */
16768 int adjust_prevlen = 0;
16769
16770 if (dest < buf)
16771 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016772 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016773 dest = buf;
16774 }
16775 if (readlen > p - buf + 1)
16776 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16777 readlen -= 3 - adjust_prevlen;
16778 prevlen -= adjust_prevlen;
16779 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016780 }
16781 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016782 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016783#endif
16784 } /* for */
16785
16786 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16787 break;
16788 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016789 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016790 /* There's part of a line in buf, store it in "prev". */
16791 if (p - start + prevlen >= prevsize)
16792 {
16793 /* need bigger "prev" buffer */
16794 char_u *newprev;
16795
16796 /* A common use case is ordinary text files and "prev" gets a
16797 * fragment of a line, so the first allocation is made
16798 * small, to avoid repeatedly 'allocing' large and
16799 * 'reallocing' small. */
16800 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016801 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016802 else
16803 {
16804 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016805 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016806 prevsize = grow50pc > growmin ? grow50pc : growmin;
16807 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016808 newprev = prev == NULL ? alloc(prevsize)
16809 : vim_realloc(prev, prevsize);
16810 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016811 {
16812 do_outofmem_msg((long_u)prevsize);
16813 failed = TRUE;
16814 break;
16815 }
16816 prev = newprev;
16817 }
16818 /* Add the line part to end of "prev". */
16819 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016820 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016821 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016822 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016823
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016824 /*
16825 * For a negative line count use only the lines at the end of the file,
16826 * free the rest.
16827 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016828 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016829 while (cnt > -maxline)
16830 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016831 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016832 --cnt;
16833 }
16834
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016835 if (failed)
16836 {
16837 list_free(rettv->vval.v_list, TRUE);
16838 /* readfile doc says an empty list is returned on error */
16839 rettv->vval.v_list = list_alloc();
16840 }
16841
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016842 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016843 fclose(fd);
16844}
16845
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016846#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016847static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016848
16849/*
16850 * Convert a List to proftime_T.
16851 * Return FAIL when there is something wrong.
16852 */
16853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016854list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016855{
16856 long n1, n2;
16857 int error = FALSE;
16858
16859 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16860 || arg->vval.v_list->lv_len != 2)
16861 return FAIL;
16862 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16863 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16864# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016865 tm->HighPart = n1;
16866 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016867# else
16868 tm->tv_sec = n1;
16869 tm->tv_usec = n2;
16870# endif
16871 return error ? FAIL : OK;
16872}
16873#endif /* FEAT_RELTIME */
16874
16875/*
16876 * "reltime()" function
16877 */
16878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016879f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016880{
16881#ifdef FEAT_RELTIME
16882 proftime_T res;
16883 proftime_T start;
16884
16885 if (argvars[0].v_type == VAR_UNKNOWN)
16886 {
16887 /* No arguments: get current time. */
16888 profile_start(&res);
16889 }
16890 else if (argvars[1].v_type == VAR_UNKNOWN)
16891 {
16892 if (list2proftime(&argvars[0], &res) == FAIL)
16893 return;
16894 profile_end(&res);
16895 }
16896 else
16897 {
16898 /* Two arguments: compute the difference. */
16899 if (list2proftime(&argvars[0], &start) == FAIL
16900 || list2proftime(&argvars[1], &res) == FAIL)
16901 return;
16902 profile_sub(&res, &start);
16903 }
16904
16905 if (rettv_list_alloc(rettv) == OK)
16906 {
16907 long n1, n2;
16908
16909# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016910 n1 = res.HighPart;
16911 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016912# else
16913 n1 = res.tv_sec;
16914 n2 = res.tv_usec;
16915# endif
16916 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16917 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16918 }
16919#endif
16920}
16921
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016922#ifdef FEAT_FLOAT
16923/*
16924 * "reltimefloat()" function
16925 */
16926 static void
16927f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16928{
16929# ifdef FEAT_RELTIME
16930 proftime_T tm;
16931# endif
16932
16933 rettv->v_type = VAR_FLOAT;
16934 rettv->vval.v_float = 0;
16935# ifdef FEAT_RELTIME
16936 if (list2proftime(&argvars[0], &tm) == OK)
16937 rettv->vval.v_float = profile_float(&tm);
16938# endif
16939}
16940#endif
16941
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016942/*
16943 * "reltimestr()" function
16944 */
16945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016947{
16948#ifdef FEAT_RELTIME
16949 proftime_T tm;
16950#endif
16951
16952 rettv->v_type = VAR_STRING;
16953 rettv->vval.v_string = NULL;
16954#ifdef FEAT_RELTIME
16955 if (list2proftime(&argvars[0], &tm) == OK)
16956 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16957#endif
16958}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016959
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016961static void make_connection(void);
16962static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963
16964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016965make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966{
16967 if (X_DISPLAY == NULL
16968# ifdef FEAT_GUI
16969 && !gui.in_use
16970# endif
16971 )
16972 {
16973 x_force_connect = TRUE;
16974 setup_term_clip();
16975 x_force_connect = FALSE;
16976 }
16977}
16978
16979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016980check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981{
16982 make_connection();
16983 if (X_DISPLAY == NULL)
16984 {
16985 EMSG(_("E240: No connection to Vim server"));
16986 return FAIL;
16987 }
16988 return OK;
16989}
16990#endif
16991
16992#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016993static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994
16995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016996remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997{
16998 char_u *server_name;
16999 char_u *keys;
17000 char_u *r = NULL;
17001 char_u buf[NUMBUFLEN];
17002# ifdef WIN32
17003 HWND w;
17004# else
17005 Window w;
17006# endif
17007
17008 if (check_restricted() || check_secure())
17009 return;
17010
17011# ifdef FEAT_X11
17012 if (check_connection() == FAIL)
17013 return;
17014# endif
17015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017016 server_name = get_tv_string_chk(&argvars[0]);
17017 if (server_name == NULL)
17018 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 keys = get_tv_string_buf(&argvars[1], buf);
17020# ifdef WIN32
17021 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17022# else
17023 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17024 < 0)
17025# endif
17026 {
17027 if (r != NULL)
17028 EMSG(r); /* sending worked but evaluation failed */
17029 else
17030 EMSG2(_("E241: Unable to send to %s"), server_name);
17031 return;
17032 }
17033
17034 rettv->vval.v_string = r;
17035
17036 if (argvars[2].v_type != VAR_UNKNOWN)
17037 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017039 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017042 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 v.di_tv.v_type = VAR_STRING;
17044 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 idvar = get_tv_string_chk(&argvars[2]);
17046 if (idvar != NULL)
17047 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 }
17050}
17051#endif
17052
17053/*
17054 * "remote_expr()" function
17055 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017057f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058{
17059 rettv->v_type = VAR_STRING;
17060 rettv->vval.v_string = NULL;
17061#ifdef FEAT_CLIENTSERVER
17062 remote_common(argvars, rettv, TRUE);
17063#endif
17064}
17065
17066/*
17067 * "remote_foreground()" function
17068 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017070f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072#ifdef FEAT_CLIENTSERVER
17073# ifdef WIN32
17074 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 {
17076 char_u *server_name = get_tv_string_chk(&argvars[0]);
17077
17078 if (server_name != NULL)
17079 serverForeground(server_name);
17080 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081# else
17082 /* Send a foreground() expression to the server. */
17083 argvars[1].v_type = VAR_STRING;
17084 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17085 argvars[2].v_type = VAR_UNKNOWN;
17086 remote_common(argvars, rettv, TRUE);
17087 vim_free(argvars[1].vval.v_string);
17088# endif
17089#endif
17090}
17091
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017093f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094{
17095#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 char_u *s = NULL;
17098# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017099 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102
17103 if (check_restricted() || check_secure())
17104 {
17105 rettv->vval.v_number = -1;
17106 return;
17107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017108 serverid = get_tv_string_chk(&argvars[0]);
17109 if (serverid == NULL)
17110 {
17111 rettv->vval.v_number = -1;
17112 return; /* type error; errmsg already given */
17113 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017115 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 if (n == 0)
17117 rettv->vval.v_number = -1;
17118 else
17119 {
17120 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17121 rettv->vval.v_number = (s != NULL);
17122 }
17123# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 if (check_connection() == FAIL)
17125 return;
17126
17127 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017128 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129# endif
17130
17131 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 char_u *retvar;
17134
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 v.di_tv.v_type = VAR_STRING;
17136 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 retvar = get_tv_string_chk(&argvars[1]);
17138 if (retvar != NULL)
17139 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017140 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 }
17142#else
17143 rettv->vval.v_number = -1;
17144#endif
17145}
17146
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017148f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149{
17150 char_u *r = NULL;
17151
17152#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 char_u *serverid = get_tv_string_chk(&argvars[0]);
17154
17155 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 {
17157# ifdef WIN32
17158 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017159 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017161 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 if (n != 0)
17163 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17164 if (r == NULL)
17165# else
17166 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168# endif
17169 EMSG(_("E277: Unable to read a server reply"));
17170 }
17171#endif
17172 rettv->v_type = VAR_STRING;
17173 rettv->vval.v_string = r;
17174}
17175
17176/*
17177 * "remote_send()" function
17178 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017180f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181{
17182 rettv->v_type = VAR_STRING;
17183 rettv->vval.v_string = NULL;
17184#ifdef FEAT_CLIENTSERVER
17185 remote_common(argvars, rettv, FALSE);
17186#endif
17187}
17188
17189/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017190 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017191 */
17192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017193f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194{
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 list_T *l;
17196 listitem_T *item, *item2;
17197 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017198 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017199 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017200 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017201 dict_T *d;
17202 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017203 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017204
Bram Moolenaar8c711452005-01-14 21:53:12 +000017205 if (argvars[0].v_type == VAR_DICT)
17206 {
17207 if (argvars[2].v_type != VAR_UNKNOWN)
17208 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017209 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017210 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 key = get_tv_string_chk(&argvars[1]);
17213 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 di = dict_find(d, key, -1);
17216 if (di == NULL)
17217 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017218 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17219 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017220 {
17221 *rettv = di->di_tv;
17222 init_tv(&di->di_tv);
17223 dictitem_remove(d, di);
17224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017226 }
17227 }
17228 else if (argvars[0].v_type != VAR_LIST)
17229 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017230 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017231 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 int error = FALSE;
17234
17235 idx = get_tv_number_chk(&argvars[1], &error);
17236 if (error)
17237 ; /* type error: do nothing, errmsg already given */
17238 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017239 EMSGN(_(e_listidx), idx);
17240 else
17241 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017242 if (argvars[2].v_type == VAR_UNKNOWN)
17243 {
17244 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017245 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017246 *rettv = item->li_tv;
17247 vim_free(item);
17248 }
17249 else
17250 {
17251 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 end = get_tv_number_chk(&argvars[2], &error);
17253 if (error)
17254 ; /* type error: do nothing */
17255 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 EMSGN(_(e_listidx), end);
17257 else
17258 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017259 int cnt = 0;
17260
17261 for (li = item; li != NULL; li = li->li_next)
17262 {
17263 ++cnt;
17264 if (li == item2)
17265 break;
17266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017267 if (li == NULL) /* didn't find "item2" after "item" */
17268 EMSG(_(e_invrange));
17269 else
17270 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017271 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017272 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017274 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017275 l->lv_first = item;
17276 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017277 item->li_prev = NULL;
17278 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017279 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017280 }
17281 }
17282 }
17283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017284 }
17285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286}
17287
17288/*
17289 * "rename({from}, {to})" function
17290 */
17291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017292f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293{
17294 char_u buf[NUMBUFLEN];
17295
17296 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17300 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301}
17302
17303/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017304 * "repeat()" function
17305 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017307f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017308{
17309 char_u *p;
17310 int n;
17311 int slen;
17312 int len;
17313 char_u *r;
17314 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017315
17316 n = get_tv_number(&argvars[1]);
17317 if (argvars[0].v_type == VAR_LIST)
17318 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017319 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017320 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017321 if (list_extend(rettv->vval.v_list,
17322 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017324 }
17325 else
17326 {
17327 p = get_tv_string(&argvars[0]);
17328 rettv->v_type = VAR_STRING;
17329 rettv->vval.v_string = NULL;
17330
17331 slen = (int)STRLEN(p);
17332 len = slen * n;
17333 if (len <= 0)
17334 return;
17335
17336 r = alloc(len + 1);
17337 if (r != NULL)
17338 {
17339 for (i = 0; i < n; i++)
17340 mch_memmove(r + i * slen, p, (size_t)slen);
17341 r[len] = NUL;
17342 }
17343
17344 rettv->vval.v_string = r;
17345 }
17346}
17347
17348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 * "resolve()" function
17350 */
17351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017352f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353{
17354 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017355#ifdef HAVE_READLINK
17356 char_u *buf = NULL;
17357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360#ifdef FEAT_SHORTCUT
17361 {
17362 char_u *v = NULL;
17363
17364 v = mch_resolve_shortcut(p);
17365 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 }
17370#else
17371# ifdef HAVE_READLINK
17372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 char_u *cpy;
17374 int len;
17375 char_u *remain = NULL;
17376 char_u *q;
17377 int is_relative_to_current = FALSE;
17378 int has_trailing_pathsep = FALSE;
17379 int limit = 100;
17380
17381 p = vim_strsave(p);
17382
17383 if (p[0] == '.' && (vim_ispathsep(p[1])
17384 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17385 is_relative_to_current = TRUE;
17386
17387 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017388 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017391 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393
17394 q = getnextcomp(p);
17395 if (*q != NUL)
17396 {
17397 /* Separate the first path component in "p", and keep the
17398 * remainder (beginning with the path separator). */
17399 remain = vim_strsave(q - 1);
17400 q[-1] = NUL;
17401 }
17402
Bram Moolenaard9462e32011-04-11 21:35:11 +020017403 buf = alloc(MAXPATHL + 1);
17404 if (buf == NULL)
17405 goto fail;
17406
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 for (;;)
17408 {
17409 for (;;)
17410 {
17411 len = readlink((char *)p, (char *)buf, MAXPATHL);
17412 if (len <= 0)
17413 break;
17414 buf[len] = NUL;
17415
17416 if (limit-- == 0)
17417 {
17418 vim_free(p);
17419 vim_free(remain);
17420 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 goto fail;
17423 }
17424
17425 /* Ensure that the result will have a trailing path separator
17426 * if the argument has one. */
17427 if (remain == NULL && has_trailing_pathsep)
17428 add_pathsep(buf);
17429
17430 /* Separate the first path component in the link value and
17431 * concatenate the remainders. */
17432 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17433 if (*q != NUL)
17434 {
17435 if (remain == NULL)
17436 remain = vim_strsave(q - 1);
17437 else
17438 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017439 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 if (cpy != NULL)
17441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 vim_free(remain);
17443 remain = cpy;
17444 }
17445 }
17446 q[-1] = NUL;
17447 }
17448
17449 q = gettail(p);
17450 if (q > p && *q == NUL)
17451 {
17452 /* Ignore trailing path separator. */
17453 q[-1] = NUL;
17454 q = gettail(p);
17455 }
17456 if (q > p && !mch_isFullName(buf))
17457 {
17458 /* symlink is relative to directory of argument */
17459 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17460 if (cpy != NULL)
17461 {
17462 STRCPY(cpy, p);
17463 STRCPY(gettail(cpy), buf);
17464 vim_free(p);
17465 p = cpy;
17466 }
17467 }
17468 else
17469 {
17470 vim_free(p);
17471 p = vim_strsave(buf);
17472 }
17473 }
17474
17475 if (remain == NULL)
17476 break;
17477
17478 /* Append the first path component of "remain" to "p". */
17479 q = getnextcomp(remain + 1);
17480 len = q - remain - (*q != NUL);
17481 cpy = vim_strnsave(p, STRLEN(p) + len);
17482 if (cpy != NULL)
17483 {
17484 STRNCAT(cpy, remain, len);
17485 vim_free(p);
17486 p = cpy;
17487 }
17488 /* Shorten "remain". */
17489 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017490 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 else
17492 {
17493 vim_free(remain);
17494 remain = NULL;
17495 }
17496 }
17497
17498 /* If the result is a relative path name, make it explicitly relative to
17499 * the current directory if and only if the argument had this form. */
17500 if (!vim_ispathsep(*p))
17501 {
17502 if (is_relative_to_current
17503 && *p != NUL
17504 && !(p[0] == '.'
17505 && (p[1] == NUL
17506 || vim_ispathsep(p[1])
17507 || (p[1] == '.'
17508 && (p[2] == NUL
17509 || vim_ispathsep(p[2]))))))
17510 {
17511 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 if (cpy != NULL)
17514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 vim_free(p);
17516 p = cpy;
17517 }
17518 }
17519 else if (!is_relative_to_current)
17520 {
17521 /* Strip leading "./". */
17522 q = p;
17523 while (q[0] == '.' && vim_ispathsep(q[1]))
17524 q += 2;
17525 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017526 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 }
17528 }
17529
17530 /* Ensure that the result will have no trailing path separator
17531 * if the argument had none. But keep "/" or "//". */
17532 if (!has_trailing_pathsep)
17533 {
17534 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017535 if (after_pathsep(p, q))
17536 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 }
17538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 }
17541# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543# endif
17544#endif
17545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
17548#ifdef HAVE_READLINK
17549fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017550 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553}
17554
17555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 */
17558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017559f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560{
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 list_T *l;
17562 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 if (argvars[0].v_type != VAR_LIST)
17565 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017566 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017567 && !tv_check_lock(l->lv_lock,
17568 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 {
17570 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017571 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017572 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 while (li != NULL)
17574 {
17575 ni = li->li_prev;
17576 list_append(l, li);
17577 li = ni;
17578 }
17579 rettv->vval.v_list = l;
17580 rettv->v_type = VAR_LIST;
17581 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017582 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584}
17585
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017586#define SP_NOMOVE 0x01 /* don't move cursor */
17587#define SP_REPEAT 0x02 /* repeat to find outer pair */
17588#define SP_RETCOUNT 0x04 /* return matchcount */
17589#define SP_SETPCMARK 0x08 /* set previous context mark */
17590#define SP_START 0x10 /* accept match at start position */
17591#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17592#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017593#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017594
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017595static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596
17597/*
17598 * Get flags for a search function.
17599 * Possibly sets "p_ws".
17600 * Returns BACKWARD, FORWARD or zero (for an error).
17601 */
17602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017603get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604{
17605 int dir = FORWARD;
17606 char_u *flags;
17607 char_u nbuf[NUMBUFLEN];
17608 int mask;
17609
17610 if (varp->v_type != VAR_UNKNOWN)
17611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017612 flags = get_tv_string_buf_chk(varp, nbuf);
17613 if (flags == NULL)
17614 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 while (*flags != NUL)
17616 {
17617 switch (*flags)
17618 {
17619 case 'b': dir = BACKWARD; break;
17620 case 'w': p_ws = TRUE; break;
17621 case 'W': p_ws = FALSE; break;
17622 default: mask = 0;
17623 if (flagsp != NULL)
17624 switch (*flags)
17625 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017627 case 'e': mask = SP_END; break;
17628 case 'm': mask = SP_RETCOUNT; break;
17629 case 'n': mask = SP_NOMOVE; break;
17630 case 'p': mask = SP_SUBPAT; break;
17631 case 'r': mask = SP_REPEAT; break;
17632 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017633 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 }
17635 if (mask == 0)
17636 {
17637 EMSG2(_(e_invarg2), flags);
17638 dir = 0;
17639 }
17640 else
17641 *flagsp |= mask;
17642 }
17643 if (dir == 0)
17644 break;
17645 ++flags;
17646 }
17647 }
17648 return dir;
17649}
17650
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017652 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017655search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017657 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 char_u *pat;
17659 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017660 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 int save_p_ws = p_ws;
17662 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017663 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017664 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017665 proftime_T tm;
17666#ifdef FEAT_RELTIME
17667 long time_limit = 0;
17668#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017670 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017673 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017674 if (dir == 0)
17675 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017676 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017677 if (flags & SP_START)
17678 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017679 if (flags & SP_END)
17680 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017681 if (flags & SP_COLUMN)
17682 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017683
Bram Moolenaar76929292008-01-06 19:07:36 +000017684 /* Optional arguments: line number to stop searching and timeout. */
17685 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017686 {
17687 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17688 if (lnum_stop < 0)
17689 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017690#ifdef FEAT_RELTIME
17691 if (argvars[3].v_type != VAR_UNKNOWN)
17692 {
17693 time_limit = get_tv_number_chk(&argvars[3], NULL);
17694 if (time_limit < 0)
17695 goto theend;
17696 }
17697#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017698 }
17699
Bram Moolenaar76929292008-01-06 19:07:36 +000017700#ifdef FEAT_RELTIME
17701 /* Set the time limit, if there is one. */
17702 profile_setlimit(time_limit, &tm);
17703#endif
17704
Bram Moolenaar231334e2005-07-25 20:46:57 +000017705 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017706 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017707 * Check to make sure only those flags are set.
17708 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17709 * flags cannot be set. Check for that condition also.
17710 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017711 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017712 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017715 goto theend;
17716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017718 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017719 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017720 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017721 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017723 if (flags & SP_SUBPAT)
17724 retval = subpatnum;
17725 else
17726 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017727 if (flags & SP_SETPCMARK)
17728 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017730 if (match_pos != NULL)
17731 {
17732 /* Store the match cursor position */
17733 match_pos->lnum = pos.lnum;
17734 match_pos->col = pos.col + 1;
17735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 /* "/$" will put the cursor after the end of the line, may need to
17737 * correct that here */
17738 check_cursor();
17739 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017740
17741 /* If 'n' flag is used: restore cursor position. */
17742 if (flags & SP_NOMOVE)
17743 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017744 else
17745 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017746theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017748
17749 return retval;
17750}
17751
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017753
17754/*
17755 * round() is not in C90, use ceil() or floor() instead.
17756 */
17757 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017758vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017759{
17760 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17761}
17762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017763/*
17764 * "round({float})" function
17765 */
17766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017767f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017769 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017770
17771 rettv->v_type = VAR_FLOAT;
17772 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017773 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017774 else
17775 rettv->vval.v_float = 0.0;
17776}
17777#endif
17778
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017779/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017780 * "screenattr()" function
17781 */
17782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017783f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017784{
17785 int row;
17786 int col;
17787 int c;
17788
17789 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17790 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17791 if (row < 0 || row >= screen_Rows
17792 || col < 0 || col >= screen_Columns)
17793 c = -1;
17794 else
17795 c = ScreenAttrs[LineOffset[row] + col];
17796 rettv->vval.v_number = c;
17797}
17798
17799/*
17800 * "screenchar()" function
17801 */
17802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017803f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017804{
17805 int row;
17806 int col;
17807 int off;
17808 int c;
17809
17810 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17811 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17812 if (row < 0 || row >= screen_Rows
17813 || col < 0 || col >= screen_Columns)
17814 c = -1;
17815 else
17816 {
17817 off = LineOffset[row] + col;
17818#ifdef FEAT_MBYTE
17819 if (enc_utf8 && ScreenLinesUC[off] != 0)
17820 c = ScreenLinesUC[off];
17821 else
17822#endif
17823 c = ScreenLines[off];
17824 }
17825 rettv->vval.v_number = c;
17826}
17827
17828/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017829 * "screencol()" function
17830 *
17831 * First column is 1 to be consistent with virtcol().
17832 */
17833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017835{
17836 rettv->vval.v_number = screen_screencol() + 1;
17837}
17838
17839/*
17840 * "screenrow()" function
17841 */
17842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017844{
17845 rettv->vval.v_number = screen_screenrow() + 1;
17846}
17847
17848/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017849 * "search()" function
17850 */
17851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017852f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017853{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017854 int flags = 0;
17855
17856 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857}
17858
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017860 * "searchdecl()" function
17861 */
17862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017863f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017864{
17865 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017866 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017867 int error = FALSE;
17868 char_u *name;
17869
17870 rettv->vval.v_number = 1; /* default: FAIL */
17871
17872 name = get_tv_string_chk(&argvars[0]);
17873 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017874 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017875 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017876 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17877 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17878 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017879 if (!error && name != NULL)
17880 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017881 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017882}
17883
17884/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017885 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017888searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889{
17890 char_u *spat, *mpat, *epat;
17891 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 int dir;
17894 int flags = 0;
17895 char_u nbuf1[NUMBUFLEN];
17896 char_u nbuf2[NUMBUFLEN];
17897 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017898 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017899 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017900 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 spat = get_tv_string_chk(&argvars[0]);
17904 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17905 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17906 if (spat == NULL || mpat == NULL || epat == NULL)
17907 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 /* Handle the optional fourth argument: flags */
17910 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017911 if (dir == 0)
17912 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017913
17914 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017915 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17916 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017917 if ((flags & (SP_END | SP_SUBPAT)) != 0
17918 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017919 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017920 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017921 goto theend;
17922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017924 /* Using 'r' implies 'W', otherwise it doesn't work. */
17925 if (flags & SP_REPEAT)
17926 p_ws = FALSE;
17927
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017928 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017929 if (argvars[3].v_type == VAR_UNKNOWN
17930 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 skip = (char_u *)"";
17932 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017935 if (argvars[5].v_type != VAR_UNKNOWN)
17936 {
17937 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17938 if (lnum_stop < 0)
17939 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017940#ifdef FEAT_RELTIME
17941 if (argvars[6].v_type != VAR_UNKNOWN)
17942 {
17943 time_limit = get_tv_number_chk(&argvars[6], NULL);
17944 if (time_limit < 0)
17945 goto theend;
17946 }
17947#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017948 }
17949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017950 if (skip == NULL)
17951 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017953 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017954 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017955
17956theend:
17957 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958
17959 return retval;
17960}
17961
17962/*
17963 * "searchpair()" function
17964 */
17965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017966f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017967{
17968 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17969}
17970
17971/*
17972 * "searchpairpos()" function
17973 */
17974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017975f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017976{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977 pos_T match_pos;
17978 int lnum = 0;
17979 int col = 0;
17980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983
17984 if (searchpair_cmn(argvars, &match_pos) > 0)
17985 {
17986 lnum = match_pos.lnum;
17987 col = match_pos.col;
17988 }
17989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017990 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17991 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017992}
17993
17994/*
17995 * Search for a start/middle/end thing.
17996 * Used by searchpair(), see its documentation for the details.
17997 * Returns 0 or -1 for no match,
17998 */
17999 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018000do_searchpair(
18001 char_u *spat, /* start pattern */
18002 char_u *mpat, /* middle pattern */
18003 char_u *epat, /* end pattern */
18004 int dir, /* BACKWARD or FORWARD */
18005 char_u *skip, /* skip expression */
18006 int flags, /* SP_SETPCMARK and other SP_ values */
18007 pos_T *match_pos,
18008 linenr_T lnum_stop, /* stop at this line if not zero */
18009 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018010{
18011 char_u *save_cpo;
18012 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18013 long retval = 0;
18014 pos_T pos;
18015 pos_T firstpos;
18016 pos_T foundpos;
18017 pos_T save_cursor;
18018 pos_T save_pos;
18019 int n;
18020 int r;
18021 int nest = 1;
18022 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018023 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018024 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018025
18026 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18027 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018028 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018029
Bram Moolenaar76929292008-01-06 19:07:36 +000018030#ifdef FEAT_RELTIME
18031 /* Set the time limit, if there is one. */
18032 profile_setlimit(time_limit, &tm);
18033#endif
18034
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018035 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18036 * start/middle/end (pat3, for the top pair). */
18037 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18038 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18039 if (pat2 == NULL || pat3 == NULL)
18040 goto theend;
18041 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18042 if (*mpat == NUL)
18043 STRCPY(pat3, pat2);
18044 else
18045 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18046 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018047 if (flags & SP_START)
18048 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018049
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 save_cursor = curwin->w_cursor;
18051 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018052 clearpos(&firstpos);
18053 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 pat = pat3;
18055 for (;;)
18056 {
18057 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018058 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18060 /* didn't find it or found the first match again: FAIL */
18061 break;
18062
18063 if (firstpos.lnum == 0)
18064 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018065 if (equalpos(pos, foundpos))
18066 {
18067 /* Found the same position again. Can happen with a pattern that
18068 * has "\zs" at the end and searching backwards. Advance one
18069 * character and try again. */
18070 if (dir == BACKWARD)
18071 decl(&pos);
18072 else
18073 incl(&pos);
18074 }
18075 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018077 /* clear the start flag to avoid getting stuck here */
18078 options &= ~SEARCH_START;
18079
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 /* If the skip pattern matches, ignore this match. */
18081 if (*skip != NUL)
18082 {
18083 save_pos = curwin->w_cursor;
18084 curwin->w_cursor = pos;
18085 r = eval_to_bool(skip, &err, NULL, FALSE);
18086 curwin->w_cursor = save_pos;
18087 if (err)
18088 {
18089 /* Evaluating {skip} caused an error, break here. */
18090 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018091 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 break;
18093 }
18094 if (r)
18095 continue;
18096 }
18097
18098 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18099 {
18100 /* Found end when searching backwards or start when searching
18101 * forward: nested pair. */
18102 ++nest;
18103 pat = pat2; /* nested, don't search for middle */
18104 }
18105 else
18106 {
18107 /* Found end when searching forward or start when searching
18108 * backward: end of (nested) pair; or found middle in outer pair. */
18109 if (--nest == 1)
18110 pat = pat3; /* outer level, search for middle */
18111 }
18112
18113 if (nest == 0)
18114 {
18115 /* Found the match: return matchcount or line number. */
18116 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018117 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018119 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018120 if (flags & SP_SETPCMARK)
18121 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 curwin->w_cursor = pos;
18123 if (!(flags & SP_REPEAT))
18124 break;
18125 nest = 1; /* search for next unmatched */
18126 }
18127 }
18128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018129 if (match_pos != NULL)
18130 {
18131 /* Store the match cursor position */
18132 match_pos->lnum = curwin->w_cursor.lnum;
18133 match_pos->col = curwin->w_cursor.col + 1;
18134 }
18135
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018137 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 curwin->w_cursor = save_cursor;
18139
18140theend:
18141 vim_free(pat2);
18142 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018143 if (p_cpo == empty_option)
18144 p_cpo = save_cpo;
18145 else
18146 /* Darn, evaluating the {skip} expression changed the value. */
18147 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018148
18149 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150}
18151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018152/*
18153 * "searchpos()" function
18154 */
18155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018156f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018157{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018158 pos_T match_pos;
18159 int lnum = 0;
18160 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018161 int n;
18162 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018164 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018165 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018166
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018167 n = search_cmn(argvars, &match_pos, &flags);
18168 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018169 {
18170 lnum = match_pos.lnum;
18171 col = match_pos.col;
18172 }
18173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018174 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18175 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018176 if (flags & SP_SUBPAT)
18177 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018178}
18179
Bram Moolenaar0d660222005-01-07 21:51:51 +000018180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018181f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183#ifdef FEAT_CLIENTSERVER
18184 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018185 char_u *server = get_tv_string_chk(&argvars[0]);
18186 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187
Bram Moolenaar0d660222005-01-07 21:51:51 +000018188 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018189 if (server == NULL || reply == NULL)
18190 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 if (check_restricted() || check_secure())
18192 return;
18193# ifdef FEAT_X11
18194 if (check_connection() == FAIL)
18195 return;
18196# endif
18197
18198 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200 EMSG(_("E258: Unable to send to client"));
18201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 rettv->vval.v_number = 0;
18204#else
18205 rettv->vval.v_number = -1;
18206#endif
18207}
18208
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018210f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018211{
18212 char_u *r = NULL;
18213
18214#ifdef FEAT_CLIENTSERVER
18215# ifdef WIN32
18216 r = serverGetVimNames();
18217# else
18218 make_connection();
18219 if (X_DISPLAY != NULL)
18220 r = serverGetVimNames(X_DISPLAY);
18221# endif
18222#endif
18223 rettv->v_type = VAR_STRING;
18224 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225}
18226
18227/*
18228 * "setbufvar()" function
18229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018231f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232{
18233 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018236 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 char_u nbuf[NUMBUFLEN];
18238
18239 if (check_restricted() || check_secure())
18240 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18242 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018243 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 varp = &argvars[2];
18245
18246 if (buf != NULL && varname != NULL && varp != NULL)
18247 {
18248 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250
18251 if (*varname == '&')
18252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018253 long numval;
18254 char_u *strval;
18255 int error = FALSE;
18256
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018258 numval = get_tv_number_chk(varp, &error);
18259 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 if (!error && strval != NULL)
18261 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 }
18263 else
18264 {
18265 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18266 if (bufvarname != NULL)
18267 {
18268 STRCPY(bufvarname, "b:");
18269 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018270 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 vim_free(bufvarname);
18272 }
18273 }
18274
18275 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278}
18279
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018281f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018282{
18283 dict_T *d;
18284 dictitem_T *di;
18285 char_u *csearch;
18286
18287 if (argvars[0].v_type != VAR_DICT)
18288 {
18289 EMSG(_(e_dictreq));
18290 return;
18291 }
18292
18293 if ((d = argvars[0].vval.v_dict) != NULL)
18294 {
18295 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18296 if (csearch != NULL)
18297 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018298#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018299 if (enc_utf8)
18300 {
18301 int pcc[MAX_MCO];
18302 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018303
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018304 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18305 }
18306 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018307#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018308 set_last_csearch(PTR2CHAR(csearch),
18309 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018310 }
18311
18312 di = dict_find(d, (char_u *)"forward", -1);
18313 if (di != NULL)
18314 set_csearch_direction(get_tv_number(&di->di_tv)
18315 ? FORWARD : BACKWARD);
18316
18317 di = dict_find(d, (char_u *)"until", -1);
18318 if (di != NULL)
18319 set_csearch_until(!!get_tv_number(&di->di_tv));
18320 }
18321}
18322
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323/*
18324 * "setcmdpos()" function
18325 */
18326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018329 int pos = (int)get_tv_number(&argvars[0]) - 1;
18330
18331 if (pos >= 0)
18332 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333}
18334
18335/*
18336 * "setline()" function
18337 */
18338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018339f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340{
18341 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018342 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018343 list_T *l = NULL;
18344 listitem_T *li = NULL;
18345 long added = 0;
18346 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018348 lnum = get_tv_lnum(&argvars[0]);
18349 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018351 l = argvars[1].vval.v_list;
18352 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018354 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018355 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018356
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018357 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018358 for (;;)
18359 {
18360 if (l != NULL)
18361 {
18362 /* list argument, get next string */
18363 if (li == NULL)
18364 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018365 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018366 li = li->li_next;
18367 }
18368
18369 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018370 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018371 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018372
18373 /* When coming here from Insert mode, sync undo, so that this can be
18374 * undone separately from what was previously inserted. */
18375 if (u_sync_once == 2)
18376 {
18377 u_sync_once = 1; /* notify that u_sync() was called */
18378 u_sync(TRUE);
18379 }
18380
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018381 if (lnum <= curbuf->b_ml.ml_line_count)
18382 {
18383 /* existing line, replace it */
18384 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18385 {
18386 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018387 if (lnum == curwin->w_cursor.lnum)
18388 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018389 rettv->vval.v_number = 0; /* OK */
18390 }
18391 }
18392 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18393 {
18394 /* lnum is one past the last line, append the line */
18395 ++added;
18396 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18397 rettv->vval.v_number = 0; /* OK */
18398 }
18399
18400 if (l == NULL) /* only one string argument */
18401 break;
18402 ++lnum;
18403 }
18404
18405 if (added > 0)
18406 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407}
18408
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018409static 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 +000018410
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018412 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018413 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018415set_qf_ll_list(
18416 win_T *wp UNUSED,
18417 typval_T *list_arg UNUSED,
18418 typval_T *action_arg UNUSED,
18419 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018420{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018421#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018422 char_u *act;
18423 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018424#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018425
Bram Moolenaar2641f772005-03-25 21:58:17 +000018426 rettv->vval.v_number = -1;
18427
18428#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018429 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018430 EMSG(_(e_listreq));
18431 else
18432 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018433 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018434
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018435 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018436 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018437 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018438 if (act == NULL)
18439 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018440 if (*act == 'a' || *act == 'r')
18441 action = *act;
18442 }
18443
Bram Moolenaar81484f42012-12-05 15:16:47 +010018444 if (l != NULL && set_errorlist(wp, l, action,
18445 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018446 rettv->vval.v_number = 0;
18447 }
18448#endif
18449}
18450
18451/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018452 * "setloclist()" function
18453 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018455f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018456{
18457 win_T *win;
18458
18459 rettv->vval.v_number = -1;
18460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018461 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018462 if (win != NULL)
18463 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18464}
18465
18466/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018467 * "setmatches()" function
18468 */
18469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018470f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018471{
18472#ifdef FEAT_SEARCH_EXTRA
18473 list_T *l;
18474 listitem_T *li;
18475 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018476 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018477
18478 rettv->vval.v_number = -1;
18479 if (argvars[0].v_type != VAR_LIST)
18480 {
18481 EMSG(_(e_listreq));
18482 return;
18483 }
18484 if ((l = argvars[0].vval.v_list) != NULL)
18485 {
18486
18487 /* To some extent make sure that we are dealing with a list from
18488 * "getmatches()". */
18489 li = l->lv_first;
18490 while (li != NULL)
18491 {
18492 if (li->li_tv.v_type != VAR_DICT
18493 || (d = li->li_tv.vval.v_dict) == NULL)
18494 {
18495 EMSG(_(e_invarg));
18496 return;
18497 }
18498 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018499 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18500 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018501 && dict_find(d, (char_u *)"priority", -1) != NULL
18502 && dict_find(d, (char_u *)"id", -1) != NULL))
18503 {
18504 EMSG(_(e_invarg));
18505 return;
18506 }
18507 li = li->li_next;
18508 }
18509
18510 clear_matches(curwin);
18511 li = l->lv_first;
18512 while (li != NULL)
18513 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018514 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018515 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018516 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018517 char_u *group;
18518 int priority;
18519 int id;
18520 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018521
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018522 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018523 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18524 {
18525 if (s == NULL)
18526 {
18527 s = list_alloc();
18528 if (s == NULL)
18529 return;
18530 }
18531
18532 /* match from matchaddpos() */
18533 for (i = 1; i < 9; i++)
18534 {
18535 sprintf((char *)buf, (char *)"pos%d", i);
18536 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18537 {
18538 if (di->di_tv.v_type != VAR_LIST)
18539 return;
18540
18541 list_append_tv(s, &di->di_tv);
18542 s->lv_refcount++;
18543 }
18544 else
18545 break;
18546 }
18547 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018548
18549 group = get_dict_string(d, (char_u *)"group", FALSE);
18550 priority = (int)get_dict_number(d, (char_u *)"priority");
18551 id = (int)get_dict_number(d, (char_u *)"id");
18552 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18553 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18554 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018555 if (i == 0)
18556 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018557 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018558 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018559 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018560 }
18561 else
18562 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018563 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018564 list_unref(s);
18565 s = NULL;
18566 }
18567
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018568 li = li->li_next;
18569 }
18570 rettv->vval.v_number = 0;
18571 }
18572#endif
18573}
18574
18575/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018576 * "setpos()" function
18577 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018580{
18581 pos_T pos;
18582 int fnum;
18583 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018584 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018585
Bram Moolenaar08250432008-02-13 11:42:46 +000018586 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018587 name = get_tv_string_chk(argvars);
18588 if (name != NULL)
18589 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018590 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018591 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018592 if (--pos.col < 0)
18593 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018594 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018595 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018596 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018597 if (fnum == curbuf->b_fnum)
18598 {
18599 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018600 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018601 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018602 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018603 curwin->w_set_curswant = FALSE;
18604 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018605 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018606 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018607 }
18608 else
18609 EMSG(_(e_invarg));
18610 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018611 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18612 {
18613 /* set mark */
18614 if (setmark_pos(name[1], &pos, fnum) == OK)
18615 rettv->vval.v_number = 0;
18616 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018617 else
18618 EMSG(_(e_invarg));
18619 }
18620 }
18621}
18622
18623/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018624 * "setqflist()" function
18625 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018627f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018628{
18629 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18630}
18631
18632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 * "setreg()" function
18634 */
18635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018636f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
18638 int regname;
18639 char_u *strregname;
18640 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018641 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 int append;
18643 char_u yank_type;
18644 long block_len;
18645
18646 block_len = -1;
18647 yank_type = MAUTO;
18648 append = FALSE;
18649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018650 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018653 if (strregname == NULL)
18654 return; /* type error; errmsg already given */
18655 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 if (regname == 0 || regname == '@')
18657 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018661 stropt = get_tv_string_chk(&argvars[2]);
18662 if (stropt == NULL)
18663 return; /* type error */
18664 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 switch (*stropt)
18666 {
18667 case 'a': case 'A': /* append */
18668 append = TRUE;
18669 break;
18670 case 'v': case 'c': /* character-wise selection */
18671 yank_type = MCHAR;
18672 break;
18673 case 'V': case 'l': /* line-wise selection */
18674 yank_type = MLINE;
18675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 case 'b': case Ctrl_V: /* block-wise selection */
18677 yank_type = MBLOCK;
18678 if (VIM_ISDIGIT(stropt[1]))
18679 {
18680 ++stropt;
18681 block_len = getdigits(&stropt) - 1;
18682 --stropt;
18683 }
18684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 }
18686 }
18687
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018688 if (argvars[1].v_type == VAR_LIST)
18689 {
18690 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018691 char_u **allocval;
18692 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018693 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018694 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018695 int len = argvars[1].vval.v_list->lv_len;
18696 listitem_T *li;
18697
Bram Moolenaar7d647822014-04-05 21:28:56 +020018698 /* First half: use for pointers to result lines; second half: use for
18699 * pointers to allocated copies. */
18700 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018701 if (lstval == NULL)
18702 return;
18703 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018704 allocval = lstval + len + 2;
18705 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018706
18707 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18708 li = li->li_next)
18709 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018710 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018711 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018712 goto free_lstval;
18713 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018714 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018715 /* Need to make a copy, next get_tv_string_buf_chk() will
18716 * overwrite the string. */
18717 strval = vim_strsave(buf);
18718 if (strval == NULL)
18719 goto free_lstval;
18720 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018721 }
18722 *curval++ = strval;
18723 }
18724 *curval++ = NULL;
18725
18726 write_reg_contents_lst(regname, lstval, -1,
18727 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018728free_lstval:
18729 while (curallocval > allocval)
18730 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018731 vim_free(lstval);
18732 }
18733 else
18734 {
18735 strval = get_tv_string_chk(&argvars[1]);
18736 if (strval == NULL)
18737 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018738 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742}
18743
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018744/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018745 * "settabvar()" function
18746 */
18747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018748f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018749{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018750#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018751 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018752 tabpage_T *tp;
18753#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018754 char_u *varname, *tabvarname;
18755 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018756
18757 rettv->vval.v_number = 0;
18758
18759 if (check_restricted() || check_secure())
18760 return;
18761
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018762#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018763 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018764#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018765 varname = get_tv_string_chk(&argvars[1]);
18766 varp = &argvars[2];
18767
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018768 if (varname != NULL && varp != NULL
18769#ifdef FEAT_WINDOWS
18770 && tp != NULL
18771#endif
18772 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018773 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018774#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018775 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018776 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018777#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018778
18779 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18780 if (tabvarname != NULL)
18781 {
18782 STRCPY(tabvarname, "t:");
18783 STRCPY(tabvarname + 2, varname);
18784 set_var(tabvarname, varp, TRUE);
18785 vim_free(tabvarname);
18786 }
18787
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018788#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018789 /* Restore current tabpage */
18790 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018791 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018792#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018793 }
18794}
18795
18796/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018797 * "settabwinvar()" function
18798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018801{
18802 setwinvar(argvars, rettv, 1);
18803}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804
18805/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018806 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018809f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018811 setwinvar(argvars, rettv, 0);
18812}
18813
18814/*
18815 * "setwinvar()" and "settabwinvar()" functions
18816 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018817
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018819setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018820{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 win_T *win;
18822#ifdef FEAT_WINDOWS
18823 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018824 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018825 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826#endif
18827 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018830 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831
18832 if (check_restricted() || check_secure())
18833 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018834
18835#ifdef FEAT_WINDOWS
18836 if (off == 1)
18837 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18838 else
18839 tp = curtab;
18840#endif
18841 win = find_win_by_nr(&argvars[off], tp);
18842 varname = get_tv_string_chk(&argvars[off + 1]);
18843 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844
18845 if (win != NULL && varname != NULL && varp != NULL)
18846 {
18847#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018848 need_switch_win = !(tp == curtab && win == curwin);
18849 if (!need_switch_win
18850 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018853 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018855 long numval;
18856 char_u *strval;
18857 int error = FALSE;
18858
18859 ++varname;
18860 numval = get_tv_number_chk(varp, &error);
18861 strval = get_tv_string_buf_chk(varp, nbuf);
18862 if (!error && strval != NULL)
18863 set_option_value(varname, numval, strval, OPT_LOCAL);
18864 }
18865 else
18866 {
18867 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18868 if (winvarname != NULL)
18869 {
18870 STRCPY(winvarname, "w:");
18871 STRCPY(winvarname + 2, varname);
18872 set_var(winvarname, varp, TRUE);
18873 vim_free(winvarname);
18874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 }
18876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018878 if (need_switch_win)
18879 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880#endif
18881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882}
18883
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018884#ifdef FEAT_CRYPT
18885/*
18886 * "sha256({string})" function
18887 */
18888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018889f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018890{
18891 char_u *p;
18892
18893 p = get_tv_string(&argvars[0]);
18894 rettv->vval.v_string = vim_strsave(
18895 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18896 rettv->v_type = VAR_STRING;
18897}
18898#endif /* FEAT_CRYPT */
18899
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018901 * "shellescape({string})" function
18902 */
18903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018904f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018905{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018906 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018907 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018908 rettv->v_type = VAR_STRING;
18909}
18910
18911/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018912 * shiftwidth() function
18913 */
18914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018915f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018916{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018917 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018918}
18919
18920/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 */
18923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018926 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927
Bram Moolenaar0d660222005-01-07 21:51:51 +000018928 p = get_tv_string(&argvars[0]);
18929 rettv->vval.v_string = vim_strsave(p);
18930 simplify_filename(rettv->vval.v_string); /* simplify in place */
18931 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932}
18933
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018934#ifdef FEAT_FLOAT
18935/*
18936 * "sin()" function
18937 */
18938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018939f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018940{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018941 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018942
18943 rettv->v_type = VAR_FLOAT;
18944 if (get_float_arg(argvars, &f) == OK)
18945 rettv->vval.v_float = sin(f);
18946 else
18947 rettv->vval.v_float = 0.0;
18948}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018949
18950/*
18951 * "sinh()" function
18952 */
18953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018954f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018955{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018956 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018957
18958 rettv->v_type = VAR_FLOAT;
18959 if (get_float_arg(argvars, &f) == OK)
18960 rettv->vval.v_float = sinh(f);
18961 else
18962 rettv->vval.v_float = 0.0;
18963}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018964#endif
18965
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966static int
18967#ifdef __BORLANDC__
18968 _RTLENTRYF
18969#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018970 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971static int
18972#ifdef __BORLANDC__
18973 _RTLENTRYF
18974#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018975 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018977/* struct used in the array that's given to qsort() */
18978typedef struct
18979{
18980 listitem_T *item;
18981 int idx;
18982} sortItem_T;
18983
Bram Moolenaar0b962472016-02-22 22:51:33 +010018984/* struct storing information about current sort */
18985typedef struct
18986{
18987 int item_compare_ic;
18988 int item_compare_numeric;
18989 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018990#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018991 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018992#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018993 char_u *item_compare_func;
18994 dict_T *item_compare_selfdict;
18995 int item_compare_func_err;
18996 int item_compare_keep_zero;
18997} sortinfo_T;
18998static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018999static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019000#define ITEM_COMPARE_FAIL 999
19001
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019003 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 static int
19006#ifdef __BORLANDC__
19007_RTLENTRYF
19008#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019009item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019011 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019012 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019014 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019015 int res;
19016 char_u numbuf1[NUMBUFLEN];
19017 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019019 si1 = (sortItem_T *)s1;
19020 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019021 tv1 = &si1->item->li_tv;
19022 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019023
Bram Moolenaar0b962472016-02-22 22:51:33 +010019024 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019025 {
19026 long v1 = get_tv_number(tv1);
19027 long v2 = get_tv_number(tv2);
19028
19029 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19030 }
19031
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019032#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019033 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019034 {
19035 float_T v1 = get_tv_float(tv1);
19036 float_T v2 = get_tv_float(tv2);
19037
19038 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19039 }
19040#endif
19041
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019042 /* tv2string() puts quotes around a string and allocates memory. Don't do
19043 * that for string variables. Use a single quote when comparing with a
19044 * non-string to do what the docs promise. */
19045 if (tv1->v_type == VAR_STRING)
19046 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019047 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019048 p1 = (char_u *)"'";
19049 else
19050 p1 = tv1->vval.v_string;
19051 }
19052 else
19053 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19054 if (tv2->v_type == VAR_STRING)
19055 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019056 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019057 p2 = (char_u *)"'";
19058 else
19059 p2 = tv2->vval.v_string;
19060 }
19061 else
19062 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019063 if (p1 == NULL)
19064 p1 = (char_u *)"";
19065 if (p2 == NULL)
19066 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019067 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019068 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019069 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019070 res = STRICMP(p1, p2);
19071 else
19072 res = STRCMP(p1, p2);
19073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019075 {
19076 double n1, n2;
19077 n1 = strtod((char *)p1, (char **)&p1);
19078 n2 = strtod((char *)p2, (char **)&p2);
19079 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19080 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019081
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019082 /* When the result would be zero, compare the item indexes. Makes the
19083 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019085 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019086
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087 vim_free(tofree1);
19088 vim_free(tofree2);
19089 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090}
19091
19092 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093#ifdef __BORLANDC__
19094_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019096item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019097{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019098 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019099 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019100 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019101 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019102 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019104 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019106 return 0;
19107
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019108 si1 = (sortItem_T *)s1;
19109 si2 = (sortItem_T *)s2;
19110
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019111 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019112 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019113 copy_tv(&si1->item->li_tv, &argv[0]);
19114 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115
19116 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019118 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019119 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019121 clear_tv(&argv[0]);
19122 clear_tv(&argv[1]);
19123
19124 if (res == FAIL)
19125 res = ITEM_COMPARE_FAIL;
19126 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19128 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019129 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019131
19132 /* When the result would be zero, compare the pointers themselves. Makes
19133 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019135 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019136
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 return res;
19138}
19139
19140/*
19141 * "sort({list})" function
19142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019144do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145{
Bram Moolenaar33570922005-01-25 22:26:29 +000019146 list_T *l;
19147 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019148 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 sortinfo_T *old_sortinfo;
19150 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151 long len;
19152 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153
Bram Moolenaar0b962472016-02-22 22:51:33 +010019154 /* Pointer to current info struct used in compare function. Save and
19155 * restore the current one for nested calls. */
19156 old_sortinfo = sortinfo;
19157 sortinfo = &info;
19158
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019160 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 else
19162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019163 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019164 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019165 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19166 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 rettv->vval.v_list = l;
19169 rettv->v_type = VAR_LIST;
19170 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 len = list_len(l);
19173 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 info.item_compare_ic = FALSE;
19177 info.item_compare_numeric = FALSE;
19178 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019179#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019181#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 info.item_compare_func = NULL;
19183 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019184 if (argvars[1].v_type != VAR_UNKNOWN)
19185 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019186 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019188 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019189 else
19190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019191 int error = FALSE;
19192
19193 i = get_tv_number_chk(&argvars[1], &error);
19194 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019195 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019199 info.item_compare_func = get_tv_string(&argvars[1]);
19200 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019201 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019202 if (STRCMP(info.item_compare_func, "n") == 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_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019206 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019207 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019208 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019209 info.item_compare_func = NULL;
19210 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019211 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019212#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019213 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019214 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 info.item_compare_func = NULL;
19216 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019217 }
19218#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019220 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019221 info.item_compare_func = NULL;
19222 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019223 }
19224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019226
19227 if (argvars[2].v_type != VAR_UNKNOWN)
19228 {
19229 /* optional third argument: {dict} */
19230 if (argvars[2].v_type != VAR_DICT)
19231 {
19232 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019234 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019235 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019240 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019241 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019242 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243
Bram Moolenaar327aa022014-03-25 18:24:23 +010019244 i = 0;
19245 if (sort)
19246 {
19247 /* sort(): ptrs will be the list to sort */
19248 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019249 {
19250 ptrs[i].item = li;
19251 ptrs[i].idx = i;
19252 ++i;
19253 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019254
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 info.item_compare_func_err = FALSE;
19256 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019257 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019258 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019259 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019260 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019261 EMSG(_("E702: Sort compare function failed"));
19262 else
19263 {
19264 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019265 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019266 info.item_compare_func == NULL
19267 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019268
Bram Moolenaar0b962472016-02-22 22:51:33 +010019269 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019270 {
19271 /* Clear the List and append the items in sorted order. */
19272 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19273 l->lv_len = 0;
19274 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019275 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019276 }
19277 }
19278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019280 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019281 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019282
19283 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019284 info.item_compare_func_err = FALSE;
19285 info.item_compare_keep_zero = TRUE;
19286 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019287 ? item_compare2 : item_compare;
19288
19289 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19290 li = li->li_next)
19291 {
19292 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19293 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019294 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019295 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019296 {
19297 EMSG(_("E882: Uniq compare function failed"));
19298 break;
19299 }
19300 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019301
Bram Moolenaar0b962472016-02-22 22:51:33 +010019302 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019303 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019304 while (--i >= 0)
19305 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019306 li = ptrs[i].item->li_next;
19307 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019308 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019309 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019310 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019311 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019312 list_fix_watch(l, li);
19313 listitem_free(li);
19314 l->lv_len--;
19315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 }
19318
19319 vim_free(ptrs);
19320 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321theend:
19322 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323}
19324
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019326 * "sort({list})" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019330{
19331 do_sort_uniq(argvars, rettv, TRUE);
19332}
19333
19334/*
19335 * "uniq({list})" function
19336 */
19337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019338f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019339{
19340 do_sort_uniq(argvars, rettv, FALSE);
19341}
19342
19343/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019344 * "soundfold({word})" function
19345 */
19346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019347f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019348{
19349 char_u *s;
19350
19351 rettv->v_type = VAR_STRING;
19352 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019353#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019354 rettv->vval.v_string = eval_soundfold(s);
19355#else
19356 rettv->vval.v_string = vim_strsave(s);
19357#endif
19358}
19359
19360/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019361 * "spellbadword()" function
19362 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019364f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019365{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019366 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019367 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019368 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019369
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019370 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019371 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019372
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019373#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019374 if (argvars[0].v_type == VAR_UNKNOWN)
19375 {
19376 /* Find the start and length of the badly spelled word. */
19377 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19378 if (len != 0)
19379 word = ml_get_cursor();
19380 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019381 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019382 {
19383 char_u *str = get_tv_string_chk(&argvars[0]);
19384 int capcol = -1;
19385
19386 if (str != NULL)
19387 {
19388 /* Check the argument for spelling. */
19389 while (*str != NUL)
19390 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019391 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019392 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019393 {
19394 word = str;
19395 break;
19396 }
19397 str += len;
19398 }
19399 }
19400 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019401#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019402
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019403 list_append_string(rettv->vval.v_list, word, len);
19404 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019405 attr == HLF_SPB ? "bad" :
19406 attr == HLF_SPR ? "rare" :
19407 attr == HLF_SPL ? "local" :
19408 attr == HLF_SPC ? "caps" :
19409 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019410}
19411
19412/*
19413 * "spellsuggest()" function
19414 */
19415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019416f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019417{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019418#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019419 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019420 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019421 int maxcount;
19422 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019423 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019424 listitem_T *li;
19425 int need_capital = FALSE;
19426#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019428 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019429 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019430
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019431#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019432 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019433 {
19434 str = get_tv_string(&argvars[0]);
19435 if (argvars[1].v_type != VAR_UNKNOWN)
19436 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019437 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019438 if (maxcount <= 0)
19439 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019440 if (argvars[2].v_type != VAR_UNKNOWN)
19441 {
19442 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19443 if (typeerr)
19444 return;
19445 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019446 }
19447 else
19448 maxcount = 25;
19449
Bram Moolenaar4770d092006-01-12 23:22:24 +000019450 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019451
19452 for (i = 0; i < ga.ga_len; ++i)
19453 {
19454 str = ((char_u **)ga.ga_data)[i];
19455
19456 li = listitem_alloc();
19457 if (li == NULL)
19458 vim_free(str);
19459 else
19460 {
19461 li->li_tv.v_type = VAR_STRING;
19462 li->li_tv.v_lock = 0;
19463 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019464 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019465 }
19466 }
19467 ga_clear(&ga);
19468 }
19469#endif
19470}
19471
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019474{
19475 char_u *str;
19476 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019477 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019478 regmatch_T regmatch;
19479 char_u patbuf[NUMBUFLEN];
19480 char_u *save_cpo;
19481 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019483 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019484 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019485
19486 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19487 save_cpo = p_cpo;
19488 p_cpo = (char_u *)"";
19489
19490 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019491 if (argvars[1].v_type != VAR_UNKNOWN)
19492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019493 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19494 if (pat == NULL)
19495 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019496 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019497 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019498 }
19499 if (pat == NULL || *pat == NUL)
19500 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019502 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019504 if (typeerr)
19505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506
Bram Moolenaar0d660222005-01-07 21:51:51 +000019507 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19508 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019510 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019511 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019512 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019513 if (*str == NUL)
19514 match = FALSE; /* empty item at the end */
19515 else
19516 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019517 if (match)
19518 end = regmatch.startp[0];
19519 else
19520 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019521 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19522 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019523 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019524 if (list_append_string(rettv->vval.v_list, str,
19525 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019526 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019527 }
19528 if (!match)
19529 break;
19530 /* Advance to just after the match. */
19531 if (regmatch.endp[0] > str)
19532 col = 0;
19533 else
19534 {
19535 /* Don't get stuck at the same match. */
19536#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019537 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019538#else
19539 col = 1;
19540#endif
19541 }
19542 str = regmatch.endp[0];
19543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544
Bram Moolenaar473de612013-06-08 18:19:48 +020019545 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547
Bram Moolenaar0d660222005-01-07 21:51:51 +000019548 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549}
19550
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019551#ifdef FEAT_FLOAT
19552/*
19553 * "sqrt()" function
19554 */
19555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019557{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019558 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019559
19560 rettv->v_type = VAR_FLOAT;
19561 if (get_float_arg(argvars, &f) == OK)
19562 rettv->vval.v_float = sqrt(f);
19563 else
19564 rettv->vval.v_float = 0.0;
19565}
19566
19567/*
19568 * "str2float()" function
19569 */
19570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019571f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019572{
19573 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19574
19575 if (*p == '+')
19576 p = skipwhite(p + 1);
19577 (void)string2float(p, &rettv->vval.v_float);
19578 rettv->v_type = VAR_FLOAT;
19579}
19580#endif
19581
Bram Moolenaar2c932302006-03-18 21:42:09 +000019582/*
19583 * "str2nr()" function
19584 */
19585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019586f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019587{
19588 int base = 10;
19589 char_u *p;
19590 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019591 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019592
19593 if (argvars[1].v_type != VAR_UNKNOWN)
19594 {
19595 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019596 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019597 {
19598 EMSG(_(e_invarg));
19599 return;
19600 }
19601 }
19602
19603 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019604 if (*p == '+')
19605 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019606 switch (base)
19607 {
19608 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19609 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19610 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19611 default: what = 0;
19612 }
19613 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019614 rettv->vval.v_number = n;
19615}
19616
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617#ifdef HAVE_STRFTIME
19618/*
19619 * "strftime({format}[, {time}])" function
19620 */
19621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019622f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623{
19624 char_u result_buf[256];
19625 struct tm *curtime;
19626 time_t seconds;
19627 char_u *p;
19628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019631 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 seconds = time(NULL);
19634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 curtime = localtime(&seconds);
19637 /* MSVC returns NULL for an invalid value of seconds. */
19638 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019639 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 else
19641 {
19642# ifdef FEAT_MBYTE
19643 vimconv_T conv;
19644 char_u *enc;
19645
19646 conv.vc_type = CONV_NONE;
19647 enc = enc_locale();
19648 convert_setup(&conv, p_enc, enc);
19649 if (conv.vc_type != CONV_NONE)
19650 p = string_convert(&conv, p, NULL);
19651# endif
19652 if (p != NULL)
19653 (void)strftime((char *)result_buf, sizeof(result_buf),
19654 (char *)p, curtime);
19655 else
19656 result_buf[0] = NUL;
19657
19658# ifdef FEAT_MBYTE
19659 if (conv.vc_type != CONV_NONE)
19660 vim_free(p);
19661 convert_setup(&conv, enc, p_enc);
19662 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 else
19665# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667
19668# ifdef FEAT_MBYTE
19669 /* Release conversion descriptors */
19670 convert_setup(&conv, NULL, NULL);
19671 vim_free(enc);
19672# endif
19673 }
19674}
19675#endif
19676
19677/*
19678 * "stridx()" function
19679 */
19680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019681f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682{
19683 char_u buf[NUMBUFLEN];
19684 char_u *needle;
19685 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019690 needle = get_tv_string_chk(&argvars[1]);
19691 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019693 if (needle == NULL || haystack == NULL)
19694 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 if (argvars[2].v_type != VAR_UNKNOWN)
19697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019698 int error = FALSE;
19699
19700 start_idx = get_tv_number_chk(&argvars[2], &error);
19701 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019703 if (start_idx >= 0)
19704 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019705 }
19706
19707 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19708 if (pos != NULL)
19709 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710}
19711
19712/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019713 * "string()" function
19714 */
19715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019716f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019717{
19718 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019719 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019722 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019723 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019724 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726}
19727
19728/*
19729 * "strlen()" function
19730 */
19731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019732f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 rettv->vval.v_number = (varnumber_T)(STRLEN(
19735 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736}
19737
19738/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019739 * "strchars()" function
19740 */
19741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019742f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019743{
19744 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019745 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019746#ifdef FEAT_MBYTE
19747 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019748 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019749#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019750
19751 if (argvars[1].v_type != VAR_UNKNOWN)
19752 skipcc = get_tv_number_chk(&argvars[1], NULL);
19753 if (skipcc < 0 || skipcc > 1)
19754 EMSG(_(e_invarg));
19755 else
19756 {
19757#ifdef FEAT_MBYTE
19758 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19759 while (*s != NUL)
19760 {
19761 func_mb_ptr2char_adv(&s);
19762 ++len;
19763 }
19764 rettv->vval.v_number = len;
19765#else
19766 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19767#endif
19768 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019769}
19770
19771/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019772 * "strdisplaywidth()" function
19773 */
19774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019775f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019776{
19777 char_u *s = get_tv_string(&argvars[0]);
19778 int col = 0;
19779
19780 if (argvars[1].v_type != VAR_UNKNOWN)
19781 col = get_tv_number(&argvars[1]);
19782
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019783 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019784}
19785
19786/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019787 * "strwidth()" function
19788 */
19789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019790f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019791{
19792 char_u *s = get_tv_string(&argvars[0]);
19793
19794 rettv->vval.v_number = (varnumber_T)(
19795#ifdef FEAT_MBYTE
19796 mb_string2cells(s, -1)
19797#else
19798 STRLEN(s)
19799#endif
19800 );
19801}
19802
19803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 * "strpart()" function
19805 */
19806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019807f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808{
19809 char_u *p;
19810 int n;
19811 int len;
19812 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019813 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 slen = (int)STRLEN(p);
19817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019818 n = get_tv_number_chk(&argvars[1], &error);
19819 if (error)
19820 len = 0;
19821 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 else
19824 len = slen - n; /* default len: all bytes that are available. */
19825
19826 /*
19827 * Only return the overlap between the specified part and the actual
19828 * string.
19829 */
19830 if (n < 0)
19831 {
19832 len += n;
19833 n = 0;
19834 }
19835 else if (n > slen)
19836 n = slen;
19837 if (len < 0)
19838 len = 0;
19839 else if (n + len > slen)
19840 len = slen - n;
19841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 rettv->v_type = VAR_STRING;
19843 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844}
19845
19846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019847 * "strridx()" function
19848 */
19849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019850f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019851{
19852 char_u buf[NUMBUFLEN];
19853 char_u *needle;
19854 char_u *haystack;
19855 char_u *rest;
19856 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019857 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019859 needle = get_tv_string_chk(&argvars[1]);
19860 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019861
19862 rettv->vval.v_number = -1;
19863 if (needle == NULL || haystack == NULL)
19864 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019865
19866 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019867 if (argvars[2].v_type != VAR_UNKNOWN)
19868 {
19869 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019871 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019872 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019873 }
19874 else
19875 end_idx = haystack_len;
19876
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019878 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019879 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019880 lastmatch = haystack + end_idx;
19881 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019882 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019883 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019884 for (rest = haystack; *rest != '\0'; ++rest)
19885 {
19886 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019887 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019888 break;
19889 lastmatch = rest;
19890 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019892
19893 if (lastmatch == NULL)
19894 rettv->vval.v_number = -1;
19895 else
19896 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19897}
19898
19899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 * "strtrans()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019905 rettv->v_type = VAR_STRING;
19906 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019910 * "submatch()" function
19911 */
19912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019914{
Bram Moolenaar41571762014-04-02 19:00:58 +020019915 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019916 int no;
19917 int retList = 0;
19918
19919 no = (int)get_tv_number_chk(&argvars[0], &error);
19920 if (error)
19921 return;
19922 error = FALSE;
19923 if (argvars[1].v_type != VAR_UNKNOWN)
19924 retList = get_tv_number_chk(&argvars[1], &error);
19925 if (error)
19926 return;
19927
19928 if (retList == 0)
19929 {
19930 rettv->v_type = VAR_STRING;
19931 rettv->vval.v_string = reg_submatch(no);
19932 }
19933 else
19934 {
19935 rettv->v_type = VAR_LIST;
19936 rettv->vval.v_list = reg_submatch_list(no);
19937 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019938}
19939
19940/*
19941 * "substitute()" function
19942 */
19943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019944f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019945{
19946 char_u patbuf[NUMBUFLEN];
19947 char_u subbuf[NUMBUFLEN];
19948 char_u flagsbuf[NUMBUFLEN];
19949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019950 char_u *str = get_tv_string_chk(&argvars[0]);
19951 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19952 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19953 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19954
Bram Moolenaar0d660222005-01-07 21:51:51 +000019955 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019956 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19957 rettv->vval.v_string = NULL;
19958 else
19959 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019960}
19961
19962/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019963 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967{
19968 int id = 0;
19969#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019970 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 long col;
19972 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019973 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 lnum = get_tv_lnum(argvars); /* -1 on type error */
19976 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19977 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019979 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019980 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019981 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982#endif
19983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985}
19986
19987/*
19988 * "synIDattr(id, what [, mode])" function
19989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019991f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992{
19993 char_u *p = NULL;
19994#ifdef FEAT_SYN_HL
19995 int id;
19996 char_u *what;
19997 char_u *mode;
19998 char_u modebuf[NUMBUFLEN];
19999 int modec;
20000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 id = get_tv_number(&argvars[0]);
20002 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020007 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 modec = 0; /* replace invalid with current */
20009 }
20010 else
20011 {
20012#ifdef FEAT_GUI
20013 if (gui.in_use)
20014 modec = 'g';
20015 else
20016#endif
20017 if (t_colors > 1)
20018 modec = 'c';
20019 else
20020 modec = 't';
20021 }
20022
20023
20024 switch (TOLOWER_ASC(what[0]))
20025 {
20026 case 'b':
20027 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20028 p = highlight_color(id, what, modec);
20029 else /* bold */
20030 p = highlight_has_attr(id, HL_BOLD, modec);
20031 break;
20032
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020033 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 p = highlight_color(id, what, modec);
20035 break;
20036
20037 case 'i':
20038 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20039 p = highlight_has_attr(id, HL_INVERSE, modec);
20040 else /* italic */
20041 p = highlight_has_attr(id, HL_ITALIC, modec);
20042 break;
20043
20044 case 'n': /* name */
20045 p = get_highlight_name(NULL, id - 1);
20046 break;
20047
20048 case 'r': /* reverse */
20049 p = highlight_has_attr(id, HL_INVERSE, modec);
20050 break;
20051
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020052 case 's':
20053 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20054 p = highlight_color(id, what, modec);
20055 else /* standout */
20056 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 break;
20058
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020059 case 'u':
20060 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20061 /* underline */
20062 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20063 else
20064 /* undercurl */
20065 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 break;
20067 }
20068
20069 if (p != NULL)
20070 p = vim_strsave(p);
20071#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072 rettv->v_type = VAR_STRING;
20073 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074}
20075
20076/*
20077 * "synIDtrans(id)" function
20078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020080f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081{
20082 int id;
20083
20084#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086
20087 if (id > 0)
20088 id = syn_get_final_id(id);
20089 else
20090#endif
20091 id = 0;
20092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094}
20095
20096/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020097 * "synconcealed(lnum, col)" function
20098 */
20099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020100f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020101{
20102#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20103 long lnum;
20104 long col;
20105 int syntax_flags = 0;
20106 int cchar;
20107 int matchid = 0;
20108 char_u str[NUMBUFLEN];
20109#endif
20110
20111 rettv->v_type = VAR_LIST;
20112 rettv->vval.v_list = NULL;
20113
20114#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20115 lnum = get_tv_lnum(argvars); /* -1 on type error */
20116 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20117
20118 vim_memset(str, NUL, sizeof(str));
20119
20120 if (rettv_list_alloc(rettv) != FAIL)
20121 {
20122 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20123 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20124 && curwin->w_p_cole > 0)
20125 {
20126 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20127 syntax_flags = get_syntax_info(&matchid);
20128
20129 /* get the conceal character */
20130 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20131 {
20132 cchar = syn_get_sub_char();
20133 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20134 cchar = lcs_conceal;
20135 if (cchar != NUL)
20136 {
20137# ifdef FEAT_MBYTE
20138 if (has_mbyte)
20139 (*mb_char2bytes)(cchar, str);
20140 else
20141# endif
20142 str[0] = cchar;
20143 }
20144 }
20145 }
20146
20147 list_append_number(rettv->vval.v_list,
20148 (syntax_flags & HL_CONCEAL) != 0);
20149 /* -1 to auto-determine strlen */
20150 list_append_string(rettv->vval.v_list, str, -1);
20151 list_append_number(rettv->vval.v_list, matchid);
20152 }
20153#endif
20154}
20155
20156/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020157 * "synstack(lnum, col)" function
20158 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020160f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020161{
20162#ifdef FEAT_SYN_HL
20163 long lnum;
20164 long col;
20165 int i;
20166 int id;
20167#endif
20168
20169 rettv->v_type = VAR_LIST;
20170 rettv->vval.v_list = NULL;
20171
20172#ifdef FEAT_SYN_HL
20173 lnum = get_tv_lnum(argvars); /* -1 on type error */
20174 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20175
20176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020177 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020178 && rettv_list_alloc(rettv) != FAIL)
20179 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020180 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020181 for (i = 0; ; ++i)
20182 {
20183 id = syn_get_stack_item(i);
20184 if (id < 0)
20185 break;
20186 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20187 break;
20188 }
20189 }
20190#endif
20191}
20192
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020194get_cmd_output_as_rettv(
20195 typval_T *argvars,
20196 typval_T *rettv,
20197 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020199 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020201 char_u *infile = NULL;
20202 char_u buf[NUMBUFLEN];
20203 int err = FALSE;
20204 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020205 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020206 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020208 rettv->v_type = VAR_STRING;
20209 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020210 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020211 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020212
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020213 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020214 {
20215 /*
20216 * Write the string to a temp file, to be used for input of the shell
20217 * command.
20218 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020219 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020220 {
20221 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020222 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020223 }
20224
20225 fd = mch_fopen((char *)infile, WRITEBIN);
20226 if (fd == NULL)
20227 {
20228 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020229 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020230 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020231 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020232 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020233 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20234 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020235 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020236 else
20237 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020238 size_t len;
20239
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020240 p = get_tv_string_buf_chk(&argvars[1], buf);
20241 if (p == NULL)
20242 {
20243 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020244 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020245 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020246 len = STRLEN(p);
20247 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020248 err = TRUE;
20249 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020250 if (fclose(fd) != 0)
20251 err = TRUE;
20252 if (err)
20253 {
20254 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020255 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020256 }
20257 }
20258
Bram Moolenaar52a72462014-08-29 15:53:52 +020020259 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20260 * echoes typeahead, that messes up the display. */
20261 if (!msg_silent)
20262 flags += SHELL_COOKED;
20263
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020266 int len;
20267 listitem_T *li;
20268 char_u *s = NULL;
20269 char_u *start;
20270 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020271 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272
Bram Moolenaar52a72462014-08-29 15:53:52 +020020273 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 if (res == NULL)
20275 goto errret;
20276
20277 list = list_alloc();
20278 if (list == NULL)
20279 goto errret;
20280
20281 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020283 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020284 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020286 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020287
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020288 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020289 if (s == NULL)
20290 goto errret;
20291
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020292 for (p = s; start < end; ++p, ++start)
20293 *p = *start == NUL ? NL : *start;
20294 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020295
20296 li = listitem_alloc();
20297 if (li == NULL)
20298 {
20299 vim_free(s);
20300 goto errret;
20301 }
20302 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020303 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020304 li->li_tv.vval.v_string = s;
20305 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020307
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020308 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020309 rettv->v_type = VAR_LIST;
20310 rettv->vval.v_list = list;
20311 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020313 else
20314 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020315 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316#ifdef USE_CR
20317 /* translate <CR> into <NL> */
20318 if (res != NULL)
20319 {
20320 char_u *s;
20321
20322 for (s = res; *s; ++s)
20323 {
20324 if (*s == CAR)
20325 *s = NL;
20326 }
20327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328#else
20329# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020330 /* translate <CR><NL> into <NL> */
20331 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020333 char_u *s, *d;
20334
20335 d = res;
20336 for (s = res; *s; ++s)
20337 {
20338 if (s[0] == CAR && s[1] == NL)
20339 ++s;
20340 *d++ = *s;
20341 }
20342 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344# endif
20345#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 rettv->vval.v_string = res;
20347 res = NULL;
20348 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020349
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020350errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020351 if (infile != NULL)
20352 {
20353 mch_remove(infile);
20354 vim_free(infile);
20355 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020356 if (res != NULL)
20357 vim_free(res);
20358 if (list != NULL)
20359 list_free(list, TRUE);
20360}
20361
20362/*
20363 * "system()" function
20364 */
20365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020366f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020367{
20368 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20369}
20370
20371/*
20372 * "systemlist()" function
20373 */
20374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020376{
20377 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378}
20379
20380/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020381 * "tabpagebuflist()" function
20382 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020384f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020385{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020386#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020387 tabpage_T *tp;
20388 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020389
20390 if (argvars[0].v_type == VAR_UNKNOWN)
20391 wp = firstwin;
20392 else
20393 {
20394 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20395 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020396 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020397 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020398 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020399 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020400 for (; wp != NULL; wp = wp->w_next)
20401 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020402 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020403 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020404 }
20405#endif
20406}
20407
20408
20409/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020410 * "tabpagenr()" function
20411 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020413f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020414{
20415 int nr = 1;
20416#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020417 char_u *arg;
20418
20419 if (argvars[0].v_type != VAR_UNKNOWN)
20420 {
20421 arg = get_tv_string_chk(&argvars[0]);
20422 nr = 0;
20423 if (arg != NULL)
20424 {
20425 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020426 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020427 else
20428 EMSG2(_(e_invexpr2), arg);
20429 }
20430 }
20431 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020432 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020433#endif
20434 rettv->vval.v_number = nr;
20435}
20436
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020437
20438#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020439static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020440
20441/*
20442 * Common code for tabpagewinnr() and winnr().
20443 */
20444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020445get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020446{
20447 win_T *twin;
20448 int nr = 1;
20449 win_T *wp;
20450 char_u *arg;
20451
20452 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20453 if (argvar->v_type != VAR_UNKNOWN)
20454 {
20455 arg = get_tv_string_chk(argvar);
20456 if (arg == NULL)
20457 nr = 0; /* type error; errmsg already given */
20458 else if (STRCMP(arg, "$") == 0)
20459 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20460 else if (STRCMP(arg, "#") == 0)
20461 {
20462 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20463 if (twin == NULL)
20464 nr = 0;
20465 }
20466 else
20467 {
20468 EMSG2(_(e_invexpr2), arg);
20469 nr = 0;
20470 }
20471 }
20472
20473 if (nr > 0)
20474 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20475 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020476 {
20477 if (wp == NULL)
20478 {
20479 /* didn't find it in this tabpage */
20480 nr = 0;
20481 break;
20482 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020483 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020484 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020485 return nr;
20486}
20487#endif
20488
20489/*
20490 * "tabpagewinnr()" function
20491 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020493f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020494{
20495 int nr = 1;
20496#ifdef FEAT_WINDOWS
20497 tabpage_T *tp;
20498
20499 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20500 if (tp == NULL)
20501 nr = 0;
20502 else
20503 nr = get_winnr(tp, &argvars[1]);
20504#endif
20505 rettv->vval.v_number = nr;
20506}
20507
20508
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020509/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020510 * "tagfiles()" function
20511 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020513f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020514{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020515 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020516 tagname_T tn;
20517 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020519 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020520 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020521 fname = alloc(MAXPATHL);
20522 if (fname == NULL)
20523 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020524
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020525 for (first = TRUE; ; first = FALSE)
20526 if (get_tagfname(&tn, first, fname) == FAIL
20527 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020528 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020529 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020530 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020531}
20532
20533/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020534 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020538{
20539 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020540
20541 tag_pattern = get_tv_string(&argvars[0]);
20542
20543 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020544 if (*tag_pattern == NUL)
20545 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020547 if (rettv_list_alloc(rettv) == OK)
20548 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020549}
20550
20551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 * "tempname()" function
20553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020555f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556{
20557 static int x = 'A';
20558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020560 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561
20562 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20563 * names. Skip 'I' and 'O', they are used for shell redirection. */
20564 do
20565 {
20566 if (x == 'Z')
20567 x = '0';
20568 else if (x == '9')
20569 x = 'A';
20570 else
20571 {
20572#ifdef EBCDIC
20573 if (x == 'I')
20574 x = 'J';
20575 else if (x == 'R')
20576 x = 'S';
20577 else
20578#endif
20579 ++x;
20580 }
20581 } while (x == 'I' || x == 'O');
20582}
20583
20584/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020585 * "test(list)" function: Just checking the walls...
20586 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020588f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020589{
20590 /* Used for unit testing. Change the code below to your liking. */
20591#if 0
20592 listitem_T *li;
20593 list_T *l;
20594 char_u *bad, *good;
20595
20596 if (argvars[0].v_type != VAR_LIST)
20597 return;
20598 l = argvars[0].vval.v_list;
20599 if (l == NULL)
20600 return;
20601 li = l->lv_first;
20602 if (li == NULL)
20603 return;
20604 bad = get_tv_string(&li->li_tv);
20605 li = li->li_next;
20606 if (li == NULL)
20607 return;
20608 good = get_tv_string(&li->li_tv);
20609 rettv->vval.v_number = test_edit_score(bad, good);
20610#endif
20611}
20612
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020613#ifdef FEAT_FLOAT
20614/*
20615 * "tan()" function
20616 */
20617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020618f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020619{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020620 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020621
20622 rettv->v_type = VAR_FLOAT;
20623 if (get_float_arg(argvars, &f) == OK)
20624 rettv->vval.v_float = tan(f);
20625 else
20626 rettv->vval.v_float = 0.0;
20627}
20628
20629/*
20630 * "tanh()" function
20631 */
20632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020633f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020634{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020635 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020636
20637 rettv->v_type = VAR_FLOAT;
20638 if (get_float_arg(argvars, &f) == OK)
20639 rettv->vval.v_float = tanh(f);
20640 else
20641 rettv->vval.v_float = 0.0;
20642}
20643#endif
20644
Bram Moolenaard52d9742005-08-21 22:20:28 +000020645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 * "tolower(string)" function
20647 */
20648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020649f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650{
20651 char_u *p;
20652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020653 p = vim_strsave(get_tv_string(&argvars[0]));
20654 rettv->v_type = VAR_STRING;
20655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656
20657 if (p != NULL)
20658 while (*p != NUL)
20659 {
20660#ifdef FEAT_MBYTE
20661 int l;
20662
20663 if (enc_utf8)
20664 {
20665 int c, lc;
20666
20667 c = utf_ptr2char(p);
20668 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020669 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 /* TODO: reallocate string when byte count changes. */
20671 if (utf_char2len(lc) == l)
20672 utf_char2bytes(lc, p);
20673 p += l;
20674 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020675 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 p += l; /* skip multi-byte character */
20677 else
20678#endif
20679 {
20680 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20681 ++p;
20682 }
20683 }
20684}
20685
20686/*
20687 * "toupper(string)" function
20688 */
20689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020690f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020692 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020693 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694}
20695
20696/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020697 * "tr(string, fromstr, tostr)" function
20698 */
20699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020700f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020701{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020702 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020703 char_u *fromstr;
20704 char_u *tostr;
20705 char_u *p;
20706#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020707 int inlen;
20708 int fromlen;
20709 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020710 int idx;
20711 char_u *cpstr;
20712 int cplen;
20713 int first = TRUE;
20714#endif
20715 char_u buf[NUMBUFLEN];
20716 char_u buf2[NUMBUFLEN];
20717 garray_T ga;
20718
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020719 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020720 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20721 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020722
20723 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020724 rettv->v_type = VAR_STRING;
20725 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020726 if (fromstr == NULL || tostr == NULL)
20727 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020728 ga_init2(&ga, (int)sizeof(char), 80);
20729
20730#ifdef FEAT_MBYTE
20731 if (!has_mbyte)
20732#endif
20733 /* not multi-byte: fromstr and tostr must be the same length */
20734 if (STRLEN(fromstr) != STRLEN(tostr))
20735 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020736#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020737error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020738#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020739 EMSG2(_(e_invarg2), fromstr);
20740 ga_clear(&ga);
20741 return;
20742 }
20743
20744 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020745 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020746 {
20747#ifdef FEAT_MBYTE
20748 if (has_mbyte)
20749 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020750 inlen = (*mb_ptr2len)(in_str);
20751 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020752 cplen = inlen;
20753 idx = 0;
20754 for (p = fromstr; *p != NUL; p += fromlen)
20755 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020756 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020757 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020758 {
20759 for (p = tostr; *p != NUL; p += tolen)
20760 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020761 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020762 if (idx-- == 0)
20763 {
20764 cplen = tolen;
20765 cpstr = p;
20766 break;
20767 }
20768 }
20769 if (*p == NUL) /* tostr is shorter than fromstr */
20770 goto error;
20771 break;
20772 }
20773 ++idx;
20774 }
20775
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020776 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020777 {
20778 /* Check that fromstr and tostr have the same number of
20779 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020780 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020781 first = FALSE;
20782 for (p = tostr; *p != NUL; p += tolen)
20783 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020784 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020785 --idx;
20786 }
20787 if (idx != 0)
20788 goto error;
20789 }
20790
Bram Moolenaarcde88542015-08-11 19:14:00 +020020791 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020792 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020793 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020794
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020795 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020796 }
20797 else
20798#endif
20799 {
20800 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020801 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020802 if (p != NULL)
20803 ga_append(&ga, tostr[p - fromstr]);
20804 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020805 ga_append(&ga, *in_str);
20806 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020807 }
20808 }
20809
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020810 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020811 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020812 ga_append(&ga, NUL);
20813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020815}
20816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020817#ifdef FEAT_FLOAT
20818/*
20819 * "trunc({float})" function
20820 */
20821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020822f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020823{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020824 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020825
20826 rettv->v_type = VAR_FLOAT;
20827 if (get_float_arg(argvars, &f) == OK)
20828 /* trunc() is not in C90, use floor() or ceil() instead. */
20829 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20830 else
20831 rettv->vval.v_float = 0.0;
20832}
20833#endif
20834
Bram Moolenaar8299df92004-07-10 09:47:34 +000020835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 * "type(expr)" function
20837 */
20838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020839f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020841 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020842
20843 switch (argvars[0].v_type)
20844 {
20845 case VAR_NUMBER: n = 0; break;
20846 case VAR_STRING: n = 1; break;
20847 case VAR_FUNC: n = 2; break;
20848 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020849 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020850 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020851 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020852 if (argvars[0].vval.v_number == VVAL_FALSE
20853 || argvars[0].vval.v_number == VVAL_TRUE)
20854 n = 6;
20855 else
20856 n = 7;
20857 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020858 case VAR_JOB: n = 8; break;
20859 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020860 case VAR_UNKNOWN:
20861 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20862 n = -1;
20863 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020864 }
20865 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866}
20867
20868/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020869 * "undofile(name)" function
20870 */
20871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020873{
20874 rettv->v_type = VAR_STRING;
20875#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020876 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020877 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020878
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020879 if (*fname == NUL)
20880 {
20881 /* If there is no file name there will be no undo file. */
20882 rettv->vval.v_string = NULL;
20883 }
20884 else
20885 {
20886 char_u *ffname = FullName_save(fname, FALSE);
20887
20888 if (ffname != NULL)
20889 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20890 vim_free(ffname);
20891 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020892 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020893#else
20894 rettv->vval.v_string = NULL;
20895#endif
20896}
20897
20898/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020899 * "undotree()" function
20900 */
20901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020903{
20904 if (rettv_dict_alloc(rettv) == OK)
20905 {
20906 dict_T *dict = rettv->vval.v_dict;
20907 list_T *list;
20908
Bram Moolenaar730cde92010-06-27 05:18:54 +020020909 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020910 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020911 dict_add_nr_str(dict, "save_last",
20912 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020913 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20914 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020915 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020916
20917 list = list_alloc();
20918 if (list != NULL)
20919 {
20920 u_eval_tree(curbuf->b_u_oldhead, list);
20921 dict_add_list(dict, "entries", list);
20922 }
20923 }
20924}
20925
20926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020927 * "values(dict)" function
20928 */
20929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020930f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020931{
20932 dict_list(argvars, rettv, 1);
20933}
20934
20935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 * "virtcol(string)" function
20937 */
20938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020939f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940{
20941 colnr_T vcol = 0;
20942 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020943 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020945 fp = var2fpos(&argvars[0], FALSE, &fnum);
20946 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20947 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 {
20949 getvvcol(curwin, fp, NULL, NULL, &vcol);
20950 ++vcol;
20951 }
20952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020953 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954}
20955
20956/*
20957 * "visualmode()" function
20958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 char_u str[2];
20963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 str[0] = curbuf->b_visual_mode_eval;
20966 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968
20969 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020970 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972}
20973
20974/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020975 * "wildmenumode()" function
20976 */
20977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020978f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020979{
20980#ifdef FEAT_WILDMENU
20981 if (wild_menu_showing)
20982 rettv->vval.v_number = 1;
20983#endif
20984}
20985
20986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 * "winbufnr(nr)" function
20988 */
20989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020990f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991{
20992 win_T *wp;
20993
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020994 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020998 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999}
21000
21001/*
21002 * "wincol()" function
21003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021005f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006{
21007 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021008 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009}
21010
21011/*
21012 * "winheight(nr)" function
21013 */
21014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021015f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016{
21017 win_T *wp;
21018
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021019 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021021 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024}
21025
21026/*
21027 * "winline()" function
21028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021030f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031{
21032 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021033 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034}
21035
21036/*
21037 * "winnr()" function
21038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021040f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041{
21042 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021043
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021045 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048}
21049
21050/*
21051 * "winrestcmd()" function
21052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021054f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055{
21056#ifdef FEAT_WINDOWS
21057 win_T *wp;
21058 int winnr = 1;
21059 garray_T ga;
21060 char_u buf[50];
21061
21062 ga_init2(&ga, (int)sizeof(char), 70);
21063 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21064 {
21065 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21066 ga_concat(&ga, buf);
21067# ifdef FEAT_VERTSPLIT
21068 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21069 ga_concat(&ga, buf);
21070# endif
21071 ++winnr;
21072 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021073 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021075 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021077 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080}
21081
21082/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021083 * "winrestview()" function
21084 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021086f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021087{
21088 dict_T *dict;
21089
21090 if (argvars[0].v_type != VAR_DICT
21091 || (dict = argvars[0].vval.v_dict) == NULL)
21092 EMSG(_(e_invarg));
21093 else
21094 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021095 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21096 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21097 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21098 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021099#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021100 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21101 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021102#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021103 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21104 {
21105 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21106 curwin->w_set_curswant = FALSE;
21107 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021108
Bram Moolenaar82c25852014-05-28 16:47:16 +020021109 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21110 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021111#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021112 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21113 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021114#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021115 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21116 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21117 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21118 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021119
21120 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021121 win_new_height(curwin, curwin->w_height);
21122# ifdef FEAT_VERTSPLIT
21123 win_new_width(curwin, W_WIDTH(curwin));
21124# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021125 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021126
Bram Moolenaarb851a962014-10-31 15:45:52 +010021127 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021128 curwin->w_topline = 1;
21129 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21130 curwin->w_topline = curbuf->b_ml.ml_line_count;
21131#ifdef FEAT_DIFF
21132 check_topfill(curwin, TRUE);
21133#endif
21134 }
21135}
21136
21137/*
21138 * "winsaveview()" function
21139 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021141f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021142{
21143 dict_T *dict;
21144
Bram Moolenaara800b422010-06-27 01:15:55 +020021145 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021146 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021147 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021148
21149 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21150 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21151#ifdef FEAT_VIRTUALEDIT
21152 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21153#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021154 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021155 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21156
21157 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21158#ifdef FEAT_DIFF
21159 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21160#endif
21161 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21162 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21163}
21164
21165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 * "winwidth(nr)" function
21167 */
21168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021169f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170{
21171 win_T *wp;
21172
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021173 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 else
21177#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021178 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021180 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181#endif
21182}
21183
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021185 * "wordcount()" function
21186 */
21187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021188f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021189{
21190 if (rettv_dict_alloc(rettv) == FAIL)
21191 return;
21192 cursor_pos_info(rettv->vval.v_dict);
21193}
21194
21195/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021196 * Write list of strings to file
21197 */
21198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021199write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021200{
21201 listitem_T *li;
21202 int c;
21203 int ret = OK;
21204 char_u *s;
21205
21206 for (li = list->lv_first; li != NULL; li = li->li_next)
21207 {
21208 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21209 {
21210 if (*s == '\n')
21211 c = putc(NUL, fd);
21212 else
21213 c = putc(*s, fd);
21214 if (c == EOF)
21215 {
21216 ret = FAIL;
21217 break;
21218 }
21219 }
21220 if (!binary || li->li_next != NULL)
21221 if (putc('\n', fd) == EOF)
21222 {
21223 ret = FAIL;
21224 break;
21225 }
21226 if (ret == FAIL)
21227 {
21228 EMSG(_(e_write));
21229 break;
21230 }
21231 }
21232 return ret;
21233}
21234
21235/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021236 * "writefile()" function
21237 */
21238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021239f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021240{
21241 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021242 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021243 char_u *fname;
21244 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021245 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021246
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021247 if (check_restricted() || check_secure())
21248 return;
21249
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021250 if (argvars[0].v_type != VAR_LIST)
21251 {
21252 EMSG2(_(e_listarg), "writefile()");
21253 return;
21254 }
21255 if (argvars[0].vval.v_list == NULL)
21256 return;
21257
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021258 if (argvars[2].v_type != VAR_UNKNOWN)
21259 {
21260 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21261 binary = TRUE;
21262 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21263 append = TRUE;
21264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021265
21266 /* Always open the file in binary mode, library functions have a mind of
21267 * their own about CR-LF conversion. */
21268 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021269 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21270 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021271 {
21272 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21273 ret = -1;
21274 }
21275 else
21276 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021277 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21278 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021279 fclose(fd);
21280 }
21281
21282 rettv->vval.v_number = ret;
21283}
21284
21285/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021286 * "xor(expr, expr)" function
21287 */
21288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021289f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021290{
21291 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21292 ^ get_tv_number_chk(&argvars[1], NULL);
21293}
21294
21295
21296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021298 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 */
21300 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021301var2fpos(
21302 typval_T *varp,
21303 int dollar_lnum, /* TRUE when $ is last line */
21304 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021306 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021308 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309
Bram Moolenaara5525202006-03-02 22:52:09 +000021310 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021311 if (varp->v_type == VAR_LIST)
21312 {
21313 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021314 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021315 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021316 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021317
21318 l = varp->vval.v_list;
21319 if (l == NULL)
21320 return NULL;
21321
21322 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021323 pos.lnum = list_find_nr(l, 0L, &error);
21324 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021325 return NULL; /* invalid line number */
21326
21327 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021328 pos.col = list_find_nr(l, 1L, &error);
21329 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021330 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021331 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021332
21333 /* We accept "$" for the column number: last column. */
21334 li = list_find(l, 1L);
21335 if (li != NULL && li->li_tv.v_type == VAR_STRING
21336 && li->li_tv.vval.v_string != NULL
21337 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21338 pos.col = len + 1;
21339
Bram Moolenaara5525202006-03-02 22:52:09 +000021340 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021341 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021342 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021343 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021344
Bram Moolenaara5525202006-03-02 22:52:09 +000021345#ifdef FEAT_VIRTUALEDIT
21346 /* Get the virtual offset. Defaults to zero. */
21347 pos.coladd = list_find_nr(l, 2L, &error);
21348 if (error)
21349 pos.coladd = 0;
21350#endif
21351
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021352 return &pos;
21353 }
21354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021355 name = get_tv_string_chk(varp);
21356 if (name == NULL)
21357 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021358 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021360 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21361 {
21362 if (VIsual_active)
21363 return &VIsual;
21364 return &curwin->w_cursor;
21365 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021366 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021368 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21370 return NULL;
21371 return pp;
21372 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021373
21374#ifdef FEAT_VIRTUALEDIT
21375 pos.coladd = 0;
21376#endif
21377
Bram Moolenaar477933c2007-07-17 14:32:23 +000021378 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021379 {
21380 pos.col = 0;
21381 if (name[1] == '0') /* "w0": first visible line */
21382 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021383 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021384 pos.lnum = curwin->w_topline;
21385 return &pos;
21386 }
21387 else if (name[1] == '$') /* "w$": last visible line */
21388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021389 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021390 pos.lnum = curwin->w_botline - 1;
21391 return &pos;
21392 }
21393 }
21394 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021396 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 {
21398 pos.lnum = curbuf->b_ml.ml_line_count;
21399 pos.col = 0;
21400 }
21401 else
21402 {
21403 pos.lnum = curwin->w_cursor.lnum;
21404 pos.col = (colnr_T)STRLEN(ml_get_curline());
21405 }
21406 return &pos;
21407 }
21408 return NULL;
21409}
21410
21411/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021412 * Convert list in "arg" into a position and optional file number.
21413 * When "fnump" is NULL there is no file number, only 3 items.
21414 * Note that the column is passed on as-is, the caller may want to decrement
21415 * it to use 1 for the first column.
21416 * Return FAIL when conversion is not possible, doesn't check the position for
21417 * validity.
21418 */
21419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021420list2fpos(
21421 typval_T *arg,
21422 pos_T *posp,
21423 int *fnump,
21424 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021425{
21426 list_T *l = arg->vval.v_list;
21427 long i = 0;
21428 long n;
21429
Bram Moolenaar493c1782014-05-28 14:34:46 +020021430 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21431 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021432 if (arg->v_type != VAR_LIST
21433 || l == NULL
21434 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021435 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021436 return FAIL;
21437
21438 if (fnump != NULL)
21439 {
21440 n = list_find_nr(l, i++, NULL); /* fnum */
21441 if (n < 0)
21442 return FAIL;
21443 if (n == 0)
21444 n = curbuf->b_fnum; /* current buffer */
21445 *fnump = n;
21446 }
21447
21448 n = list_find_nr(l, i++, NULL); /* lnum */
21449 if (n < 0)
21450 return FAIL;
21451 posp->lnum = n;
21452
21453 n = list_find_nr(l, i++, NULL); /* col */
21454 if (n < 0)
21455 return FAIL;
21456 posp->col = n;
21457
21458#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021459 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021460 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021461 posp->coladd = 0;
21462 else
21463 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021464#endif
21465
Bram Moolenaar493c1782014-05-28 14:34:46 +020021466 if (curswantp != NULL)
21467 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21468
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021469 return OK;
21470}
21471
21472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 * Get the length of an environment variable name.
21474 * Advance "arg" to the first character after the name.
21475 * Return 0 for error.
21476 */
21477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021478get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479{
21480 char_u *p;
21481 int len;
21482
21483 for (p = *arg; vim_isIDc(*p); ++p)
21484 ;
21485 if (p == *arg) /* no name found */
21486 return 0;
21487
21488 len = (int)(p - *arg);
21489 *arg = p;
21490 return len;
21491}
21492
21493/*
21494 * Get the length of the name of a function or internal variable.
21495 * "arg" is advanced to the first non-white character after the name.
21496 * Return 0 if something is wrong.
21497 */
21498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500{
21501 char_u *p;
21502 int len;
21503
21504 /* Find the end of the name. */
21505 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021506 {
21507 if (*p == ':')
21508 {
21509 /* "s:" is start of "s:var", but "n:" is not and can be used in
21510 * slice "[n:]". Also "xx:" is not a namespace. */
21511 len = (int)(p - *arg);
21512 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21513 || len > 1)
21514 break;
21515 }
21516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 if (p == *arg) /* no name found */
21518 return 0;
21519
21520 len = (int)(p - *arg);
21521 *arg = skipwhite(p);
21522
21523 return len;
21524}
21525
21526/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021527 * Get the length of the name of a variable or function.
21528 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021530 * Return -1 if curly braces expansion failed.
21531 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 * If the name contains 'magic' {}'s, expand them and return the
21533 * expanded name in an allocated string via 'alias' - caller must free.
21534 */
21535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021536get_name_len(
21537 char_u **arg,
21538 char_u **alias,
21539 int evaluate,
21540 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541{
21542 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 char_u *p;
21544 char_u *expr_start;
21545 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546
21547 *alias = NULL; /* default to no alias */
21548
21549 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21550 && (*arg)[2] == (int)KE_SNR)
21551 {
21552 /* hard coded <SNR>, already translated */
21553 *arg += 3;
21554 return get_id_len(arg) + 3;
21555 }
21556 len = eval_fname_script(*arg);
21557 if (len > 0)
21558 {
21559 /* literal "<SID>", "s:" or "<SNR>" */
21560 *arg += len;
21561 }
21562
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021564 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021566 p = find_name_end(*arg, &expr_start, &expr_end,
21567 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 if (expr_start != NULL)
21569 {
21570 char_u *temp_string;
21571
21572 if (!evaluate)
21573 {
21574 len += (int)(p - *arg);
21575 *arg = skipwhite(p);
21576 return len;
21577 }
21578
21579 /*
21580 * Include any <SID> etc in the expanded string:
21581 * Thus the -len here.
21582 */
21583 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21584 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021585 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 *alias = temp_string;
21587 *arg = skipwhite(p);
21588 return (int)STRLEN(temp_string);
21589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590
21591 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021592 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 EMSG2(_(e_invexpr2), *arg);
21594
21595 return len;
21596}
21597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021598/*
21599 * Find the end of a variable or function name, taking care of magic braces.
21600 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21601 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021602 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 * Return a pointer to just after the name. Equal to "arg" if there is no
21604 * valid name.
21605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607find_name_end(
21608 char_u *arg,
21609 char_u **expr_start,
21610 char_u **expr_end,
21611 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 int mb_nest = 0;
21614 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021616 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021618 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021620 *expr_start = NULL;
21621 *expr_end = NULL;
21622 }
21623
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021624 /* Quick check for valid starting character. */
21625 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21626 return arg;
21627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021628 for (p = arg; *p != NUL
21629 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021630 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021631 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021633 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021634 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021635 if (*p == '\'')
21636 {
21637 /* skip over 'string' to avoid counting [ and ] inside it. */
21638 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21639 ;
21640 if (*p == NUL)
21641 break;
21642 }
21643 else if (*p == '"')
21644 {
21645 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21646 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21647 if (*p == '\\' && p[1] != NUL)
21648 ++p;
21649 if (*p == NUL)
21650 break;
21651 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021652 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21653 {
21654 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021655 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021656 len = (int)(p - arg);
21657 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021658 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021659 break;
21660 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021662 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664 if (*p == '[')
21665 ++br_nest;
21666 else if (*p == ']')
21667 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021670 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021672 if (*p == '{')
21673 {
21674 mb_nest++;
21675 if (expr_start != NULL && *expr_start == NULL)
21676 *expr_start = p;
21677 }
21678 else if (*p == '}')
21679 {
21680 mb_nest--;
21681 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21682 *expr_end = p;
21683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 }
21686
21687 return p;
21688}
21689
21690/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021691 * Expands out the 'magic' {}'s in a variable/function name.
21692 * Note that this can call itself recursively, to deal with
21693 * constructs like foo{bar}{baz}{bam}
21694 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21695 * "in_start" ^
21696 * "expr_start" ^
21697 * "expr_end" ^
21698 * "in_end" ^
21699 *
21700 * Returns a new allocated string, which the caller must free.
21701 * Returns NULL for failure.
21702 */
21703 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021704make_expanded_name(
21705 char_u *in_start,
21706 char_u *expr_start,
21707 char_u *expr_end,
21708 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021709{
21710 char_u c1;
21711 char_u *retval = NULL;
21712 char_u *temp_result;
21713 char_u *nextcmd = NULL;
21714
21715 if (expr_end == NULL || in_end == NULL)
21716 return NULL;
21717 *expr_start = NUL;
21718 *expr_end = NUL;
21719 c1 = *in_end;
21720 *in_end = NUL;
21721
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021722 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021723 if (temp_result != NULL && nextcmd == NULL)
21724 {
21725 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21726 + (in_end - expr_end) + 1));
21727 if (retval != NULL)
21728 {
21729 STRCPY(retval, in_start);
21730 STRCAT(retval, temp_result);
21731 STRCAT(retval, expr_end + 1);
21732 }
21733 }
21734 vim_free(temp_result);
21735
21736 *in_end = c1; /* put char back for error messages */
21737 *expr_start = '{';
21738 *expr_end = '}';
21739
21740 if (retval != NULL)
21741 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021742 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021743 if (expr_start != NULL)
21744 {
21745 /* Further expansion! */
21746 temp_result = make_expanded_name(retval, expr_start,
21747 expr_end, temp_result);
21748 vim_free(retval);
21749 retval = temp_result;
21750 }
21751 }
21752
21753 return retval;
21754}
21755
21756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021758 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 */
21760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021761eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021763 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21764}
21765
21766/*
21767 * Return TRUE if character "c" can be used as the first character in a
21768 * variable or function name (excluding '{' and '}').
21769 */
21770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021771eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021772{
21773 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774}
21775
21776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 * Set number v: variable to "val".
21778 */
21779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021780set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021782 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783}
21784
21785/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021786 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 */
21788 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021789get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021791 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792}
21793
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021794/*
21795 * Get string v: variable value. Uses a static buffer, can only be used once.
21796 */
21797 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021798get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021799{
21800 return get_tv_string(&vimvars[idx].vv_tv);
21801}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021802
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021804 * Get List v: variable value. Caller must take care of reference count when
21805 * needed.
21806 */
21807 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021808get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021809{
21810 return vimvars[idx].vv_list;
21811}
21812
21813/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021814 * Set v:char to character "c".
21815 */
21816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021817set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021818{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021819 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021820
21821#ifdef FEAT_MBYTE
21822 if (has_mbyte)
21823 buf[(*mb_char2bytes)(c, buf)] = NUL;
21824 else
21825#endif
21826 {
21827 buf[0] = c;
21828 buf[1] = NUL;
21829 }
21830 set_vim_var_string(VV_CHAR, buf, -1);
21831}
21832
21833/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021834 * Set v:count to "count" and v:count1 to "count1".
21835 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 */
21837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838set_vcount(
21839 long count,
21840 long count1,
21841 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021843 if (set_prevcount)
21844 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021845 vimvars[VV_COUNT].vv_nr = count;
21846 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847}
21848
21849/*
21850 * Set string v: variable to a copy of "val".
21851 */
21852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021853set_vim_var_string(
21854 int idx,
21855 char_u *val,
21856 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857{
Bram Moolenaara542c682016-01-31 16:28:04 +010021858 clear_tv(&vimvars[idx].vv_di.di_tv);
21859 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021861 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021865 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866}
21867
21868/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021869 * Set List v: variable to "val".
21870 */
21871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021872set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021873{
Bram Moolenaara542c682016-01-31 16:28:04 +010021874 clear_tv(&vimvars[idx].vv_di.di_tv);
21875 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021876 vimvars[idx].vv_list = val;
21877 if (val != NULL)
21878 ++val->lv_refcount;
21879}
21880
21881/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021882 * Set Dictionary v: variable to "val".
21883 */
21884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021885set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021886{
21887 int todo;
21888 hashitem_T *hi;
21889
Bram Moolenaara542c682016-01-31 16:28:04 +010021890 clear_tv(&vimvars[idx].vv_di.di_tv);
21891 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021892 vimvars[idx].vv_dict = val;
21893 if (val != NULL)
21894 {
21895 ++val->dv_refcount;
21896
21897 /* Set readonly */
21898 todo = (int)val->dv_hashtab.ht_used;
21899 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21900 {
21901 if (HASHITEM_EMPTY(hi))
21902 continue;
21903 --todo;
21904 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21905 }
21906 }
21907}
21908
21909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 * Set v:register if needed.
21911 */
21912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914{
21915 char_u regname;
21916
21917 if (c == 0 || c == ' ')
21918 regname = '"';
21919 else
21920 regname = c;
21921 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 set_vim_var_string(VV_REG, &regname, 1);
21924}
21925
21926/*
21927 * Get or set v:exception. If "oldval" == NULL, return the current value.
21928 * Otherwise, restore the value to "oldval" and return NULL.
21929 * Must always be called in pairs to save and restore v:exception! Does not
21930 * take care of memory allocations.
21931 */
21932 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021933v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934{
21935 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937
Bram Moolenaare9a41262005-01-15 22:18:47 +000021938 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 return NULL;
21940}
21941
21942/*
21943 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21944 * Otherwise, restore the value to "oldval" and return NULL.
21945 * Must always be called in pairs to save and restore v:throwpoint! Does not
21946 * take care of memory allocations.
21947 */
21948 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021949v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
21951 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953
Bram Moolenaare9a41262005-01-15 22:18:47 +000021954 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 return NULL;
21956}
21957
21958#if defined(FEAT_AUTOCMD) || defined(PROTO)
21959/*
21960 * Set v:cmdarg.
21961 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21962 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21963 * Must always be called in pairs!
21964 */
21965 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021966set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967{
21968 char_u *oldval;
21969 char_u *newval;
21970 unsigned len;
21971
Bram Moolenaare9a41262005-01-15 22:18:47 +000021972 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021973 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021975 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021976 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021977 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 }
21979
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021980 if (eap->force_bin == FORCE_BIN)
21981 len = 6;
21982 else if (eap->force_bin == FORCE_NOBIN)
21983 len = 8;
21984 else
21985 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021986
21987 if (eap->read_edit)
21988 len += 7;
21989
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021990 if (eap->force_ff != 0)
21991 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21992# ifdef FEAT_MBYTE
21993 if (eap->force_enc != 0)
21994 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021995 if (eap->bad_char != 0)
21996 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021997# endif
21998
21999 newval = alloc(len + 1);
22000 if (newval == NULL)
22001 return NULL;
22002
22003 if (eap->force_bin == FORCE_BIN)
22004 sprintf((char *)newval, " ++bin");
22005 else if (eap->force_bin == FORCE_NOBIN)
22006 sprintf((char *)newval, " ++nobin");
22007 else
22008 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022009
22010 if (eap->read_edit)
22011 STRCAT(newval, " ++edit");
22012
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022013 if (eap->force_ff != 0)
22014 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22015 eap->cmd + eap->force_ff);
22016# ifdef FEAT_MBYTE
22017 if (eap->force_enc != 0)
22018 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22019 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022020 if (eap->bad_char == BAD_KEEP)
22021 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22022 else if (eap->bad_char == BAD_DROP)
22023 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22024 else if (eap->bad_char != 0)
22025 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022026# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022028 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029}
22030#endif
22031
22032/*
22033 * Get the value of internal variable "name".
22034 * Return OK or FAIL.
22035 */
22036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022037get_var_tv(
22038 char_u *name,
22039 int len, /* length of "name" */
22040 typval_T *rettv, /* NULL when only checking existence */
22041 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22042 int verbose, /* may give error message */
22043 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044{
22045 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022046 typval_T *tv = NULL;
22047 typval_T atv;
22048 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
22051 /* truncate the name, so that we can use strcmp() */
22052 cc = name[len];
22053 name[len] = NUL;
22054
22055 /*
22056 * Check for "b:changedtick".
22057 */
22058 if (STRCMP(name, "b:changedtick") == 0)
22059 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022060 atv.v_type = VAR_NUMBER;
22061 atv.vval.v_number = curbuf->b_changedtick;
22062 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 }
22064
22065 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 * Check for user-defined variables.
22067 */
22068 else
22069 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022070 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022072 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022073 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022074 if (dip != NULL)
22075 *dip = v;
22076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 }
22078
Bram Moolenaare9a41262005-01-15 22:18:47 +000022079 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022081 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022082 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 ret = FAIL;
22084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022085 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022086 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087
22088 name[len] = cc;
22089
22090 return ret;
22091}
22092
22093/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022094 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22095 * Also handle function call with Funcref variable: func(expr)
22096 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22097 */
22098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099handle_subscript(
22100 char_u **arg,
22101 typval_T *rettv,
22102 int evaluate, /* do more than finding the end */
22103 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022104{
22105 int ret = OK;
22106 dict_T *selfdict = NULL;
22107 char_u *s;
22108 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022109 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022110
22111 while (ret == OK
22112 && (**arg == '['
22113 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022114 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022115 && !vim_iswhite(*(*arg - 1)))
22116 {
22117 if (**arg == '(')
22118 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022119 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022120 if (evaluate)
22121 {
22122 functv = *rettv;
22123 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022124
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022125 /* Invoke the function. Recursive! */
22126 s = functv.vval.v_string;
22127 }
22128 else
22129 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022130 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022131 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22132 &len, evaluate, selfdict);
22133
22134 /* Clear the funcref afterwards, so that deleting it while
22135 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022136 if (evaluate)
22137 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022138
22139 /* Stop the expression evaluation when immediately aborting on
22140 * error, or when an interrupt occurred or an exception was thrown
22141 * but not caught. */
22142 if (aborting())
22143 {
22144 if (ret == OK)
22145 clear_tv(rettv);
22146 ret = FAIL;
22147 }
22148 dict_unref(selfdict);
22149 selfdict = NULL;
22150 }
22151 else /* **arg == '[' || **arg == '.' */
22152 {
22153 dict_unref(selfdict);
22154 if (rettv->v_type == VAR_DICT)
22155 {
22156 selfdict = rettv->vval.v_dict;
22157 if (selfdict != NULL)
22158 ++selfdict->dv_refcount;
22159 }
22160 else
22161 selfdict = NULL;
22162 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22163 {
22164 clear_tv(rettv);
22165 ret = FAIL;
22166 }
22167 }
22168 }
22169 dict_unref(selfdict);
22170 return ret;
22171}
22172
22173/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022174 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022175 * value).
22176 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022177 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022179{
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022181}
22182
22183/*
22184 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 * The string "s" must have been allocated, it is consumed.
22186 * Return NULL for out of memory, the variable otherwise.
22187 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022188 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022189alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190{
Bram Moolenaar33570922005-01-25 22:26:29 +000022191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022193 rettv = alloc_tv();
22194 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022196 rettv->v_type = VAR_STRING;
22197 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 }
22199 else
22200 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022201 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202}
22203
22204/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209{
22210 if (varp != NULL)
22211 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 switch (varp->v_type)
22213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022214 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022215 func_unref(varp->vval.v_string);
22216 /*FALLTHROUGH*/
22217 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022218 vim_free(varp->vval.v_string);
22219 break;
22220 case VAR_LIST:
22221 list_unref(varp->vval.v_list);
22222 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022223 case VAR_DICT:
22224 dict_unref(varp->vval.v_dict);
22225 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022226 case VAR_JOB:
22227#ifdef FEAT_JOB
22228 job_unref(varp->vval.v_job);
22229 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022230#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022231 case VAR_CHANNEL:
22232#ifdef FEAT_CHANNEL
22233 channel_unref(varp->vval.v_channel);
22234 break;
22235#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022236 case VAR_NUMBER:
22237 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022238 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022239 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022240 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 vim_free(varp);
22243 }
22244}
22245
22246/*
22247 * Free the memory for a variable value and set the value to NULL or 0.
22248 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022250clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251{
22252 if (varp != NULL)
22253 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022254 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022256 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022257 func_unref(varp->vval.v_string);
22258 /*FALLTHROUGH*/
22259 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022260 vim_free(varp->vval.v_string);
22261 varp->vval.v_string = NULL;
22262 break;
22263 case VAR_LIST:
22264 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022265 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022266 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022267 case VAR_DICT:
22268 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022269 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022270 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022271 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022272 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022273 varp->vval.v_number = 0;
22274 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022275 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022276#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022277 varp->vval.v_float = 0.0;
22278 break;
22279#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022280 case VAR_JOB:
22281#ifdef FEAT_JOB
22282 job_unref(varp->vval.v_job);
22283 varp->vval.v_job = NULL;
22284#endif
22285 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022286 case VAR_CHANNEL:
22287#ifdef FEAT_CHANNEL
22288 channel_unref(varp->vval.v_channel);
22289 varp->vval.v_channel = NULL;
22290#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022291 case VAR_UNKNOWN:
22292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022294 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 }
22296}
22297
22298/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022299 * Set the value of a variable to NULL without freeing items.
22300 */
22301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022302init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022303{
22304 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022305 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022306}
22307
22308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 * Get the number value of a variable.
22310 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022311 * For incompatible types, return 0.
22312 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22313 * caller of incompatible types: it sets *denote to TRUE if "denote"
22314 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 */
22316 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022317get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022319 int error = FALSE;
22320
22321 return get_tv_number_chk(varp, &error); /* return 0L on error */
22322}
22323
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022324 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022325get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022326{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022327 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022329 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022331 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022332 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022333 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022334#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022335 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022336 break;
22337#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022338 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022339 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022340 break;
22341 case VAR_STRING:
22342 if (varp->vval.v_string != NULL)
22343 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022344 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022345 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022346 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022347 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022348 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022350 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022351 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022352 case VAR_SPECIAL:
22353 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22354 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022355 case VAR_JOB:
22356#ifdef FEAT_JOB
22357 EMSG(_("E910: Using a Job as a Number"));
22358 break;
22359#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022360 case VAR_CHANNEL:
22361#ifdef FEAT_CHANNEL
22362 EMSG(_("E913: Using a Channel as a Number"));
22363 break;
22364#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022365 case VAR_UNKNOWN:
22366 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022369 if (denote == NULL) /* useful for values that must be unsigned */
22370 n = -1;
22371 else
22372 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022373 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374}
22375
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022376#ifdef FEAT_FLOAT
22377 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022378get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022379{
22380 switch (varp->v_type)
22381 {
22382 case VAR_NUMBER:
22383 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022384 case VAR_FLOAT:
22385 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022386 case VAR_FUNC:
22387 EMSG(_("E891: Using a Funcref as a Float"));
22388 break;
22389 case VAR_STRING:
22390 EMSG(_("E892: Using a String as a Float"));
22391 break;
22392 case VAR_LIST:
22393 EMSG(_("E893: Using a List as a Float"));
22394 break;
22395 case VAR_DICT:
22396 EMSG(_("E894: Using a Dictionary as a Float"));
22397 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022398 case VAR_SPECIAL:
22399 EMSG(_("E907: Using a special value as a Float"));
22400 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022401 case VAR_JOB:
22402# ifdef FEAT_JOB
22403 EMSG(_("E911: Using a Job as a Float"));
22404 break;
22405# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022406 case VAR_CHANNEL:
22407# ifdef FEAT_CHANNEL
22408 EMSG(_("E914: Using a Channel as a Float"));
22409 break;
22410# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022411 case VAR_UNKNOWN:
22412 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022413 break;
22414 }
22415 return 0;
22416}
22417#endif
22418
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022420 * Get the lnum from the first argument.
22421 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022422 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 */
22424 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022425get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426{
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 linenr_T lnum;
22429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022430 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 if (lnum == 0) /* no valid number, try using line() */
22432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 rettv.v_type = VAR_NUMBER;
22434 f_line(argvars, &rettv);
22435 lnum = rettv.vval.v_number;
22436 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437 }
22438 return lnum;
22439}
22440
22441/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022442 * Get the lnum from the first argument.
22443 * Also accepts "$", then "buf" is used.
22444 * Returns 0 on error.
22445 */
22446 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022448{
22449 if (argvars[0].v_type == VAR_STRING
22450 && argvars[0].vval.v_string != NULL
22451 && argvars[0].vval.v_string[0] == '$'
22452 && buf != NULL)
22453 return buf->b_ml.ml_line_count;
22454 return get_tv_number_chk(&argvars[0], NULL);
22455}
22456
22457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 * Get the string value of a variable.
22459 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022460 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22461 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 * If the String variable has never been set, return an empty string.
22463 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022464 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22465 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 */
22467 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022468get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469{
22470 static char_u mybuf[NUMBUFLEN];
22471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022472 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
22475 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022476get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022478 char_u *res = get_tv_string_buf_chk(varp, buf);
22479
22480 return res != NULL ? res : (char_u *)"";
22481}
22482
Bram Moolenaar7d647822014-04-05 21:28:56 +020022483/*
22484 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22485 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022487get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022488{
22489 static char_u mybuf[NUMBUFLEN];
22490
22491 return get_tv_string_buf_chk(varp, mybuf);
22492}
22493
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022494 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022495get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022496{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022497 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 case VAR_NUMBER:
22500 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22501 return buf;
22502 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022503 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022504 break;
22505 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022506 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022507 break;
22508 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022509 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022511 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022512#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022513 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022514 break;
22515#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022516 case VAR_STRING:
22517 if (varp->vval.v_string != NULL)
22518 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022519 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022520 case VAR_SPECIAL:
22521 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22522 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022523 case VAR_JOB:
22524#ifdef FEAT_JOB
22525 {
22526 job_T *job = varp->vval.v_job;
22527 char *status = job->jv_status == JOB_FAILED ? "fail"
22528 : job->jv_status == JOB_ENDED ? "dead"
22529 : "run";
22530# ifdef UNIX
22531 vim_snprintf((char *)buf, NUMBUFLEN,
22532 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022533# elif defined(WIN32)
22534 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022535 "process %ld %s",
22536 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022537 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022538# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022539 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022540 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22541# endif
22542 return buf;
22543 }
22544#endif
22545 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022546 case VAR_CHANNEL:
22547#ifdef FEAT_CHANNEL
22548 {
22549 channel_T *channel = varp->vval.v_channel;
22550 char *status = channel_status(channel);
22551
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022552 if (channel == NULL)
22553 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22554 else
22555 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022556 "channel %d %s", channel->ch_id, status);
22557 return buf;
22558 }
22559#endif
22560 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022561 case VAR_UNKNOWN:
22562 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022565 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566}
22567
22568/*
22569 * Find variable "name" in the list of variables.
22570 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022571 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022572 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022576find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580
Bram Moolenaara7043832005-01-21 11:56:39 +000022581 ht = find_var_ht(name, &varname);
22582 if (htp != NULL)
22583 *htp = ht;
22584 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022586 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587}
22588
22589/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022590 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022591 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022593 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022594find_var_in_ht(
22595 hashtab_T *ht,
22596 int htname,
22597 char_u *varname,
22598 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022599{
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 hashitem_T *hi;
22601
22602 if (*varname == NUL)
22603 {
22604 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022605 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022607 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 case 'g': return &globvars_var;
22609 case 'v': return &vimvars_var;
22610 case 'b': return &curbuf->b_bufvar;
22611 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022612#ifdef FEAT_WINDOWS
22613 case 't': return &curtab->tp_winvar;
22614#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022615 case 'l': return current_funccal == NULL
22616 ? NULL : &current_funccal->l_vars_var;
22617 case 'a': return current_funccal == NULL
22618 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 }
22620 return NULL;
22621 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022622
22623 hi = hash_find(ht, varname);
22624 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022625 {
22626 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022627 * worked find the variable again. Don't auto-load a script if it was
22628 * loaded already, otherwise it would be loaded every time when
22629 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022630 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022631 {
22632 /* Note: script_autoload() may make "hi" invalid. It must either
22633 * be obtained again or not used. */
22634 if (!script_autoload(varname, FALSE) || aborting())
22635 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022636 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022637 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022638 if (HASHITEM_EMPTY(hi))
22639 return NULL;
22640 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022641 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022642}
22643
22644/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022646 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022647 * Set "varname" to the start of name without ':'.
22648 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022649 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022650find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022652 hashitem_T *hi;
22653
Bram Moolenaar73627d02015-08-11 15:46:09 +020022654 if (name[0] == NUL)
22655 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 if (name[1] != ':')
22657 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022658 /* The name must not start with a colon or #. */
22659 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 return NULL;
22661 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022662
22663 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022664 hi = hash_find(&compat_hashtab, name);
22665 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022666 return &compat_hashtab;
22667
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022670 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 }
22672 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022673 if (*name == 'g') /* global variable */
22674 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022675 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22676 */
22677 if (vim_strchr(name + 2, ':') != NULL
22678 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022679 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022681 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022683 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022684#ifdef FEAT_WINDOWS
22685 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022686 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022687#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 if (*name == 'v') /* v: variable */
22689 return &vimvarht;
22690 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022691 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022693 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 if (*name == 's' /* script variable */
22695 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22696 return &SCRIPT_VARS(current_SID);
22697 return NULL;
22698}
22699
22700/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022701 * Get function call environment based on bactrace debug level
22702 */
22703 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022704get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022705{
22706 int i;
22707 funccall_T *funccal;
22708 funccall_T *temp_funccal;
22709
22710 funccal = current_funccal;
22711 if (debug_backtrace_level > 0)
22712 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022713 for (i = 0; i < debug_backtrace_level; i++)
22714 {
22715 temp_funccal = funccal->caller;
22716 if (temp_funccal)
22717 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022718 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022719 /* backtrace level overflow. reset to max */
22720 debug_backtrace_level = i;
22721 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022722 }
22723 return funccal;
22724}
22725
22726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022728 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 * Returns NULL when it doesn't exist.
22730 */
22731 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022732get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733{
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022736 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 if (v == NULL)
22738 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 * sourcing this script and when executing functions defined in the script.
22745 */
22746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022747new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748{
Bram Moolenaara7043832005-01-21 11:56:39 +000022749 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 hashtab_T *ht;
22751 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022752
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022755 /* Re-allocating ga_data means that an ht_array pointing to
22756 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022758 for (i = 1; i <= ga_scripts.ga_len; ++i)
22759 {
22760 ht = &SCRIPT_VARS(i);
22761 if (ht->ht_mask == HT_INIT_SIZE - 1)
22762 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022763 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022765 }
22766
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 while (ga_scripts.ga_len < id)
22768 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022769 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022770 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022771 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 }
22774 }
22775}
22776
22777/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22779 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 */
22781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022782init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783{
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022785 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022786 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022787 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022788 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 dict_var->di_tv.vval.v_dict = dict;
22790 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022791 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22793 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794}
22795
22796/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022797 * Unreference a dictionary initialized by init_var_dict().
22798 */
22799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022800unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022801{
22802 /* Now the dict needs to be freed if no one else is using it, go back to
22803 * normal reference counting. */
22804 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22805 dict_unref(dict);
22806}
22807
22808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 * Frees all allocated variables and the value they contain.
22811 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 */
22813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022814vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022815{
22816 vars_clear_ext(ht, TRUE);
22817}
22818
22819/*
22820 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22821 */
22822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022823vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824{
Bram Moolenaara7043832005-01-21 11:56:39 +000022825 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022826 hashitem_T *hi;
22827 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022830 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022831 for (hi = ht->ht_array; todo > 0; ++hi)
22832 {
22833 if (!HASHITEM_EMPTY(hi))
22834 {
22835 --todo;
22836
Bram Moolenaar33570922005-01-25 22:26:29 +000022837 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022838 * ht_array might change then. hash_clear() takes care of it
22839 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 v = HI2DI(hi);
22841 if (free_val)
22842 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022843 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022844 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022845 }
22846 }
22847 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022848 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849}
22850
Bram Moolenaara7043832005-01-21 11:56:39 +000022851/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 * Delete a variable from hashtab "ht" at item "hi".
22853 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022856delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857{
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022859
22860 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022861 clear_tv(&di->di_tv);
22862 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863}
22864
22865/*
22866 * List the value of one internal variable.
22867 */
22868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022869list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022871 char_u *tofree;
22872 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022873 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022874
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022875 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022876 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022877 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022878 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879}
22880
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022882list_one_var_a(
22883 char_u *prefix,
22884 char_u *name,
22885 int type,
22886 char_u *string,
22887 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888{
Bram Moolenaar31859182007-08-14 20:41:13 +000022889 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22890 msg_start();
22891 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 if (name != NULL) /* "a:" vars don't have a name stored */
22893 msg_puts(name);
22894 msg_putchar(' ');
22895 msg_advance(22);
22896 if (type == VAR_NUMBER)
22897 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022898 else if (type == VAR_FUNC)
22899 msg_putchar('*');
22900 else if (type == VAR_LIST)
22901 {
22902 msg_putchar('[');
22903 if (*string == '[')
22904 ++string;
22905 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022906 else if (type == VAR_DICT)
22907 {
22908 msg_putchar('{');
22909 if (*string == '{')
22910 ++string;
22911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 else
22913 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022914
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022916
22917 if (type == VAR_FUNC)
22918 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022919 if (*first)
22920 {
22921 msg_clr_eos();
22922 *first = FALSE;
22923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924}
22925
22926/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022927 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 * If the variable already exists, the value is updated.
22929 * Otherwise the variable is created.
22930 */
22931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022932set_var(
22933 char_u *name,
22934 typval_T *tv,
22935 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936{
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022941 ht = find_var_ht(name, &varname);
22942 if (ht == NULL || *varname == NUL)
22943 {
22944 EMSG2(_(e_illvar), name);
22945 return;
22946 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022947 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022948
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022949 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22950 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022951
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022954 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022955 if (var_check_ro(v->di_flags, name, FALSE)
22956 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022957 return;
22958 if (v->di_tv.v_type != tv->v_type
22959 && !((v->di_tv.v_type == VAR_STRING
22960 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022961 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022962 || tv->v_type == VAR_NUMBER))
22963#ifdef FEAT_FLOAT
22964 && !((v->di_tv.v_type == VAR_NUMBER
22965 || v->di_tv.v_type == VAR_FLOAT)
22966 && (tv->v_type == VAR_NUMBER
22967 || tv->v_type == VAR_FLOAT))
22968#endif
22969 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022970 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022971 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022972 return;
22973 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022974
22975 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022976 * Handle setting internal v: variables separately where needed to
22977 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 */
22979 if (ht == &vimvarht)
22980 {
22981 if (v->di_tv.v_type == VAR_STRING)
22982 {
22983 vim_free(v->di_tv.vval.v_string);
22984 if (copy || tv->v_type != VAR_STRING)
22985 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22986 else
22987 {
22988 /* Take over the string to avoid an extra alloc/free. */
22989 v->di_tv.vval.v_string = tv->vval.v_string;
22990 tv->vval.v_string = NULL;
22991 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022992 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022993 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022994 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022995 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022997 if (STRCMP(varname, "searchforward") == 0)
22998 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022999#ifdef FEAT_SEARCH_EXTRA
23000 else if (STRCMP(varname, "hlsearch") == 0)
23001 {
23002 no_hlsearch = !v->di_tv.vval.v_number;
23003 redraw_all_later(SOME_VALID);
23004 }
23005#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023006 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023007 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023008 else if (v->di_tv.v_type != tv->v_type)
23009 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023010 }
23011
23012 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 }
23014 else /* add a new variable */
23015 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023016 /* Can't add "v:" variable. */
23017 if (ht == &vimvarht)
23018 {
23019 EMSG2(_(e_illvar), name);
23020 return;
23021 }
23022
Bram Moolenaar92124a32005-06-17 22:03:40 +000023023 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023024 if (!valid_varname(varname))
23025 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023026
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023027 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23028 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023029 if (v == NULL)
23030 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023031 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023034 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 return;
23036 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023037 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023039
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023040 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023042 else
23043 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023045 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023046 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048}
23049
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023050/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023051 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023052 * Also give an error message.
23053 */
23054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023055var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023056{
23057 if (flags & DI_FLAGS_RO)
23058 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023059 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 return TRUE;
23061 }
23062 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23063 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023064 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 return TRUE;
23066 }
23067 return FALSE;
23068}
23069
23070/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023071 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23072 * Also give an error message.
23073 */
23074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023075var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023076{
23077 if (flags & DI_FLAGS_FIX)
23078 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023079 EMSG2(_("E795: Cannot delete variable %s"),
23080 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023081 return TRUE;
23082 }
23083 return FALSE;
23084}
23085
23086/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023087 * Check if a funcref is assigned to a valid variable name.
23088 * Return TRUE and give an error if not.
23089 */
23090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023091var_check_func_name(
23092 char_u *name, /* points to start of variable name */
23093 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023094{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023095 /* Allow for w: b: s: and t:. */
23096 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023097 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23098 ? name[2] : name[0]))
23099 {
23100 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23101 name);
23102 return TRUE;
23103 }
23104 /* Don't allow hiding a function. When "v" is not NULL we might be
23105 * assigning another function to the same var, the type is checked
23106 * below. */
23107 if (new_var && function_exists(name))
23108 {
23109 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23110 name);
23111 return TRUE;
23112 }
23113 return FALSE;
23114}
23115
23116/*
23117 * Check if a variable name is valid.
23118 * Return FALSE and give an error if not.
23119 */
23120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023121valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023122{
23123 char_u *p;
23124
23125 for (p = varname; *p != NUL; ++p)
23126 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23127 && *p != AUTOLOAD_CHAR)
23128 {
23129 EMSG2(_(e_illvar), varname);
23130 return FALSE;
23131 }
23132 return TRUE;
23133}
23134
23135/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023136 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023137 * Also give an error message, using "name" or _("name") when use_gettext is
23138 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023139 */
23140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023141tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023142{
23143 if (lock & VAR_LOCKED)
23144 {
23145 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023146 name == NULL ? (char_u *)_("Unknown")
23147 : use_gettext ? (char_u *)_(name)
23148 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023149 return TRUE;
23150 }
23151 if (lock & VAR_FIXED)
23152 {
23153 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023154 name == NULL ? (char_u *)_("Unknown")
23155 : use_gettext ? (char_u *)_(name)
23156 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023157 return TRUE;
23158 }
23159 return FALSE;
23160}
23161
23162/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023163 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023164 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023165 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023166 * It is OK for "from" and "to" to point to the same item. This is used to
23167 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023168 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023170copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023172 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023173 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023174 switch (from->v_type)
23175 {
23176 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023177 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023178 to->vval.v_number = from->vval.v_number;
23179 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023180 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023181#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023182 to->vval.v_float = from->vval.v_float;
23183 break;
23184#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023185 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023186#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023187 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023188 if (to->vval.v_job != NULL)
23189 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023190 break;
23191#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023192 case VAR_CHANNEL:
23193#ifdef FEAT_CHANNEL
23194 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023195 if (to->vval.v_channel != NULL)
23196 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023197 break;
23198#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023199 case VAR_STRING:
23200 case VAR_FUNC:
23201 if (from->vval.v_string == NULL)
23202 to->vval.v_string = NULL;
23203 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023205 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023206 if (from->v_type == VAR_FUNC)
23207 func_ref(to->vval.v_string);
23208 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209 break;
23210 case VAR_LIST:
23211 if (from->vval.v_list == NULL)
23212 to->vval.v_list = NULL;
23213 else
23214 {
23215 to->vval.v_list = from->vval.v_list;
23216 ++to->vval.v_list->lv_refcount;
23217 }
23218 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023219 case VAR_DICT:
23220 if (from->vval.v_dict == NULL)
23221 to->vval.v_dict = NULL;
23222 else
23223 {
23224 to->vval.v_dict = from->vval.v_dict;
23225 ++to->vval.v_dict->dv_refcount;
23226 }
23227 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023228 case VAR_UNKNOWN:
23229 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023230 break;
23231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232}
23233
23234/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023235 * Make a copy of an item.
23236 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023237 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23238 * reference to an already copied list/dict can be used.
23239 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023240 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023242item_copy(
23243 typval_T *from,
23244 typval_T *to,
23245 int deep,
23246 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023247{
23248 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023249 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023250
Bram Moolenaar33570922005-01-25 22:26:29 +000023251 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023252 {
23253 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023254 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023255 }
23256 ++recurse;
23257
23258 switch (from->v_type)
23259 {
23260 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023261 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023262 case VAR_STRING:
23263 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023264 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023265 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023266 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023267 copy_tv(from, to);
23268 break;
23269 case VAR_LIST:
23270 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023271 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023272 if (from->vval.v_list == NULL)
23273 to->vval.v_list = NULL;
23274 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23275 {
23276 /* use the copy made earlier */
23277 to->vval.v_list = from->vval.v_list->lv_copylist;
23278 ++to->vval.v_list->lv_refcount;
23279 }
23280 else
23281 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23282 if (to->vval.v_list == NULL)
23283 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023284 break;
23285 case VAR_DICT:
23286 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023287 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023288 if (from->vval.v_dict == NULL)
23289 to->vval.v_dict = NULL;
23290 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23291 {
23292 /* use the copy made earlier */
23293 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23294 ++to->vval.v_dict->dv_refcount;
23295 }
23296 else
23297 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23298 if (to->vval.v_dict == NULL)
23299 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023300 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023301 case VAR_UNKNOWN:
23302 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023303 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023304 }
23305 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023306 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023307}
23308
23309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 * ":echo expr1 ..." print each argument separated with a space, add a
23311 * newline at the end.
23312 * ":echon expr1 ..." print each argument plain.
23313 */
23314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023315ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316{
23317 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023319 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 char_u *p;
23321 int needclr = TRUE;
23322 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023323 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324
23325 if (eap->skip)
23326 ++emsg_skip;
23327 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23328 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023329 /* If eval1() causes an error message the text from the command may
23330 * still need to be cleared. E.g., "echo 22,44". */
23331 need_clr_eos = needclr;
23332
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023334 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 {
23336 /*
23337 * Report the invalid expression unless the expression evaluation
23338 * has been cancelled due to an aborting error, an interrupt, or an
23339 * exception.
23340 */
23341 if (!aborting())
23342 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023343 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 break;
23345 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023346 need_clr_eos = FALSE;
23347
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 if (!eap->skip)
23349 {
23350 if (atstart)
23351 {
23352 atstart = FALSE;
23353 /* Call msg_start() after eval1(), evaluating the expression
23354 * may cause a message to appear. */
23355 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023356 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023357 /* Mark the saved text as finishing the line, so that what
23358 * follows is displayed on a new line when scrolling back
23359 * at the more prompt. */
23360 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 }
23364 else if (eap->cmdidx == CMD_echo)
23365 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023366 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023367 if (p != NULL)
23368 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023370 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023372 if (*p != TAB && needclr)
23373 {
23374 /* remove any text still there from the command */
23375 msg_clr_eos();
23376 needclr = FALSE;
23377 }
23378 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 }
23380 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023381 {
23382#ifdef FEAT_MBYTE
23383 if (has_mbyte)
23384 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023385 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023386
23387 (void)msg_outtrans_len_attr(p, i, echo_attr);
23388 p += i - 1;
23389 }
23390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023392 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023395 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023397 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 arg = skipwhite(arg);
23399 }
23400 eap->nextcmd = check_nextcmd(arg);
23401
23402 if (eap->skip)
23403 --emsg_skip;
23404 else
23405 {
23406 /* remove text that may still be there from the command */
23407 if (needclr)
23408 msg_clr_eos();
23409 if (eap->cmdidx == CMD_echo)
23410 msg_end();
23411 }
23412}
23413
23414/*
23415 * ":echohl {name}".
23416 */
23417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023418ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419{
23420 int id;
23421
23422 id = syn_name2id(eap->arg);
23423 if (id == 0)
23424 echo_attr = 0;
23425 else
23426 echo_attr = syn_id2attr(id);
23427}
23428
23429/*
23430 * ":execute expr1 ..." execute the result of an expression.
23431 * ":echomsg expr1 ..." Print a message
23432 * ":echoerr expr1 ..." Print an error
23433 * Each gets spaces around each argument and a newline at the end for
23434 * echo commands
23435 */
23436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023437ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438{
23439 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023440 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 int ret = OK;
23442 char_u *p;
23443 garray_T ga;
23444 int len;
23445 int save_did_emsg;
23446
23447 ga_init2(&ga, 1, 80);
23448
23449 if (eap->skip)
23450 ++emsg_skip;
23451 while (*arg != NUL && *arg != '|' && *arg != '\n')
23452 {
23453 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023454 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 {
23456 /*
23457 * Report the invalid expression unless the expression evaluation
23458 * has been cancelled due to an aborting error, an interrupt, or an
23459 * exception.
23460 */
23461 if (!aborting())
23462 EMSG2(_(e_invexpr2), p);
23463 ret = FAIL;
23464 break;
23465 }
23466
23467 if (!eap->skip)
23468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023469 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 len = (int)STRLEN(p);
23471 if (ga_grow(&ga, len + 2) == FAIL)
23472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023473 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 ret = FAIL;
23475 break;
23476 }
23477 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 ga.ga_len += len;
23481 }
23482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023483 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 arg = skipwhite(arg);
23485 }
23486
23487 if (ret != FAIL && ga.ga_data != NULL)
23488 {
23489 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023492 out_flush();
23493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 else if (eap->cmdidx == CMD_echoerr)
23495 {
23496 /* We don't want to abort following commands, restore did_emsg. */
23497 save_did_emsg = did_emsg;
23498 EMSG((char_u *)ga.ga_data);
23499 if (!force_abort)
23500 did_emsg = save_did_emsg;
23501 }
23502 else if (eap->cmdidx == CMD_execute)
23503 do_cmdline((char_u *)ga.ga_data,
23504 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23505 }
23506
23507 ga_clear(&ga);
23508
23509 if (eap->skip)
23510 --emsg_skip;
23511
23512 eap->nextcmd = check_nextcmd(arg);
23513}
23514
23515/*
23516 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23517 * "arg" points to the "&" or '+' when called, to "option" when returning.
23518 * Returns NULL when no option name found. Otherwise pointer to the char
23519 * after the option name.
23520 */
23521 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023522find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523{
23524 char_u *p = *arg;
23525
23526 ++p;
23527 if (*p == 'g' && p[1] == ':')
23528 {
23529 *opt_flags = OPT_GLOBAL;
23530 p += 2;
23531 }
23532 else if (*p == 'l' && p[1] == ':')
23533 {
23534 *opt_flags = OPT_LOCAL;
23535 p += 2;
23536 }
23537 else
23538 *opt_flags = 0;
23539
23540 if (!ASCII_ISALPHA(*p))
23541 return NULL;
23542 *arg = p;
23543
23544 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23545 p += 4; /* termcap option */
23546 else
23547 while (ASCII_ISALPHA(*p))
23548 ++p;
23549 return p;
23550}
23551
23552/*
23553 * ":function"
23554 */
23555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023556ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557{
23558 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023559 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 int j;
23561 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023563 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 char_u *name = NULL;
23565 char_u *p;
23566 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023567 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 garray_T newargs;
23569 garray_T newlines;
23570 int varargs = FALSE;
23571 int mustend = FALSE;
23572 int flags = 0;
23573 ufunc_T *fp;
23574 int indent;
23575 int nesting;
23576 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023577 dictitem_T *v;
23578 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579 static int func_nr = 0; /* number for nameless function */
23580 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023581 hashtab_T *ht;
23582 int todo;
23583 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023584 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585
23586 /*
23587 * ":function" without argument: list functions.
23588 */
23589 if (ends_excmd(*eap->arg))
23590 {
23591 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023592 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023593 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023594 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023595 {
23596 if (!HASHITEM_EMPTY(hi))
23597 {
23598 --todo;
23599 fp = HI2UF(hi);
23600 if (!isdigit(*fp->uf_name))
23601 list_func_head(fp, FALSE);
23602 }
23603 }
23604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 eap->nextcmd = check_nextcmd(eap->arg);
23606 return;
23607 }
23608
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023609 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023610 * ":function /pat": list functions matching pattern.
23611 */
23612 if (*eap->arg == '/')
23613 {
23614 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23615 if (!eap->skip)
23616 {
23617 regmatch_T regmatch;
23618
23619 c = *p;
23620 *p = NUL;
23621 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23622 *p = c;
23623 if (regmatch.regprog != NULL)
23624 {
23625 regmatch.rm_ic = p_ic;
23626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023627 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023628 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23629 {
23630 if (!HASHITEM_EMPTY(hi))
23631 {
23632 --todo;
23633 fp = HI2UF(hi);
23634 if (!isdigit(*fp->uf_name)
23635 && vim_regexec(&regmatch, fp->uf_name, 0))
23636 list_func_head(fp, FALSE);
23637 }
23638 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023639 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023640 }
23641 }
23642 if (*p == '/')
23643 ++p;
23644 eap->nextcmd = check_nextcmd(p);
23645 return;
23646 }
23647
23648 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023649 * Get the function name. There are these situations:
23650 * func normal function name
23651 * "name" == func, "fudi.fd_dict" == NULL
23652 * dict.func new dictionary entry
23653 * "name" == NULL, "fudi.fd_dict" set,
23654 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23655 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023656 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23658 * dict.func existing dict entry that's not a Funcref
23659 * "name" == NULL, "fudi.fd_dict" set,
23660 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023661 * s:func script-local function name
23662 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023665 name = trans_function_name(&p, eap->skip, 0, &fudi);
23666 paren = (vim_strchr(p, '(') != NULL);
23667 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 {
23669 /*
23670 * Return on an invalid expression in braces, unless the expression
23671 * evaluation has been cancelled due to an aborting error, an
23672 * interrupt, or an exception.
23673 */
23674 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023676 if (!eap->skip && fudi.fd_newkey != NULL)
23677 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 else
23682 eap->skip = TRUE;
23683 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023684
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 /* An error in a function call during evaluation of an expression in magic
23686 * braces should not cause the function not to be defined. */
23687 saved_did_emsg = did_emsg;
23688 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689
23690 /*
23691 * ":function func" with only function name: list function.
23692 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023693 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 {
23695 if (!ends_excmd(*skipwhite(p)))
23696 {
23697 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023698 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 }
23700 eap->nextcmd = check_nextcmd(p);
23701 if (eap->nextcmd != NULL)
23702 *p = NUL;
23703 if (!eap->skip && !got_int)
23704 {
23705 fp = find_func(name);
23706 if (fp != NULL)
23707 {
23708 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023709 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023711 if (FUNCLINE(fp, j) == NULL)
23712 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 msg_putchar('\n');
23714 msg_outnum((long)(j + 1));
23715 if (j < 9)
23716 msg_putchar(' ');
23717 if (j < 99)
23718 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023719 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 out_flush(); /* show a line at a time */
23721 ui_breakcheck();
23722 }
23723 if (!got_int)
23724 {
23725 msg_putchar('\n');
23726 msg_puts((char_u *)" endfunction");
23727 }
23728 }
23729 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023730 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023732 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 }
23734
23735 /*
23736 * ":function name(arg1, arg2)" Define function.
23737 */
23738 p = skipwhite(p);
23739 if (*p != '(')
23740 {
23741 if (!eap->skip)
23742 {
23743 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023744 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 }
23746 /* attempt to continue by skipping some text */
23747 if (vim_strchr(p, '(') != NULL)
23748 p = vim_strchr(p, '(');
23749 }
23750 p = skipwhite(p + 1);
23751
23752 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23753 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23754
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023755 if (!eap->skip)
23756 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023757 /* Check the name of the function. Unless it's a dictionary function
23758 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023759 if (name != NULL)
23760 arg = name;
23761 else
23762 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023763 if (arg != NULL && (fudi.fd_di == NULL
23764 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023765 {
23766 if (*arg == K_SPECIAL)
23767 j = 3;
23768 else
23769 j = 0;
23770 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23771 : eval_isnamec(arg[j])))
23772 ++j;
23773 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023774 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023775 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023776 /* Disallow using the g: dict. */
23777 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23778 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023779 }
23780
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 /*
23782 * Isolate the arguments: "arg1, arg2, ...)"
23783 */
23784 while (*p != ')')
23785 {
23786 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23787 {
23788 varargs = TRUE;
23789 p += 3;
23790 mustend = TRUE;
23791 }
23792 else
23793 {
23794 arg = p;
23795 while (ASCII_ISALNUM(*p) || *p == '_')
23796 ++p;
23797 if (arg == p || isdigit(*arg)
23798 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23799 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23800 {
23801 if (!eap->skip)
23802 EMSG2(_("E125: Illegal argument: %s"), arg);
23803 break;
23804 }
23805 if (ga_grow(&newargs, 1) == FAIL)
23806 goto erret;
23807 c = *p;
23808 *p = NUL;
23809 arg = vim_strsave(arg);
23810 if (arg == NULL)
23811 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023812
23813 /* Check for duplicate argument name. */
23814 for (i = 0; i < newargs.ga_len; ++i)
23815 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23816 {
23817 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023818 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023819 goto erret;
23820 }
23821
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23823 *p = c;
23824 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 if (*p == ',')
23826 ++p;
23827 else
23828 mustend = TRUE;
23829 }
23830 p = skipwhite(p);
23831 if (mustend && *p != ')')
23832 {
23833 if (!eap->skip)
23834 EMSG2(_(e_invarg2), eap->arg);
23835 break;
23836 }
23837 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023838 if (*p != ')')
23839 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 ++p; /* skip the ')' */
23841
Bram Moolenaare9a41262005-01-15 22:18:47 +000023842 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 for (;;)
23844 {
23845 p = skipwhite(p);
23846 if (STRNCMP(p, "range", 5) == 0)
23847 {
23848 flags |= FC_RANGE;
23849 p += 5;
23850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023851 else if (STRNCMP(p, "dict", 4) == 0)
23852 {
23853 flags |= FC_DICT;
23854 p += 4;
23855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 else if (STRNCMP(p, "abort", 5) == 0)
23857 {
23858 flags |= FC_ABORT;
23859 p += 5;
23860 }
23861 else
23862 break;
23863 }
23864
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023865 /* When there is a line break use what follows for the function body.
23866 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23867 if (*p == '\n')
23868 line_arg = p + 1;
23869 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 EMSG(_(e_trailing));
23871
23872 /*
23873 * Read the body of the function, until ":endfunction" is found.
23874 */
23875 if (KeyTyped)
23876 {
23877 /* Check if the function already exists, don't let the user type the
23878 * whole function before telling him it doesn't work! For a script we
23879 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 if (!eap->skip && !eap->forceit)
23881 {
23882 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23883 EMSG(_(e_funcdict));
23884 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023885 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023888 if (!eap->skip && did_emsg)
23889 goto erret;
23890
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 msg_putchar('\n'); /* don't overwrite the function name */
23892 cmdline_row = msg_row;
23893 }
23894
23895 indent = 2;
23896 nesting = 0;
23897 for (;;)
23898 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023899 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023900 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023901 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023902 saved_wait_return = FALSE;
23903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023905 sourcing_lnum_off = sourcing_lnum;
23906
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023907 if (line_arg != NULL)
23908 {
23909 /* Use eap->arg, split up in parts by line breaks. */
23910 theline = line_arg;
23911 p = vim_strchr(theline, '\n');
23912 if (p == NULL)
23913 line_arg += STRLEN(line_arg);
23914 else
23915 {
23916 *p = NUL;
23917 line_arg = p + 1;
23918 }
23919 }
23920 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921 theline = getcmdline(':', 0L, indent);
23922 else
23923 theline = eap->getline(':', eap->cookie, indent);
23924 if (KeyTyped)
23925 lines_left = Rows - 1;
23926 if (theline == NULL)
23927 {
23928 EMSG(_("E126: Missing :endfunction"));
23929 goto erret;
23930 }
23931
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023932 /* Detect line continuation: sourcing_lnum increased more than one. */
23933 if (sourcing_lnum > sourcing_lnum_off + 1)
23934 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23935 else
23936 sourcing_lnum_off = 0;
23937
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 if (skip_until != NULL)
23939 {
23940 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23941 * don't check for ":endfunc". */
23942 if (STRCMP(theline, skip_until) == 0)
23943 {
23944 vim_free(skip_until);
23945 skip_until = NULL;
23946 }
23947 }
23948 else
23949 {
23950 /* skip ':' and blanks*/
23951 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23952 ;
23953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023954 /* Check for "endfunction". */
23955 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023957 if (line_arg == NULL)
23958 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 break;
23960 }
23961
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023962 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 * at "end". */
23964 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23965 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023966 else if (STRNCMP(p, "if", 2) == 0
23967 || STRNCMP(p, "wh", 2) == 0
23968 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 || STRNCMP(p, "try", 3) == 0)
23970 indent += 2;
23971
23972 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023973 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023975 if (*p == '!')
23976 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023978 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23979 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023981 ++nesting;
23982 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 }
23984 }
23985
23986 /* Check for ":append" or ":insert". */
23987 p = skip_range(p, NULL);
23988 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23989 || (p[0] == 'i'
23990 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23991 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23992 skip_until = vim_strsave((char_u *)".");
23993
23994 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23995 arg = skipwhite(skiptowhite(p));
23996 if (arg[0] == '<' && arg[1] =='<'
23997 && ((p[0] == 'p' && p[1] == 'y'
23998 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23999 || (p[0] == 'p' && p[1] == 'e'
24000 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24001 || (p[0] == 't' && p[1] == 'c'
24002 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024003 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24004 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24006 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024007 || (p[0] == 'm' && p[1] == 'z'
24008 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 ))
24010 {
24011 /* ":python <<" continues until a dot, like ":append" */
24012 p = skipwhite(arg + 2);
24013 if (*p == NUL)
24014 skip_until = vim_strsave((char_u *)".");
24015 else
24016 skip_until = vim_strsave(p);
24017 }
24018 }
24019
24020 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024021 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024022 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024023 if (line_arg == NULL)
24024 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024026 }
24027
24028 /* Copy the line to newly allocated memory. get_one_sourceline()
24029 * allocates 250 bytes per line, this saves 80% on average. The cost
24030 * is an extra alloc/free. */
24031 p = vim_strsave(theline);
24032 if (p != NULL)
24033 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024034 if (line_arg == NULL)
24035 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024036 theline = p;
24037 }
24038
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024039 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24040
24041 /* Add NULL lines for continuation lines, so that the line count is
24042 * equal to the index in the growarray. */
24043 while (sourcing_lnum_off-- > 0)
24044 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024045
24046 /* Check for end of eap->arg. */
24047 if (line_arg != NULL && *line_arg == NUL)
24048 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 }
24050
24051 /* Don't define the function when skipping commands or when an error was
24052 * detected. */
24053 if (eap->skip || did_emsg)
24054 goto erret;
24055
24056 /*
24057 * If there are no errors, add the function
24058 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024059 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024060 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024061 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024063 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024064 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024065 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066 goto erret;
24067 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069 fp = find_func(name);
24070 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024072 if (!eap->forceit)
24073 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024074 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075 goto erret;
24076 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024077 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024079 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080 name);
24081 goto erret;
24082 }
24083 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024084 ga_clear_strings(&(fp->uf_args));
24085 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024086 vim_free(name);
24087 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 }
24090 else
24091 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024092 char numbuf[20];
24093
24094 fp = NULL;
24095 if (fudi.fd_newkey == NULL && !eap->forceit)
24096 {
24097 EMSG(_(e_funcdict));
24098 goto erret;
24099 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024100 if (fudi.fd_di == NULL)
24101 {
24102 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024103 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024104 goto erret;
24105 }
24106 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024107 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024108 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024109
24110 /* Give the function a sequential number. Can only be used with a
24111 * Funcref! */
24112 vim_free(name);
24113 sprintf(numbuf, "%d", ++func_nr);
24114 name = vim_strsave((char_u *)numbuf);
24115 if (name == NULL)
24116 goto erret;
24117 }
24118
24119 if (fp == NULL)
24120 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024121 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024122 {
24123 int slen, plen;
24124 char_u *scriptname;
24125
24126 /* Check that the autoload name matches the script name. */
24127 j = FAIL;
24128 if (sourcing_name != NULL)
24129 {
24130 scriptname = autoload_name(name);
24131 if (scriptname != NULL)
24132 {
24133 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024134 plen = (int)STRLEN(p);
24135 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024136 if (slen > plen && fnamecmp(p,
24137 sourcing_name + slen - plen) == 0)
24138 j = OK;
24139 vim_free(scriptname);
24140 }
24141 }
24142 if (j == FAIL)
24143 {
24144 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24145 goto erret;
24146 }
24147 }
24148
24149 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 if (fp == NULL)
24151 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024152
24153 if (fudi.fd_dict != NULL)
24154 {
24155 if (fudi.fd_di == NULL)
24156 {
24157 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024158 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024159 if (fudi.fd_di == NULL)
24160 {
24161 vim_free(fp);
24162 goto erret;
24163 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024164 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24165 {
24166 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024167 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024168 goto erret;
24169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024170 }
24171 else
24172 /* overwrite existing dict entry */
24173 clear_tv(&fudi.fd_di->di_tv);
24174 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024175 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024176 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024177 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024178
24179 /* behave like "dict" was used */
24180 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024181 }
24182
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024185 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24186 {
24187 vim_free(fp);
24188 goto erret;
24189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024191 fp->uf_args = newargs;
24192 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024193#ifdef FEAT_PROFILE
24194 fp->uf_tml_count = NULL;
24195 fp->uf_tml_total = NULL;
24196 fp->uf_tml_self = NULL;
24197 fp->uf_profiling = FALSE;
24198 if (prof_def_func())
24199 func_do_profile(fp);
24200#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024201 fp->uf_varargs = varargs;
24202 fp->uf_flags = flags;
24203 fp->uf_calls = 0;
24204 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206
24207erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 ga_clear_strings(&newargs);
24209 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210ret_free:
24211 vim_free(skip_until);
24212 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024215 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216}
24217
24218/*
24219 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024220 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024222 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024223 * TFN_INT: internal function name OK
24224 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024225 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 * Advances "pp" to just after the function name (if no error).
24227 */
24228 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024229trans_function_name(
24230 char_u **pp,
24231 int skip, /* only find the end, don't evaluate */
24232 int flags,
24233 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024235 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 char_u *start;
24237 char_u *end;
24238 int lead;
24239 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024241 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024242
24243 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024244 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024246
24247 /* Check for hard coded <SNR>: already translated function ID (from a user
24248 * command). */
24249 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24250 && (*pp)[2] == (int)KE_SNR)
24251 {
24252 *pp += 3;
24253 len = get_id_len(pp) + 3;
24254 return vim_strnsave(start, len);
24255 }
24256
24257 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24258 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024260 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024262
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024263 /* Note that TFN_ flags use the same values as GLV_ flags. */
24264 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024265 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024266 if (end == start)
24267 {
24268 if (!skip)
24269 EMSG(_("E129: Function name required"));
24270 goto theend;
24271 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024272 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024273 {
24274 /*
24275 * Report an invalid expression in braces, unless the expression
24276 * evaluation has been cancelled due to an aborting error, an
24277 * interrupt, or an exception.
24278 */
24279 if (!aborting())
24280 {
24281 if (end != NULL)
24282 EMSG2(_(e_invarg2), start);
24283 }
24284 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024285 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024286 goto theend;
24287 }
24288
24289 if (lv.ll_tv != NULL)
24290 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024291 if (fdp != NULL)
24292 {
24293 fdp->fd_dict = lv.ll_dict;
24294 fdp->fd_newkey = lv.ll_newkey;
24295 lv.ll_newkey = NULL;
24296 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024297 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024298 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24299 {
24300 name = vim_strsave(lv.ll_tv->vval.v_string);
24301 *pp = end;
24302 }
24303 else
24304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024305 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24306 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024307 EMSG(_(e_funcref));
24308 else
24309 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024310 name = NULL;
24311 }
24312 goto theend;
24313 }
24314
24315 if (lv.ll_name == NULL)
24316 {
24317 /* Error found, but continue after the function name. */
24318 *pp = end;
24319 goto theend;
24320 }
24321
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024322 /* Check if the name is a Funcref. If so, use the value. */
24323 if (lv.ll_exp_name != NULL)
24324 {
24325 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024326 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024327 if (name == lv.ll_exp_name)
24328 name = NULL;
24329 }
24330 else
24331 {
24332 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024333 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024334 if (name == *pp)
24335 name = NULL;
24336 }
24337 if (name != NULL)
24338 {
24339 name = vim_strsave(name);
24340 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024341 if (STRNCMP(name, "<SNR>", 5) == 0)
24342 {
24343 /* Change "<SNR>" to the byte sequence. */
24344 name[0] = K_SPECIAL;
24345 name[1] = KS_EXTRA;
24346 name[2] = (int)KE_SNR;
24347 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24348 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024349 goto theend;
24350 }
24351
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024352 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024353 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024354 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024355 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24356 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24357 {
24358 /* When there was "s:" already or the name expanded to get a
24359 * leading "s:" then remove it. */
24360 lv.ll_name += 2;
24361 len -= 2;
24362 lead = 2;
24363 }
24364 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024365 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024366 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024367 /* skip over "s:" and "g:" */
24368 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024369 lv.ll_name += 2;
24370 len = (int)(end - lv.ll_name);
24371 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024372
24373 /*
24374 * Copy the function name to allocated memory.
24375 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24376 * Accept <SNR>123_name() outside a script.
24377 */
24378 if (skip)
24379 lead = 0; /* do nothing */
24380 else if (lead > 0)
24381 {
24382 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024383 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24384 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024385 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024386 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024387 if (current_SID <= 0)
24388 {
24389 EMSG(_(e_usingsid));
24390 goto theend;
24391 }
24392 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24393 lead += (int)STRLEN(sid_buf);
24394 }
24395 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024396 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024397 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024398 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024399 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024400 goto theend;
24401 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024402 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024403 {
24404 char_u *cp = vim_strchr(lv.ll_name, ':');
24405
24406 if (cp != NULL && cp < end)
24407 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024408 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024409 goto theend;
24410 }
24411 }
24412
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024413 name = alloc((unsigned)(len + lead + 1));
24414 if (name != NULL)
24415 {
24416 if (lead > 0)
24417 {
24418 name[0] = K_SPECIAL;
24419 name[1] = KS_EXTRA;
24420 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024421 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024422 STRCPY(name + 3, sid_buf);
24423 }
24424 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024425 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024426 }
24427 *pp = end;
24428
24429theend:
24430 clear_lval(&lv);
24431 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024432}
24433
24434/*
24435 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24436 * Return 2 if "p" starts with "s:".
24437 * Return 0 otherwise.
24438 */
24439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024440eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024442 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24443 * the standard library function. */
24444 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24445 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 return 5;
24447 if (p[0] == 's' && p[1] == ':')
24448 return 2;
24449 return 0;
24450}
24451
24452/*
24453 * Return TRUE if "p" starts with "<SID>" or "s:".
24454 * Only works if eval_fname_script() returned non-zero for "p"!
24455 */
24456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024457eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458{
24459 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24460}
24461
24462/*
24463 * List the head of the function: "name(arg1, arg2)".
24464 */
24465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024466list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467{
24468 int j;
24469
24470 msg_start();
24471 if (indent)
24472 MSG_PUTS(" ");
24473 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024474 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 {
24476 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024477 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478 }
24479 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024480 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024482 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 {
24484 if (j)
24485 MSG_PUTS(", ");
24486 msg_puts(FUNCARG(fp, j));
24487 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024488 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 {
24490 if (j)
24491 MSG_PUTS(", ");
24492 MSG_PUTS("...");
24493 }
24494 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024495 if (fp->uf_flags & FC_ABORT)
24496 MSG_PUTS(" abort");
24497 if (fp->uf_flags & FC_RANGE)
24498 MSG_PUTS(" range");
24499 if (fp->uf_flags & FC_DICT)
24500 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024501 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024502 if (p_verbose > 0)
24503 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504}
24505
24506/*
24507 * Find a function by name, return pointer to it in ufuncs.
24508 * Return NULL for unknown function.
24509 */
24510 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024511find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024513 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024515 hi = hash_find(&func_hashtab, name);
24516 if (!HASHITEM_EMPTY(hi))
24517 return HI2UF(hi);
24518 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519}
24520
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024521#if defined(EXITFREE) || defined(PROTO)
24522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024523free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024524{
24525 hashitem_T *hi;
24526
24527 /* Need to start all over every time, because func_free() may change the
24528 * hash table. */
24529 while (func_hashtab.ht_used > 0)
24530 for (hi = func_hashtab.ht_array; ; ++hi)
24531 if (!HASHITEM_EMPTY(hi))
24532 {
24533 func_free(HI2UF(hi));
24534 break;
24535 }
24536}
24537#endif
24538
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024540translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024541{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024542 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024543 return find_internal_func(name) >= 0;
24544 return find_func(name) != NULL;
24545}
24546
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024547/*
24548 * Return TRUE if a function "name" exists.
24549 */
24550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024551function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024552{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024553 char_u *nm = name;
24554 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024555 int n = FALSE;
24556
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024557 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24558 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024559 nm = skipwhite(nm);
24560
24561 /* Only accept "funcname", "funcname ", "funcname (..." and
24562 * "funcname(...", not "funcname!...". */
24563 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024564 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024565 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024566 return n;
24567}
24568
Bram Moolenaara1544c02013-05-30 12:35:52 +020024569 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024570get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024571{
24572 char_u *nm = name;
24573 char_u *p;
24574
24575 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24576
24577 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024578 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024579 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024580
Bram Moolenaara1544c02013-05-30 12:35:52 +020024581 vim_free(p);
24582 return NULL;
24583}
24584
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024585/*
24586 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024587 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24588 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024589 */
24590 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024591builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024592{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024593 char_u *p;
24594
24595 if (!ASCII_ISLOWER(name[0]))
24596 return FALSE;
24597 p = vim_strchr(name, AUTOLOAD_CHAR);
24598 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024599}
24600
Bram Moolenaar05159a02005-02-26 23:04:13 +000024601#if defined(FEAT_PROFILE) || defined(PROTO)
24602/*
24603 * Start profiling function "fp".
24604 */
24605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024606func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024607{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024608 int len = fp->uf_lines.ga_len;
24609
24610 if (len == 0)
24611 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024612 fp->uf_tm_count = 0;
24613 profile_zero(&fp->uf_tm_self);
24614 profile_zero(&fp->uf_tm_total);
24615 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024616 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024617 if (fp->uf_tml_total == NULL)
24618 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024619 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024620 if (fp->uf_tml_self == NULL)
24621 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024622 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024623 fp->uf_tml_idx = -1;
24624 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24625 || fp->uf_tml_self == NULL)
24626 return; /* out of memory */
24627
24628 fp->uf_profiling = TRUE;
24629}
24630
24631/*
24632 * Dump the profiling results for all functions in file "fd".
24633 */
24634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024635func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024636{
24637 hashitem_T *hi;
24638 int todo;
24639 ufunc_T *fp;
24640 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024641 ufunc_T **sorttab;
24642 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024644 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024645 if (todo == 0)
24646 return; /* nothing to dump */
24647
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024648 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024649
Bram Moolenaar05159a02005-02-26 23:04:13 +000024650 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24651 {
24652 if (!HASHITEM_EMPTY(hi))
24653 {
24654 --todo;
24655 fp = HI2UF(hi);
24656 if (fp->uf_profiling)
24657 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024658 if (sorttab != NULL)
24659 sorttab[st_len++] = fp;
24660
Bram Moolenaar05159a02005-02-26 23:04:13 +000024661 if (fp->uf_name[0] == K_SPECIAL)
24662 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24663 else
24664 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24665 if (fp->uf_tm_count == 1)
24666 fprintf(fd, "Called 1 time\n");
24667 else
24668 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24669 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24670 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24671 fprintf(fd, "\n");
24672 fprintf(fd, "count total (s) self (s)\n");
24673
24674 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24675 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024676 if (FUNCLINE(fp, i) == NULL)
24677 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024678 prof_func_line(fd, fp->uf_tml_count[i],
24679 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024680 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24681 }
24682 fprintf(fd, "\n");
24683 }
24684 }
24685 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024686
24687 if (sorttab != NULL && st_len > 0)
24688 {
24689 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24690 prof_total_cmp);
24691 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24692 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24693 prof_self_cmp);
24694 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24695 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024696
24697 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024698}
Bram Moolenaar73830342005-02-28 22:48:19 +000024699
24700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701prof_sort_list(
24702 FILE *fd,
24703 ufunc_T **sorttab,
24704 int st_len,
24705 char *title,
24706 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024707{
24708 int i;
24709 ufunc_T *fp;
24710
24711 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24712 fprintf(fd, "count total (s) self (s) function\n");
24713 for (i = 0; i < 20 && i < st_len; ++i)
24714 {
24715 fp = sorttab[i];
24716 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24717 prefer_self);
24718 if (fp->uf_name[0] == K_SPECIAL)
24719 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24720 else
24721 fprintf(fd, " %s()\n", fp->uf_name);
24722 }
24723 fprintf(fd, "\n");
24724}
24725
24726/*
24727 * Print the count and times for one function or function line.
24728 */
24729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024730prof_func_line(
24731 FILE *fd,
24732 int count,
24733 proftime_T *total,
24734 proftime_T *self,
24735 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024736{
24737 if (count > 0)
24738 {
24739 fprintf(fd, "%5d ", count);
24740 if (prefer_self && profile_equal(total, self))
24741 fprintf(fd, " ");
24742 else
24743 fprintf(fd, "%s ", profile_msg(total));
24744 if (!prefer_self && profile_equal(total, self))
24745 fprintf(fd, " ");
24746 else
24747 fprintf(fd, "%s ", profile_msg(self));
24748 }
24749 else
24750 fprintf(fd, " ");
24751}
24752
24753/*
24754 * Compare function for total time sorting.
24755 */
24756 static int
24757#ifdef __BORLANDC__
24758_RTLENTRYF
24759#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024760prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024761{
24762 ufunc_T *p1, *p2;
24763
24764 p1 = *(ufunc_T **)s1;
24765 p2 = *(ufunc_T **)s2;
24766 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24767}
24768
24769/*
24770 * Compare function for self time sorting.
24771 */
24772 static int
24773#ifdef __BORLANDC__
24774_RTLENTRYF
24775#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024776prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024777{
24778 ufunc_T *p1, *p2;
24779
24780 p1 = *(ufunc_T **)s1;
24781 p2 = *(ufunc_T **)s2;
24782 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24783}
24784
Bram Moolenaar05159a02005-02-26 23:04:13 +000024785#endif
24786
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024787/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024788 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024789 * Return TRUE if a package was loaded.
24790 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024792script_autoload(
24793 char_u *name,
24794 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024795{
24796 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024797 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024798 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024799 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024800
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024801 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024802 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024803 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024804 return FALSE;
24805
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024806 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024807
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024808 /* Find the name in the list of previously loaded package names. Skip
24809 * "autoload/", it's always the same. */
24810 for (i = 0; i < ga_loaded.ga_len; ++i)
24811 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24812 break;
24813 if (!reload && i < ga_loaded.ga_len)
24814 ret = FALSE; /* was loaded already */
24815 else
24816 {
24817 /* Remember the name if it wasn't loaded already. */
24818 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24819 {
24820 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24821 tofree = NULL;
24822 }
24823
24824 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024825 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024826 ret = TRUE;
24827 }
24828
24829 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 return ret;
24831}
24832
24833/*
24834 * Return the autoload script name for a function or variable name.
24835 * Returns NULL when out of memory.
24836 */
24837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024838autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024839{
24840 char_u *p;
24841 char_u *scriptname;
24842
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024843 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024844 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24845 if (scriptname == NULL)
24846 return FALSE;
24847 STRCPY(scriptname, "autoload/");
24848 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024849 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024850 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024851 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024852 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024853 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024854}
24855
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24857
24858/*
24859 * Function given to ExpandGeneric() to obtain the list of user defined
24860 * function names.
24861 */
24862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024863get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024865 static long_u done;
24866 static hashitem_T *hi;
24867 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868
24869 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024871 done = 0;
24872 hi = func_hashtab.ht_array;
24873 }
24874 if (done < func_hashtab.ht_used)
24875 {
24876 if (done++ > 0)
24877 ++hi;
24878 while (HASHITEM_EMPTY(hi))
24879 ++hi;
24880 fp = HI2UF(hi);
24881
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024882 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024883 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024884
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024885 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24886 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887
24888 cat_func_name(IObuff, fp);
24889 if (xp->xp_context != EXPAND_USER_FUNC)
24890 {
24891 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024892 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 STRCAT(IObuff, ")");
24894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 return IObuff;
24896 }
24897 return NULL;
24898}
24899
24900#endif /* FEAT_CMDL_COMPL */
24901
24902/*
24903 * Copy the function name of "fp" to buffer "buf".
24904 * "buf" must be able to hold the function name plus three bytes.
24905 * Takes care of script-local function names.
24906 */
24907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024908cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024910 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 {
24912 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024913 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 }
24915 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024916 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917}
24918
24919/*
24920 * ":delfunction {name}"
24921 */
24922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024923ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024925 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926 char_u *p;
24927 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024928 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929
24930 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024931 name = trans_function_name(&p, eap->skip, 0, &fudi);
24932 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024934 {
24935 if (fudi.fd_dict != NULL && !eap->skip)
24936 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 if (!ends_excmd(*skipwhite(p)))
24940 {
24941 vim_free(name);
24942 EMSG(_(e_trailing));
24943 return;
24944 }
24945 eap->nextcmd = check_nextcmd(p);
24946 if (eap->nextcmd != NULL)
24947 *p = NUL;
24948
24949 if (!eap->skip)
24950 fp = find_func(name);
24951 vim_free(name);
24952
24953 if (!eap->skip)
24954 {
24955 if (fp == NULL)
24956 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024957 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 return;
24959 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024960 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 {
24962 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24963 return;
24964 }
24965
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024966 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024968 /* Delete the dict item that refers to the function, it will
24969 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024970 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024972 else
24973 func_free(fp);
24974 }
24975}
24976
24977/*
24978 * Free a function and remove it from the list of functions.
24979 */
24980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024981func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024982{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024983 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024984
24985 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024986 ga_clear_strings(&(fp->uf_args));
24987 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024988#ifdef FEAT_PROFILE
24989 vim_free(fp->uf_tml_count);
24990 vim_free(fp->uf_tml_total);
24991 vim_free(fp->uf_tml_self);
24992#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024993
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024994 /* remove the function from the function hashtable */
24995 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24996 if (HASHITEM_EMPTY(hi))
24997 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024998 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024999 hash_remove(&func_hashtab, hi);
25000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025001 vim_free(fp);
25002}
25003
25004/*
25005 * Unreference a Function: decrement the reference count and free it when it
25006 * becomes zero. Only for numbered functions.
25007 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025009func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025010{
25011 ufunc_T *fp;
25012
25013 if (name != NULL && isdigit(*name))
25014 {
25015 fp = find_func(name);
25016 if (fp == NULL)
25017 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025018 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025019 {
25020 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025021 * when "uf_calls" becomes zero. */
25022 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025023 func_free(fp);
25024 }
25025 }
25026}
25027
25028/*
25029 * Count a reference to a Function.
25030 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025032func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025033{
25034 ufunc_T *fp;
25035
25036 if (name != NULL && isdigit(*name))
25037 {
25038 fp = find_func(name);
25039 if (fp == NULL)
25040 EMSG2(_(e_intern2), "func_ref()");
25041 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025042 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 }
25044}
25045
25046/*
25047 * Call a user function.
25048 */
25049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025050call_user_func(
25051 ufunc_T *fp, /* pointer to function */
25052 int argcount, /* nr of args */
25053 typval_T *argvars, /* arguments */
25054 typval_T *rettv, /* return value */
25055 linenr_T firstline, /* first line of range */
25056 linenr_T lastline, /* last line of range */
25057 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058{
Bram Moolenaar33570922005-01-25 22:26:29 +000025059 char_u *save_sourcing_name;
25060 linenr_T save_sourcing_lnum;
25061 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025062 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025063 int save_did_emsg;
25064 static int depth = 0;
25065 dictitem_T *v;
25066 int fixvar_idx = 0; /* index in fixvar[] */
25067 int i;
25068 int ai;
25069 char_u numbuf[NUMBUFLEN];
25070 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025071 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025072#ifdef FEAT_PROFILE
25073 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025074 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076
25077 /* If depth of calling is getting too high, don't execute the function */
25078 if (depth >= p_mfd)
25079 {
25080 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025081 rettv->v_type = VAR_NUMBER;
25082 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 return;
25084 }
25085 ++depth;
25086
25087 line_breakcheck(); /* check for CTRL-C hit */
25088
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025089 fc = (funccall_T *)alloc(sizeof(funccall_T));
25090 fc->caller = current_funccal;
25091 current_funccal = fc;
25092 fc->func = fp;
25093 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025094 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025095 fc->linenr = 0;
25096 fc->returned = FALSE;
25097 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025099 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25100 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
Bram Moolenaar33570922005-01-25 22:26:29 +000025102 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025103 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025104 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25105 * each argument variable and saves a lot of time.
25106 */
25107 /*
25108 * Init l: variables.
25109 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025110 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025111 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025112 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025113 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25114 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025115 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025116 name = v->di_key;
25117 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025118 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025119 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025120 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025121 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025122 v->di_tv.vval.v_dict = selfdict;
25123 ++selfdict->dv_refcount;
25124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025125
Bram Moolenaar33570922005-01-25 22:26:29 +000025126 /*
25127 * Init a: variables.
25128 * Set a:0 to "argcount".
25129 * Set a:000 to a list with room for the "..." arguments.
25130 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025131 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025132 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025133 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025134 /* Use "name" to avoid a warning from some compiler that checks the
25135 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025136 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025137 name = v->di_key;
25138 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025139 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025140 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025141 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025142 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025143 v->di_tv.vval.v_list = &fc->l_varlist;
25144 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25145 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25146 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025147
25148 /*
25149 * Set a:firstline to "firstline" and a:lastline to "lastline".
25150 * Set a:name to named arguments.
25151 * Set a:N to the "..." arguments.
25152 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025153 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025154 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025155 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025156 (varnumber_T)lastline);
25157 for (i = 0; i < argcount; ++i)
25158 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025159 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025160 if (ai < 0)
25161 /* named argument a:name */
25162 name = FUNCARG(fp, i);
25163 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025165 /* "..." argument a:1, a:2, etc. */
25166 sprintf((char *)numbuf, "%d", ai + 1);
25167 name = numbuf;
25168 }
25169 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25170 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025171 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25173 }
25174 else
25175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025176 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25177 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025178 if (v == NULL)
25179 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025180 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025181 }
25182 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025183 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025184
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025185 /* Note: the values are copied directly to avoid alloc/free.
25186 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025187 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025188 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025189
25190 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25191 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025192 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25193 fc->l_listitems[ai].li_tv = argvars[i];
25194 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025195 }
25196 }
25197
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 /* Don't redraw while executing the function. */
25199 ++RedrawingDisabled;
25200 save_sourcing_name = sourcing_name;
25201 save_sourcing_lnum = sourcing_lnum;
25202 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025203 /* need space for function name + ("function " + 3) or "[number]" */
25204 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25205 + STRLEN(fp->uf_name) + 20;
25206 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 if (sourcing_name != NULL)
25208 {
25209 if (save_sourcing_name != NULL
25210 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025211 sprintf((char *)sourcing_name, "%s[%d]..",
25212 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 else
25214 STRCPY(sourcing_name, "function ");
25215 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25216
25217 if (p_verbose >= 12)
25218 {
25219 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025220 verbose_enter_scroll();
25221
Bram Moolenaar555b2802005-05-19 21:08:39 +000025222 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 if (p_verbose >= 14)
25224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025226 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025227 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025228 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229
25230 msg_puts((char_u *)"(");
25231 for (i = 0; i < argcount; ++i)
25232 {
25233 if (i > 0)
25234 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025235 if (argvars[i].v_type == VAR_NUMBER)
25236 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 else
25238 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025239 /* Do not want errors such as E724 here. */
25240 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025241 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025242 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025243 if (s != NULL)
25244 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025245 if (vim_strsize(s) > MSG_BUF_CLEN)
25246 {
25247 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25248 s = buf;
25249 }
25250 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025251 vim_free(tofree);
25252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 }
25254 }
25255 msg_puts((char_u *)")");
25256 }
25257 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025258
25259 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 --no_wait_return;
25261 }
25262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025263#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025264 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025265 {
25266 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25267 func_do_profile(fp);
25268 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025269 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025270 {
25271 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025272 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025273 profile_zero(&fp->uf_tm_children);
25274 }
25275 script_prof_save(&wait_start);
25276 }
25277#endif
25278
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025280 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 save_did_emsg = did_emsg;
25282 did_emsg = FALSE;
25283
25284 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025285 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25287
25288 --RedrawingDisabled;
25289
25290 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025291 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025293 clear_tv(rettv);
25294 rettv->v_type = VAR_NUMBER;
25295 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 }
25297
Bram Moolenaar05159a02005-02-26 23:04:13 +000025298#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025299 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025300 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025301 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025302 profile_end(&call_start);
25303 profile_sub_wait(&wait_start, &call_start);
25304 profile_add(&fp->uf_tm_total, &call_start);
25305 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025306 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025307 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025308 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25309 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025310 }
25311 }
25312#endif
25313
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 /* when being verbose, mention the return value */
25315 if (p_verbose >= 12)
25316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025318 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025321 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025322 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025323 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025324 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025327 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025328 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025329 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025330 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025331
Bram Moolenaar555b2802005-05-19 21:08:39 +000025332 /* The value may be very long. Skip the middle part, so that we
25333 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025334 * truncate it at the end. Don't want errors such as E724 here. */
25335 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025336 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025337 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025338 if (s != NULL)
25339 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025340 if (vim_strsize(s) > MSG_BUF_CLEN)
25341 {
25342 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25343 s = buf;
25344 }
25345 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025346 vim_free(tofree);
25347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 }
25349 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025350
25351 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 --no_wait_return;
25353 }
25354
25355 vim_free(sourcing_name);
25356 sourcing_name = save_sourcing_name;
25357 sourcing_lnum = save_sourcing_lnum;
25358 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025359#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025360 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025361 script_prof_restore(&wait_start);
25362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363
25364 if (p_verbose >= 12 && sourcing_name != NULL)
25365 {
25366 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025367 verbose_enter_scroll();
25368
Bram Moolenaar555b2802005-05-19 21:08:39 +000025369 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025371
25372 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 --no_wait_return;
25374 }
25375
25376 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025377 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025379
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025380 /* If the a:000 list and the l: and a: dicts are not referenced we can
25381 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025382 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25383 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25384 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25385 {
25386 free_funccal(fc, FALSE);
25387 }
25388 else
25389 {
25390 hashitem_T *hi;
25391 listitem_T *li;
25392 int todo;
25393
25394 /* "fc" is still in use. This can happen when returning "a:000" or
25395 * assigning "l:" to a global variable.
25396 * Link "fc" in the list for garbage collection later. */
25397 fc->caller = previous_funccal;
25398 previous_funccal = fc;
25399
25400 /* Make a copy of the a: variables, since we didn't do that above. */
25401 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25402 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25403 {
25404 if (!HASHITEM_EMPTY(hi))
25405 {
25406 --todo;
25407 v = HI2DI(hi);
25408 copy_tv(&v->di_tv, &v->di_tv);
25409 }
25410 }
25411
25412 /* Make a copy of the a:000 items, since we didn't do that above. */
25413 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25414 copy_tv(&li->li_tv, &li->li_tv);
25415 }
25416}
25417
25418/*
25419 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025420 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025421 */
25422 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025423can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025424{
25425 return (fc->l_varlist.lv_copyID != copyID
25426 && fc->l_vars.dv_copyID != copyID
25427 && fc->l_avars.dv_copyID != copyID);
25428}
25429
25430/*
25431 * Free "fc" and what it contains.
25432 */
25433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025434free_funccal(
25435 funccall_T *fc,
25436 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025437{
25438 listitem_T *li;
25439
25440 /* The a: variables typevals may not have been allocated, only free the
25441 * allocated variables. */
25442 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25443
25444 /* free all l: variables */
25445 vars_clear(&fc->l_vars.dv_hashtab);
25446
25447 /* Free the a:000 variables if they were allocated. */
25448 if (free_val)
25449 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25450 clear_tv(&li->li_tv);
25451
25452 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453}
25454
25455/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 * Add a number variable "name" to dict "dp" with value "nr".
25457 */
25458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025459add_nr_var(
25460 dict_T *dp,
25461 dictitem_T *v,
25462 char *name,
25463 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025464{
25465 STRCPY(v->di_key, name);
25466 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25467 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25468 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025469 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025470 v->di_tv.vval.v_number = nr;
25471}
25472
25473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 * ":return [expr]"
25475 */
25476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025477ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478{
25479 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 int returning = FALSE;
25482
25483 if (current_funccal == NULL)
25484 {
25485 EMSG(_("E133: :return not inside a function"));
25486 return;
25487 }
25488
25489 if (eap->skip)
25490 ++emsg_skip;
25491
25492 eap->nextcmd = NULL;
25493 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025494 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 {
25496 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025497 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025499 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 }
25501 /* It's safer to return also on error. */
25502 else if (!eap->skip)
25503 {
25504 /*
25505 * Return unless the expression evaluation has been cancelled due to an
25506 * aborting error, an interrupt, or an exception.
25507 */
25508 if (!aborting())
25509 returning = do_return(eap, FALSE, TRUE, NULL);
25510 }
25511
25512 /* When skipping or the return gets pending, advance to the next command
25513 * in this line (!returning). Otherwise, ignore the rest of the line.
25514 * Following lines will be ignored by get_func_line(). */
25515 if (returning)
25516 eap->nextcmd = NULL;
25517 else if (eap->nextcmd == NULL) /* no argument */
25518 eap->nextcmd = check_nextcmd(arg);
25519
25520 if (eap->skip)
25521 --emsg_skip;
25522}
25523
25524/*
25525 * Return from a function. Possibly makes the return pending. Also called
25526 * for a pending return at the ":endtry" or after returning from an extra
25527 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025528 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025529 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 * FALSE when the return gets pending.
25531 */
25532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025533do_return(
25534 exarg_T *eap,
25535 int reanimate,
25536 int is_cmd,
25537 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538{
25539 int idx;
25540 struct condstack *cstack = eap->cstack;
25541
25542 if (reanimate)
25543 /* Undo the return. */
25544 current_funccal->returned = FALSE;
25545
25546 /*
25547 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25548 * not in its finally clause (which then is to be executed next) is found.
25549 * In this case, make the ":return" pending for execution at the ":endtry".
25550 * Otherwise, return normally.
25551 */
25552 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25553 if (idx >= 0)
25554 {
25555 cstack->cs_pending[idx] = CSTP_RETURN;
25556
25557 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025558 /* A pending return again gets pending. "rettv" points to an
25559 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025561 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 else
25563 {
25564 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025565 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025567 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025569 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 {
25571 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025572 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025573 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 else
25575 EMSG(_(e_outofmem));
25576 }
25577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025578 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025579
25580 if (reanimate)
25581 {
25582 /* The pending return value could be overwritten by a ":return"
25583 * without argument in a finally clause; reset the default
25584 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025585 current_funccal->rettv->v_type = VAR_NUMBER;
25586 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 }
25588 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025589 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 }
25591 else
25592 {
25593 current_funccal->returned = TRUE;
25594
25595 /* If the return is carried out now, store the return value. For
25596 * a return immediately after reanimation, the value is already
25597 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025598 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025600 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025601 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025603 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 }
25605 }
25606
25607 return idx < 0;
25608}
25609
25610/*
25611 * Free the variable with a pending return value.
25612 */
25613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025614discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615{
Bram Moolenaar33570922005-01-25 22:26:29 +000025616 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617}
25618
25619/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025620 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 * is an allocated string. Used by report_pending() for verbose messages.
25622 */
25623 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025624get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025626 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025627 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025628 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025630 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025631 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025632 if (s == NULL)
25633 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025634
25635 STRCPY(IObuff, ":return ");
25636 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25637 if (STRLEN(s) + 8 >= IOSIZE)
25638 STRCPY(IObuff + IOSIZE - 4, "...");
25639 vim_free(tofree);
25640 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641}
25642
25643/*
25644 * Get next function line.
25645 * Called by do_cmdline() to get the next line.
25646 * Returns allocated string, or NULL for end of function.
25647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025649get_func_line(
25650 int c UNUSED,
25651 void *cookie,
25652 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653{
Bram Moolenaar33570922005-01-25 22:26:29 +000025654 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025655 ufunc_T *fp = fcp->func;
25656 char_u *retval;
25657 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658
25659 /* If breakpoints have been added/deleted need to check for it. */
25660 if (fcp->dbg_tick != debug_tick)
25661 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025662 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 sourcing_lnum);
25664 fcp->dbg_tick = debug_tick;
25665 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025666#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025667 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025668 func_line_end(cookie);
25669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670
Bram Moolenaar05159a02005-02-26 23:04:13 +000025671 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025672 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25673 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 retval = NULL;
25675 else
25676 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025677 /* Skip NULL lines (continuation lines). */
25678 while (fcp->linenr < gap->ga_len
25679 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25680 ++fcp->linenr;
25681 if (fcp->linenr >= gap->ga_len)
25682 retval = NULL;
25683 else
25684 {
25685 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25686 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025687#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025688 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025689 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025690#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 }
25693
25694 /* Did we encounter a breakpoint? */
25695 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25696 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025697 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 sourcing_lnum);
25701 fcp->dbg_tick = debug_tick;
25702 }
25703
25704 return retval;
25705}
25706
Bram Moolenaar05159a02005-02-26 23:04:13 +000025707#if defined(FEAT_PROFILE) || defined(PROTO)
25708/*
25709 * Called when starting to read a function line.
25710 * "sourcing_lnum" must be correct!
25711 * When skipping lines it may not actually be executed, but we won't find out
25712 * until later and we need to store the time now.
25713 */
25714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025715func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025716{
25717 funccall_T *fcp = (funccall_T *)cookie;
25718 ufunc_T *fp = fcp->func;
25719
25720 if (fp->uf_profiling && sourcing_lnum >= 1
25721 && sourcing_lnum <= fp->uf_lines.ga_len)
25722 {
25723 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025724 /* Skip continuation lines. */
25725 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25726 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025727 fp->uf_tml_execed = FALSE;
25728 profile_start(&fp->uf_tml_start);
25729 profile_zero(&fp->uf_tml_children);
25730 profile_get_wait(&fp->uf_tml_wait);
25731 }
25732}
25733
25734/*
25735 * Called when actually executing a function line.
25736 */
25737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025738func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025739{
25740 funccall_T *fcp = (funccall_T *)cookie;
25741 ufunc_T *fp = fcp->func;
25742
25743 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25744 fp->uf_tml_execed = TRUE;
25745}
25746
25747/*
25748 * Called when done with a function line.
25749 */
25750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025751func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025752{
25753 funccall_T *fcp = (funccall_T *)cookie;
25754 ufunc_T *fp = fcp->func;
25755
25756 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25757 {
25758 if (fp->uf_tml_execed)
25759 {
25760 ++fp->uf_tml_count[fp->uf_tml_idx];
25761 profile_end(&fp->uf_tml_start);
25762 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025763 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025764 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25765 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025766 }
25767 fp->uf_tml_idx = -1;
25768 }
25769}
25770#endif
25771
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772/*
25773 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025774 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 */
25776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025777func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778{
Bram Moolenaar33570922005-01-25 22:26:29 +000025779 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780
25781 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25782 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025783 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 || fcp->returned);
25785}
25786
25787/*
25788 * return TRUE if cookie indicates a function which "abort"s on errors.
25789 */
25790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025791func_has_abort(
25792 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025794 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795}
25796
25797#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25798typedef enum
25799{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025800 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25801 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25802 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025803} var_flavour_T;
25804
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025805static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806
25807 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809{
25810 char_u *p = varname;
25811
25812 if (ASCII_ISUPPER(*p))
25813 {
25814 while (*(++p))
25815 if (ASCII_ISLOWER(*p))
25816 return VAR_FLAVOUR_SESSION;
25817 return VAR_FLAVOUR_VIMINFO;
25818 }
25819 else
25820 return VAR_FLAVOUR_DEFAULT;
25821}
25822#endif
25823
25824#if defined(FEAT_VIMINFO) || defined(PROTO)
25825/*
25826 * Restore global vars that start with a capital from the viminfo file
25827 */
25828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025829read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830{
25831 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025832 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025833 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025834 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835
25836 if (!writing && (find_viminfo_parameter('!') != NULL))
25837 {
25838 tab = vim_strchr(virp->vir_line + 1, '\t');
25839 if (tab != NULL)
25840 {
25841 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025842 switch (*tab)
25843 {
25844 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025845#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025846 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025847#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025848 case 'D': type = VAR_DICT; break;
25849 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025850 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852
25853 tab = vim_strchr(tab, '\t');
25854 if (tab != NULL)
25855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025856 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025857 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025858 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025860#ifdef FEAT_FLOAT
25861 else if (type == VAR_FLOAT)
25862 (void)string2float(tab + 1, &tv.vval.v_float);
25863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025865 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025866 if (type == VAR_DICT || type == VAR_LIST)
25867 {
25868 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25869
25870 if (etv == NULL)
25871 /* Failed to parse back the dict or list, use it as a
25872 * string. */
25873 tv.v_type = VAR_STRING;
25874 else
25875 {
25876 vim_free(tv.vval.v_string);
25877 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025878 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025879 }
25880 }
25881
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025882 /* when in a function use global variables */
25883 save_funccal = current_funccal;
25884 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025885 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025886 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025887
25888 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025889 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025890 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25891 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 }
25893 }
25894 }
25895
25896 return viminfo_readline(virp);
25897}
25898
25899/*
25900 * Write global vars that start with a capital to the viminfo file
25901 */
25902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025903write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904{
Bram Moolenaar33570922005-01-25 22:26:29 +000025905 hashitem_T *hi;
25906 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025907 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025908 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025909 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025910 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025911 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912
25913 if (find_viminfo_parameter('!') == NULL)
25914 return;
25915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025916 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025918 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025919 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025921 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025923 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025924 this_var = HI2DI(hi);
25925 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025926 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025927 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025928 {
25929 case VAR_STRING: s = "STR"; break;
25930 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025931 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025932 case VAR_DICT: s = "DIC"; break;
25933 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025934 case VAR_SPECIAL: s = "XPL"; break;
25935
25936 case VAR_UNKNOWN:
25937 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025938 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025939 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025940 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025941 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025942 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025943 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025944 if (p != NULL)
25945 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025946 vim_free(tofree);
25947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 }
25949 }
25950}
25951#endif
25952
25953#if defined(FEAT_SESSION) || defined(PROTO)
25954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025955store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956{
Bram Moolenaar33570922005-01-25 22:26:29 +000025957 hashitem_T *hi;
25958 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025959 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 char_u *p, *t;
25961
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025962 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025963 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025965 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025967 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025968 this_var = HI2DI(hi);
25969 if ((this_var->di_tv.v_type == VAR_NUMBER
25970 || this_var->di_tv.v_type == VAR_STRING)
25971 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025972 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025973 /* Escape special characters with a backslash. Turn a LF and
25974 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025975 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025976 (char_u *)"\\\"\n\r");
25977 if (p == NULL) /* out of memory */
25978 break;
25979 for (t = p; *t != NUL; ++t)
25980 if (*t == '\n')
25981 *t = 'n';
25982 else if (*t == '\r')
25983 *t = 'r';
25984 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025985 this_var->di_key,
25986 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25987 : ' ',
25988 p,
25989 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25990 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025991 || put_eol(fd) == FAIL)
25992 {
25993 vim_free(p);
25994 return FAIL;
25995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 vim_free(p);
25997 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025998#ifdef FEAT_FLOAT
25999 else if (this_var->di_tv.v_type == VAR_FLOAT
26000 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26001 {
26002 float_T f = this_var->di_tv.vval.v_float;
26003 int sign = ' ';
26004
26005 if (f < 0)
26006 {
26007 f = -f;
26008 sign = '-';
26009 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026010 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026011 this_var->di_key, sign, f) < 0)
26012 || put_eol(fd) == FAIL)
26013 return FAIL;
26014 }
26015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 }
26017 }
26018 return OK;
26019}
26020#endif
26021
Bram Moolenaar661b1822005-07-28 22:36:45 +000026022/*
26023 * Display script name where an item was last set.
26024 * Should only be invoked when 'verbose' is non-zero.
26025 */
26026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026027last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026028{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026029 char_u *p;
26030
Bram Moolenaar661b1822005-07-28 22:36:45 +000026031 if (scriptID != 0)
26032 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026033 p = home_replace_save(NULL, get_scriptname(scriptID));
26034 if (p != NULL)
26035 {
26036 verbose_enter();
26037 MSG_PUTS(_("\n\tLast set from "));
26038 MSG_PUTS(p);
26039 vim_free(p);
26040 verbose_leave();
26041 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026042 }
26043}
26044
Bram Moolenaard812df62008-11-09 12:46:09 +000026045/*
26046 * List v:oldfiles in a nice way.
26047 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026049ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026050{
26051 list_T *l = vimvars[VV_OLDFILES].vv_list;
26052 listitem_T *li;
26053 int nr = 0;
26054
26055 if (l == NULL)
26056 msg((char_u *)_("No old files"));
26057 else
26058 {
26059 msg_start();
26060 msg_scroll = TRUE;
26061 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26062 {
26063 msg_outnum((long)++nr);
26064 MSG_PUTS(": ");
26065 msg_outtrans(get_tv_string(&li->li_tv));
26066 msg_putchar('\n');
26067 out_flush(); /* output one line at a time */
26068 ui_breakcheck();
26069 }
26070 /* Assume "got_int" was set to truncate the listing. */
26071 got_int = FALSE;
26072
26073#ifdef FEAT_BROWSE_CMD
26074 if (cmdmod.browse)
26075 {
26076 quit_more = FALSE;
26077 nr = prompt_for_number(FALSE);
26078 msg_starthere();
26079 if (nr > 0)
26080 {
26081 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26082 (long)nr);
26083
26084 if (p != NULL)
26085 {
26086 p = expand_env_save(p);
26087 eap->arg = p;
26088 eap->cmdidx = CMD_edit;
26089 cmdmod.browse = FALSE;
26090 do_exedit(eap, NULL);
26091 vim_free(p);
26092 }
26093 }
26094 }
26095#endif
26096 }
26097}
26098
Bram Moolenaar53744302015-07-17 17:38:22 +020026099/* reset v:option_new, v:option_old and v:option_type */
26100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026101reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026102{
26103 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26104 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26105 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26106}
26107
26108
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109#endif /* FEAT_EVAL */
26110
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026112#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026113
26114#ifdef WIN3264
26115/*
26116 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26117 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026118static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26119static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26120static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121
26122/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026123 * Get the short path (8.3) for the filename in "fnamep".
26124 * Only works for a valid file name.
26125 * When the path gets longer "fnamep" is changed and the allocated buffer
26126 * is put in "bufp".
26127 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26128 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129 */
26130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026131get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026133 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 char_u *newbuf;
26135
26136 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026137 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138 if (l > len - 1)
26139 {
26140 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026141 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 newbuf = vim_strnsave(*fnamep, l);
26143 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026144 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145
26146 vim_free(*bufp);
26147 *fnamep = *bufp = newbuf;
26148
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026149 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026150 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 }
26152
26153 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026154 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155}
26156
26157/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026158 * Get the short path (8.3) for the filename in "fname". The converted
26159 * path is returned in "bufp".
26160 *
26161 * Some of the directories specified in "fname" may not exist. This function
26162 * will shorten the existing directories at the beginning of the path and then
26163 * append the remaining non-existing path.
26164 *
26165 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026166 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026167 * bufp - Pointer to an allocated buffer for the filename.
26168 * fnamelen - Length of the filename pointed to by fname
26169 *
26170 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 */
26172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026173shortpath_for_invalid_fname(
26174 char_u **fname,
26175 char_u **bufp,
26176 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026177{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026178 char_u *short_fname, *save_fname, *pbuf_unused;
26179 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026181 int old_len, len;
26182 int new_len, sfx_len;
26183 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184
26185 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026186 old_len = *fnamelen;
26187 save_fname = vim_strnsave(*fname, old_len);
26188 pbuf_unused = NULL;
26189 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026191 endp = save_fname + old_len - 1; /* Find the end of the copy */
26192 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026194 /*
26195 * Try shortening the supplied path till it succeeds by removing one
26196 * directory at a time from the tail of the path.
26197 */
26198 len = 0;
26199 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026201 /* go back one path-separator */
26202 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26203 --endp;
26204 if (endp <= save_fname)
26205 break; /* processed the complete path */
26206
26207 /*
26208 * Replace the path separator with a NUL and try to shorten the
26209 * resulting path.
26210 */
26211 ch = *endp;
26212 *endp = 0;
26213 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026214 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026215 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26216 {
26217 retval = FAIL;
26218 goto theend;
26219 }
26220 *endp = ch; /* preserve the string */
26221
26222 if (len > 0)
26223 break; /* successfully shortened the path */
26224
26225 /* failed to shorten the path. Skip the path separator */
26226 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 }
26228
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026229 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026231 /*
26232 * Succeeded in shortening the path. Now concatenate the shortened
26233 * path with the remaining path at the tail.
26234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026236 /* Compute the length of the new path. */
26237 sfx_len = (int)(save_endp - endp) + 1;
26238 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026239
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026240 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026242 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026244 /* There is not enough space in the currently allocated string,
26245 * copy it to a buffer big enough. */
26246 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026248 {
26249 retval = FAIL;
26250 goto theend;
26251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252 }
26253 else
26254 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026255 /* Transfer short_fname to the main buffer (it's big enough),
26256 * unless get_short_pathname() did its work in-place. */
26257 *fname = *bufp = save_fname;
26258 if (short_fname != save_fname)
26259 vim_strncpy(save_fname, short_fname, len);
26260 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026262
26263 /* concat the not-shortened part of the path */
26264 vim_strncpy(*fname + len, endp, sfx_len);
26265 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026266 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026267
26268theend:
26269 vim_free(pbuf_unused);
26270 vim_free(save_fname);
26271
26272 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026273}
26274
26275/*
26276 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026277 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026278 */
26279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026280shortpath_for_partial(
26281 char_u **fnamep,
26282 char_u **bufp,
26283 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284{
26285 int sepcount, len, tflen;
26286 char_u *p;
26287 char_u *pbuf, *tfname;
26288 int hasTilde;
26289
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026290 /* Count up the path separators from the RHS.. so we know which part
26291 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026292 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026293 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294 if (vim_ispathsep(*p))
26295 ++sepcount;
26296
26297 /* Need full path first (use expand_env() to remove a "~/") */
26298 hasTilde = (**fnamep == '~');
26299 if (hasTilde)
26300 pbuf = tfname = expand_env_save(*fnamep);
26301 else
26302 pbuf = tfname = FullName_save(*fnamep, FALSE);
26303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026304 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026306 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26307 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308
26309 if (len == 0)
26310 {
26311 /* Don't have a valid filename, so shorten the rest of the
26312 * path if we can. This CAN give us invalid 8.3 filenames, but
26313 * there's not a lot of point in guessing what it might be.
26314 */
26315 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026316 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318 }
26319
26320 /* Count the paths backward to find the beginning of the desired string. */
26321 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026322 {
26323#ifdef FEAT_MBYTE
26324 if (has_mbyte)
26325 p -= mb_head_off(tfname, p);
26326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026327 if (vim_ispathsep(*p))
26328 {
26329 if (sepcount == 0 || (hasTilde && sepcount == 1))
26330 break;
26331 else
26332 sepcount --;
26333 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 if (hasTilde)
26336 {
26337 --p;
26338 if (p >= tfname)
26339 *p = '~';
26340 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026342 }
26343 else
26344 ++p;
26345
26346 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26347 vim_free(*bufp);
26348 *fnamelen = (int)STRLEN(p);
26349 *bufp = pbuf;
26350 *fnamep = p;
26351
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026352 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353}
26354#endif /* WIN3264 */
26355
26356/*
26357 * Adjust a filename, according to a string of modifiers.
26358 * *fnamep must be NUL terminated when called. When returning, the length is
26359 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026360 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361 * When there is an error, *fnamep is set to NULL.
26362 */
26363 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026364modify_fname(
26365 char_u *src, /* string with modifiers */
26366 int *usedlen, /* characters after src that are used */
26367 char_u **fnamep, /* file name so far */
26368 char_u **bufp, /* buffer for allocated file name or NULL */
26369 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026370{
26371 int valid = 0;
26372 char_u *tail;
26373 char_u *s, *p, *pbuf;
26374 char_u dirname[MAXPATHL];
26375 int c;
26376 int has_fullname = 0;
26377#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026378 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379 int has_shortname = 0;
26380#endif
26381
26382repeat:
26383 /* ":p" - full path/file_name */
26384 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26385 {
26386 has_fullname = 1;
26387
26388 valid |= VALID_PATH;
26389 *usedlen += 2;
26390
26391 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26392 if ((*fnamep)[0] == '~'
26393#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26394 && ((*fnamep)[1] == '/'
26395# ifdef BACKSLASH_IN_FILENAME
26396 || (*fnamep)[1] == '\\'
26397# endif
26398 || (*fnamep)[1] == NUL)
26399
26400#endif
26401 )
26402 {
26403 *fnamep = expand_env_save(*fnamep);
26404 vim_free(*bufp); /* free any allocated file name */
26405 *bufp = *fnamep;
26406 if (*fnamep == NULL)
26407 return -1;
26408 }
26409
26410 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026411 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 {
26413 if (vim_ispathsep(*p)
26414 && p[1] == '.'
26415 && (p[2] == NUL
26416 || vim_ispathsep(p[2])
26417 || (p[2] == '.'
26418 && (p[3] == NUL || vim_ispathsep(p[3])))))
26419 break;
26420 }
26421
26422 /* FullName_save() is slow, don't use it when not needed. */
26423 if (*p != NUL || !vim_isAbsName(*fnamep))
26424 {
26425 *fnamep = FullName_save(*fnamep, *p != NUL);
26426 vim_free(*bufp); /* free any allocated file name */
26427 *bufp = *fnamep;
26428 if (*fnamep == NULL)
26429 return -1;
26430 }
26431
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026432#ifdef WIN3264
26433# if _WIN32_WINNT >= 0x0500
26434 if (vim_strchr(*fnamep, '~') != NULL)
26435 {
26436 /* Expand 8.3 filename to full path. Needed to make sure the same
26437 * file does not have two different names.
26438 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26439 p = alloc(_MAX_PATH + 1);
26440 if (p != NULL)
26441 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026442 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026443 {
26444 vim_free(*bufp);
26445 *bufp = *fnamep = p;
26446 }
26447 else
26448 vim_free(p);
26449 }
26450 }
26451# endif
26452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026453 /* Append a path separator to a directory. */
26454 if (mch_isdir(*fnamep))
26455 {
26456 /* Make room for one or two extra characters. */
26457 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26458 vim_free(*bufp); /* free any allocated file name */
26459 *bufp = *fnamep;
26460 if (*fnamep == NULL)
26461 return -1;
26462 add_pathsep(*fnamep);
26463 }
26464 }
26465
26466 /* ":." - path relative to the current directory */
26467 /* ":~" - path relative to the home directory */
26468 /* ":8" - shortname path - postponed till after */
26469 while (src[*usedlen] == ':'
26470 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26471 {
26472 *usedlen += 2;
26473 if (c == '8')
26474 {
26475#ifdef WIN3264
26476 has_shortname = 1; /* Postpone this. */
26477#endif
26478 continue;
26479 }
26480 pbuf = NULL;
26481 /* Need full path first (use expand_env() to remove a "~/") */
26482 if (!has_fullname)
26483 {
26484 if (c == '.' && **fnamep == '~')
26485 p = pbuf = expand_env_save(*fnamep);
26486 else
26487 p = pbuf = FullName_save(*fnamep, FALSE);
26488 }
26489 else
26490 p = *fnamep;
26491
26492 has_fullname = 0;
26493
26494 if (p != NULL)
26495 {
26496 if (c == '.')
26497 {
26498 mch_dirname(dirname, MAXPATHL);
26499 s = shorten_fname(p, dirname);
26500 if (s != NULL)
26501 {
26502 *fnamep = s;
26503 if (pbuf != NULL)
26504 {
26505 vim_free(*bufp); /* free any allocated file name */
26506 *bufp = pbuf;
26507 pbuf = NULL;
26508 }
26509 }
26510 }
26511 else
26512 {
26513 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26514 /* Only replace it when it starts with '~' */
26515 if (*dirname == '~')
26516 {
26517 s = vim_strsave(dirname);
26518 if (s != NULL)
26519 {
26520 *fnamep = s;
26521 vim_free(*bufp);
26522 *bufp = s;
26523 }
26524 }
26525 }
26526 vim_free(pbuf);
26527 }
26528 }
26529
26530 tail = gettail(*fnamep);
26531 *fnamelen = (int)STRLEN(*fnamep);
26532
26533 /* ":h" - head, remove "/file_name", can be repeated */
26534 /* Don't remove the first "/" or "c:\" */
26535 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26536 {
26537 valid |= VALID_HEAD;
26538 *usedlen += 2;
26539 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026540 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026541 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026542 *fnamelen = (int)(tail - *fnamep);
26543#ifdef VMS
26544 if (*fnamelen > 0)
26545 *fnamelen += 1; /* the path separator is part of the path */
26546#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026547 if (*fnamelen == 0)
26548 {
26549 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26550 p = vim_strsave((char_u *)".");
26551 if (p == NULL)
26552 return -1;
26553 vim_free(*bufp);
26554 *bufp = *fnamep = tail = p;
26555 *fnamelen = 1;
26556 }
26557 else
26558 {
26559 while (tail > s && !after_pathsep(s, tail))
26560 mb_ptr_back(*fnamep, tail);
26561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 }
26563
26564 /* ":8" - shortname */
26565 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26566 {
26567 *usedlen += 2;
26568#ifdef WIN3264
26569 has_shortname = 1;
26570#endif
26571 }
26572
26573#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026574 /*
26575 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026576 */
26577 if (has_shortname)
26578 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026579 /* Copy the string if it is shortened by :h and when it wasn't copied
26580 * yet, because we are going to change it in place. Avoids changing
26581 * the buffer name for "%:8". */
26582 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583 {
26584 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026585 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 return -1;
26587 vim_free(*bufp);
26588 *bufp = *fnamep = p;
26589 }
26590
26591 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026592 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593 if (!has_fullname && !vim_isAbsName(*fnamep))
26594 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026595 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026596 return -1;
26597 }
26598 else
26599 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026600 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601
Bram Moolenaardc935552011-08-17 15:23:23 +020026602 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026603 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026604 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605 return -1;
26606
26607 if (l == 0)
26608 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026609 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026610 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026611 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026612 return -1;
26613 }
26614 *fnamelen = l;
26615 }
26616 }
26617#endif /* WIN3264 */
26618
26619 /* ":t" - tail, just the basename */
26620 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26621 {
26622 *usedlen += 2;
26623 *fnamelen -= (int)(tail - *fnamep);
26624 *fnamep = tail;
26625 }
26626
26627 /* ":e" - extension, can be repeated */
26628 /* ":r" - root, without extension, can be repeated */
26629 while (src[*usedlen] == ':'
26630 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26631 {
26632 /* find a '.' in the tail:
26633 * - for second :e: before the current fname
26634 * - otherwise: The last '.'
26635 */
26636 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26637 s = *fnamep - 2;
26638 else
26639 s = *fnamep + *fnamelen - 1;
26640 for ( ; s > tail; --s)
26641 if (s[0] == '.')
26642 break;
26643 if (src[*usedlen + 1] == 'e') /* :e */
26644 {
26645 if (s > tail)
26646 {
26647 *fnamelen += (int)(*fnamep - (s + 1));
26648 *fnamep = s + 1;
26649#ifdef VMS
26650 /* cut version from the extension */
26651 s = *fnamep + *fnamelen - 1;
26652 for ( ; s > *fnamep; --s)
26653 if (s[0] == ';')
26654 break;
26655 if (s > *fnamep)
26656 *fnamelen = s - *fnamep;
26657#endif
26658 }
26659 else if (*fnamep <= tail)
26660 *fnamelen = 0;
26661 }
26662 else /* :r */
26663 {
26664 if (s > tail) /* remove one extension */
26665 *fnamelen = (int)(s - *fnamep);
26666 }
26667 *usedlen += 2;
26668 }
26669
26670 /* ":s?pat?foo?" - substitute */
26671 /* ":gs?pat?foo?" - global substitute */
26672 if (src[*usedlen] == ':'
26673 && (src[*usedlen + 1] == 's'
26674 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26675 {
26676 char_u *str;
26677 char_u *pat;
26678 char_u *sub;
26679 int sep;
26680 char_u *flags;
26681 int didit = FALSE;
26682
26683 flags = (char_u *)"";
26684 s = src + *usedlen + 2;
26685 if (src[*usedlen + 1] == 'g')
26686 {
26687 flags = (char_u *)"g";
26688 ++s;
26689 }
26690
26691 sep = *s++;
26692 if (sep)
26693 {
26694 /* find end of pattern */
26695 p = vim_strchr(s, sep);
26696 if (p != NULL)
26697 {
26698 pat = vim_strnsave(s, (int)(p - s));
26699 if (pat != NULL)
26700 {
26701 s = p + 1;
26702 /* find end of substitution */
26703 p = vim_strchr(s, sep);
26704 if (p != NULL)
26705 {
26706 sub = vim_strnsave(s, (int)(p - s));
26707 str = vim_strnsave(*fnamep, *fnamelen);
26708 if (sub != NULL && str != NULL)
26709 {
26710 *usedlen = (int)(p + 1 - src);
26711 s = do_string_sub(str, pat, sub, flags);
26712 if (s != NULL)
26713 {
26714 *fnamep = s;
26715 *fnamelen = (int)STRLEN(s);
26716 vim_free(*bufp);
26717 *bufp = s;
26718 didit = TRUE;
26719 }
26720 }
26721 vim_free(sub);
26722 vim_free(str);
26723 }
26724 vim_free(pat);
26725 }
26726 }
26727 /* after using ":s", repeat all the modifiers */
26728 if (didit)
26729 goto repeat;
26730 }
26731 }
26732
Bram Moolenaar26df0922014-02-23 23:39:13 +010026733 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26734 {
26735 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26736 if (p == NULL)
26737 return -1;
26738 vim_free(*bufp);
26739 *bufp = *fnamep = p;
26740 *fnamelen = (int)STRLEN(p);
26741 *usedlen += 2;
26742 }
26743
Bram Moolenaar071d4272004-06-13 20:20:40 +000026744 return valid;
26745}
26746
26747/*
26748 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26749 * "flags" can be "g" to do a global substitute.
26750 * Returns an allocated string, NULL for error.
26751 */
26752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026753do_string_sub(
26754 char_u *str,
26755 char_u *pat,
26756 char_u *sub,
26757 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758{
26759 int sublen;
26760 regmatch_T regmatch;
26761 int i;
26762 int do_all;
26763 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026764 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765 garray_T ga;
26766 char_u *ret;
26767 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026768 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026769
26770 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26771 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026772 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026773
26774 ga_init2(&ga, 1, 200);
26775
26776 do_all = (flags[0] == 'g');
26777
26778 regmatch.rm_ic = p_ic;
26779 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26780 if (regmatch.regprog != NULL)
26781 {
26782 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026783 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026784 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26785 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026786 /* Skip empty match except for first match. */
26787 if (regmatch.startp[0] == regmatch.endp[0])
26788 {
26789 if (zero_width == regmatch.startp[0])
26790 {
26791 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026792 i = MB_PTR2LEN(tail);
26793 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26794 (size_t)i);
26795 ga.ga_len += i;
26796 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026797 continue;
26798 }
26799 zero_width = regmatch.startp[0];
26800 }
26801
Bram Moolenaar071d4272004-06-13 20:20:40 +000026802 /*
26803 * Get some space for a temporary buffer to do the substitution
26804 * into. It will contain:
26805 * - The text up to where the match is.
26806 * - The substituted text.
26807 * - The text after the match.
26808 */
26809 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026810 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026811 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26812 {
26813 ga_clear(&ga);
26814 break;
26815 }
26816
26817 /* copy the text up to where the match is */
26818 i = (int)(regmatch.startp[0] - tail);
26819 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26820 /* add the substituted text */
26821 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26822 + ga.ga_len + i, TRUE, TRUE, FALSE);
26823 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026824 tail = regmatch.endp[0];
26825 if (*tail == NUL)
26826 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026827 if (!do_all)
26828 break;
26829 }
26830
26831 if (ga.ga_data != NULL)
26832 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26833
Bram Moolenaar473de612013-06-08 18:19:48 +020026834 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026835 }
26836
26837 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26838 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026839 if (p_cpo == empty_option)
26840 p_cpo = save_cpo;
26841 else
26842 /* Darn, evaluating {sub} expression changed the value. */
26843 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026844
26845 return ret;
26846}
26847
26848#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */