blob: c3b7bccf1d460d9de173955b2a6a21a330e91d1c [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar33570922005-01-25 22:26:29 +000030#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000032#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
33 be freed. */
34
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000092static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000093static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000094static char *e_undefvar = N_("E121: Undefined variable: %s");
95static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000096static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000097static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000098static char *e_listreq = N_("E714: List required");
99static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000101static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
102static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
103static char *e_funcdict = N_("E717: Dictionary entry already exists");
104static char *e_funcref = N_("E718: Funcref required");
105static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
106static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000107static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000108static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200109#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200110static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200111#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000112
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100113#define NAMESPACE_CHAR (char_u *)"abglstvw"
114
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200115static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000116#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
118/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000119 * Old Vim variables such as "v:version" are also available without the "v:".
120 * Also in functions. We need a special hashtable for them.
121 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000122static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123
124/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000125 * When recursively copying lists and dicts we need to remember which ones we
126 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000127 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 */
129static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130#define COPYID_INC 2
131#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132
Bram Moolenaar8502c702014-06-17 12:51:16 +0200133/* Abort conversion to string after a recursion error. */
134static int did_echo_string_emsg = FALSE;
135
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000137 * Array to hold the hashtab with variables local to each sourced script.
138 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000140typedef struct
141{
142 dictitem_T sv_var;
143 dict_T sv_dict;
144} scriptvar_T;
145
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200146static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
147#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
148#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150static int echo_attr = 0; /* attributes used for ":echo" */
151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000152/* Values for trans_function_name() argument: */
153#define TFN_INT 1 /* internal function name OK */
154#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100155#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
156
157/* Values for get_lval() flags argument: */
158#define GLV_QUIET TFN_QUIET /* no error messages */
159#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Structure to hold info for a user function.
163 */
164typedef struct ufunc ufunc_T;
165
166struct ufunc
167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000168 int uf_varargs; /* variable nr of arguments */
169 int uf_flags;
170 int uf_calls; /* nr of active calls */
171 garray_T uf_args; /* arguments */
172 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173#ifdef FEAT_PROFILE
174 int uf_profiling; /* TRUE when func is being profiled */
175 /* profiling the function as a whole */
176 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000177 proftime_T uf_tm_total; /* time spent in function + children */
178 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 proftime_T uf_tm_children; /* time spent in children this call */
180 /* profiling the function per line */
181 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T *uf_tml_total; /* time spent in a line + children */
183 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tml_start; /* start time for current line */
185 proftime_T uf_tml_children; /* time spent in children for this line */
186 proftime_T uf_tml_wait; /* start wait time for current line */
187 int uf_tml_idx; /* index of line being timed; -1 if none */
188 int uf_tml_execed; /* line being timed was executed */
189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 int uf_refcount; /* for numbered function: reference count */
193 char_u uf_name[1]; /* name of function (actually longer); can
194 start with <SNR>123_ (<SNR> is K_SPECIAL
195 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196};
197
198/* function flags */
199#define FC_ABORT 1 /* abort function on error */
200#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000201#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000206static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000209static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
210
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000211/* list heads for garbage collection */
212static dict_T *first_dict = NULL; /* list of all dicts */
213static list_T *first_list = NULL; /* list of all lists */
214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000215/* From user function to hashitem and back. */
216static ufunc_T dumuf;
217#define UF2HIKEY(fp) ((fp)->uf_name)
218#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
219#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
220
221#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
222#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar33570922005-01-25 22:26:29 +0000224#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
225#define VAR_SHORT_LEN 20 /* short variable name length */
226#define FIXVAR_CNT 12 /* number of fixed variables */
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000229typedef struct funccall_S funccall_T;
230
231struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 ufunc_T *func; /* function being called */
234 int linenr; /* next line to be executed */
235 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000236 struct /* fixed variables for arguments */
237 {
238 dictitem_T var; /* variable (without room for name) */
239 char_u room[VAR_SHORT_LEN]; /* room for the name */
240 } fixvar[FIXVAR_CNT];
241 dict_T l_vars; /* l: local function variables */
242 dictitem_T l_vars_var; /* variable for l: scope */
243 dict_T l_avars; /* a: argument variables */
244 dictitem_T l_avars_var; /* variable for a: scope */
245 list_T l_varlist; /* list for a:000 */
246 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 linenr_T breakpoint; /* next line with breakpoint or zero */
249 int dbg_tick; /* debug_tick when breakpoint was set */
250 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000251#ifdef FEAT_PROFILE
252 proftime_T prof_child; /* time spent in a child */
253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000254 funccall_T *caller; /* calling function or NULL */
255};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258 * Info used by a ":for" loop.
259 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261{
262 int fi_semicolon; /* TRUE if ending in '; var]' */
263 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 listwatch_T fi_lw; /* keep an eye on the item used. */
265 list_T *fi_list; /* list being used */
266} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000269 * Struct used by trans_function_name()
270 */
271typedef struct
272{
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000274 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dictitem_T *fd_di; /* Dictionary item used */
276} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000277
Bram Moolenaara7043832005-01-21 11:56:39 +0000278
279/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 * Array to hold the value of v: variables.
281 * The value is in a dictitem, so that it can also be used in the v: scope.
282 * The reason to use this table anyway is for very quick access to the
283 * variables with the VV_ defines.
284 */
285#include "version.h"
286
287/* values for vv_flags: */
288#define VV_COMPAT 1 /* compatible, also used without "v:" */
289#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000290#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000292#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294static struct vimvar
295{
296 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297 dictitem_T vv_di; /* value and name for key */
298 char vv_filler[16]; /* space for LONGEST name below!!! */
299 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300} vimvars[VV_LEN] =
301{
302 /*
303 * The order here must match the VV_ defines in vim.h!
304 * Initializing a union does not work, leave tv.vval empty to get zero's.
305 */
306 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("count1", VAR_NUMBER), VV_RO},
308 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
309 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
310 {VV_NAME("warningmsg", VAR_STRING), 0},
311 {VV_NAME("statusmsg", VAR_STRING), 0},
312 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
313 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
314 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("termresponse", VAR_STRING), VV_RO},
317 {VV_NAME("fname", VAR_STRING), VV_RO},
318 {VV_NAME("lang", VAR_STRING), VV_RO},
319 {VV_NAME("lc_time", VAR_STRING), VV_RO},
320 {VV_NAME("ctype", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
323 {VV_NAME("fname_in", VAR_STRING), VV_RO},
324 {VV_NAME("fname_out", VAR_STRING), VV_RO},
325 {VV_NAME("fname_new", VAR_STRING), VV_RO},
326 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
327 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
328 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
331 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("progname", VAR_STRING), VV_RO},
333 {VV_NAME("servername", VAR_STRING), VV_RO},
334 {VV_NAME("dying", VAR_NUMBER), VV_RO},
335 {VV_NAME("exception", VAR_STRING), VV_RO},
336 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
337 {VV_NAME("register", VAR_STRING), VV_RO},
338 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
339 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000340 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
341 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000342 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000343 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
344 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000345 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000350 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000351 {VV_NAME("swapname", VAR_STRING), VV_RO},
352 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000353 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200354 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000355 {VV_NAME("mouse_win", VAR_NUMBER), 0},
356 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
357 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000358 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100360 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000361 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200362 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200363 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200364 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200365 {VV_NAME("option_new", VAR_STRING), VV_RO},
366 {VV_NAME("option_old", VAR_STRING), VV_RO},
367 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100368 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100369 {VV_NAME("false", VAR_SPECIAL), VV_RO},
370 {VV_NAME("true", VAR_SPECIAL), VV_RO},
371 {VV_NAME("null", VAR_SPECIAL), VV_RO},
372 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000373};
374
375/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000376#define vv_type vv_di.di_tv.v_type
377#define vv_nr vv_di.di_tv.vval.v_number
378#define vv_float vv_di.di_tv.vval.v_float
379#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000380#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200381#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000382#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000383
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200384static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000385#define vimvarht vimvardict.dv_hashtab
386
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100387static void prepare_vimvar(int idx, typval_T *save_tv);
388static void restore_vimvar(int idx, typval_T *save_tv);
389static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
390static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
391static char_u *skip_var_one(char_u *arg);
392static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
393static void list_glob_vars(int *first);
394static void list_buf_vars(int *first);
395static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100397static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100399static void list_vim_vars(int *first);
400static void list_script_vars(int *first);
401static void list_func_vars(int *first);
402static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
403static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
404static int check_changedtick(char_u *arg);
405static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
406static void clear_lval(lval_T *lp);
407static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
408static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
409static void list_fix_watch(list_T *l, listitem_T *item);
410static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
411static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
412static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
413static void item_lock(typval_T *tv, int deep, int lock);
414static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100416static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
417static int eval1(char_u **arg, typval_T *rettv, int evaluate);
418static int eval2(char_u **arg, typval_T *rettv, int evaluate);
419static int eval3(char_u **arg, typval_T *rettv, int evaluate);
420static int eval4(char_u **arg, typval_T *rettv, int evaluate);
421static int eval5(char_u **arg, typval_T *rettv, int evaluate);
422static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
423static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
426static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
427static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
430static long list_len(list_T *l);
431static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
432static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
433static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
434static long list_find_nr(list_T *l, long idx, int *errorp);
435static long list_idx_of_item(list_T *l, listitem_T *item);
436static int list_append_number(list_T *l, varnumber_T n);
437static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
438static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
439static list_T *list_copy(list_T *orig, int deep, int copyID);
440static char_u *list2string(typval_T *tv, int copyID);
441static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
442static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
443static int free_unref_items(int copyID);
444static dictitem_T *dictitem_copy(dictitem_T *org);
445static void dictitem_remove(dict_T *dict, dictitem_T *item);
446static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
447static long dict_len(dict_T *d);
448static char_u *dict2string(typval_T *tv, int copyID);
449static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
450static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
451static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *string_quote(char_u *str, int function);
453static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
454static int find_internal_func(char_u *name);
455static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
456static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static void emsg_funcname(char *ermsg, char_u *name);
458static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_abs(typval_T *argvars, typval_T *rettv);
462static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void f_add(typval_T *argvars, typval_T *rettv);
465static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
466static void f_and(typval_T *argvars, typval_T *rettv);
467static void f_append(typval_T *argvars, typval_T *rettv);
468static void f_argc(typval_T *argvars, typval_T *rettv);
469static void f_argidx(typval_T *argvars, typval_T *rettv);
470static void f_arglistid(typval_T *argvars, typval_T *rettv);
471static void f_argv(typval_T *argvars, typval_T *rettv);
472static void f_assert_equal(typval_T *argvars, typval_T *rettv);
473static void f_assert_exception(typval_T *argvars, typval_T *rettv);
474static void f_assert_fails(typval_T *argvars, typval_T *rettv);
475static void f_assert_false(typval_T *argvars, typval_T *rettv);
476static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_asin(typval_T *argvars, typval_T *rettv);
479static void f_atan(typval_T *argvars, typval_T *rettv);
480static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_browse(typval_T *argvars, typval_T *rettv);
483static void f_browsedir(typval_T *argvars, typval_T *rettv);
484static void f_bufexists(typval_T *argvars, typval_T *rettv);
485static void f_buflisted(typval_T *argvars, typval_T *rettv);
486static void f_bufloaded(typval_T *argvars, typval_T *rettv);
487static void f_bufname(typval_T *argvars, typval_T *rettv);
488static void f_bufnr(typval_T *argvars, typval_T *rettv);
489static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
490static void f_byte2line(typval_T *argvars, typval_T *rettv);
491static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
492static void f_byteidx(typval_T *argvars, typval_T *rettv);
493static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
494static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100498#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
501static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100502static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100503# ifdef FEAT_JOB
504static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
505# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100506static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100507static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
508static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100509static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100510static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100513static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100514static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_changenr(typval_T *argvars, typval_T *rettv);
517static void f_char2nr(typval_T *argvars, typval_T *rettv);
518static void f_cindent(typval_T *argvars, typval_T *rettv);
519static void f_clearmatches(typval_T *argvars, typval_T *rettv);
520static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000521#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100522static void f_complete(typval_T *argvars, typval_T *rettv);
523static void f_complete_add(typval_T *argvars, typval_T *rettv);
524static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_confirm(typval_T *argvars, typval_T *rettv);
527static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_cos(typval_T *argvars, typval_T *rettv);
530static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_count(typval_T *argvars, typval_T *rettv);
533static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
534static void f_cursor(typval_T *argsvars, typval_T *rettv);
535static void f_deepcopy(typval_T *argvars, typval_T *rettv);
536static void f_delete(typval_T *argvars, typval_T *rettv);
537static void f_did_filetype(typval_T *argvars, typval_T *rettv);
538static void f_diff_filler(typval_T *argvars, typval_T *rettv);
539static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100540static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_empty(typval_T *argvars, typval_T *rettv);
542static void f_escape(typval_T *argvars, typval_T *rettv);
543static void f_eval(typval_T *argvars, typval_T *rettv);
544static void f_eventhandler(typval_T *argvars, typval_T *rettv);
545static void f_executable(typval_T *argvars, typval_T *rettv);
546static void f_exepath(typval_T *argvars, typval_T *rettv);
547static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_expand(typval_T *argvars, typval_T *rettv);
552static void f_extend(typval_T *argvars, typval_T *rettv);
553static void f_feedkeys(typval_T *argvars, typval_T *rettv);
554static void f_filereadable(typval_T *argvars, typval_T *rettv);
555static void f_filewritable(typval_T *argvars, typval_T *rettv);
556static void f_filter(typval_T *argvars, typval_T *rettv);
557static void f_finddir(typval_T *argvars, typval_T *rettv);
558static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_float2nr(typval_T *argvars, typval_T *rettv);
561static void f_floor(typval_T *argvars, typval_T *rettv);
562static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_fnameescape(typval_T *argvars, typval_T *rettv);
565static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
566static void f_foldclosed(typval_T *argvars, typval_T *rettv);
567static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
568static void f_foldlevel(typval_T *argvars, typval_T *rettv);
569static void f_foldtext(typval_T *argvars, typval_T *rettv);
570static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
571static void f_foreground(typval_T *argvars, typval_T *rettv);
572static void f_function(typval_T *argvars, typval_T *rettv);
573static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
574static void f_get(typval_T *argvars, typval_T *rettv);
575static void f_getbufline(typval_T *argvars, typval_T *rettv);
576static void f_getbufvar(typval_T *argvars, typval_T *rettv);
577static void f_getchar(typval_T *argvars, typval_T *rettv);
578static void f_getcharmod(typval_T *argvars, typval_T *rettv);
579static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
580static void f_getcmdline(typval_T *argvars, typval_T *rettv);
581static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
582static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
583static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
584static void f_getcwd(typval_T *argvars, typval_T *rettv);
585static void f_getfontname(typval_T *argvars, typval_T *rettv);
586static void f_getfperm(typval_T *argvars, typval_T *rettv);
587static void f_getfsize(typval_T *argvars, typval_T *rettv);
588static void f_getftime(typval_T *argvars, typval_T *rettv);
589static void f_getftype(typval_T *argvars, typval_T *rettv);
590static void f_getline(typval_T *argvars, typval_T *rettv);
591static void f_getmatches(typval_T *argvars, typval_T *rettv);
592static void f_getpid(typval_T *argvars, typval_T *rettv);
593static void f_getcurpos(typval_T *argvars, typval_T *rettv);
594static void f_getpos(typval_T *argvars, typval_T *rettv);
595static void f_getqflist(typval_T *argvars, typval_T *rettv);
596static void f_getreg(typval_T *argvars, typval_T *rettv);
597static void f_getregtype(typval_T *argvars, typval_T *rettv);
598static void f_gettabvar(typval_T *argvars, typval_T *rettv);
599static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
600static void f_getwinposx(typval_T *argvars, typval_T *rettv);
601static void f_getwinposy(typval_T *argvars, typval_T *rettv);
602static void f_getwinvar(typval_T *argvars, typval_T *rettv);
603static void f_glob(typval_T *argvars, typval_T *rettv);
604static void f_globpath(typval_T *argvars, typval_T *rettv);
605static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
606static void f_has(typval_T *argvars, typval_T *rettv);
607static void f_has_key(typval_T *argvars, typval_T *rettv);
608static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
609static void f_hasmapto(typval_T *argvars, typval_T *rettv);
610static void f_histadd(typval_T *argvars, typval_T *rettv);
611static void f_histdel(typval_T *argvars, typval_T *rettv);
612static void f_histget(typval_T *argvars, typval_T *rettv);
613static void f_histnr(typval_T *argvars, typval_T *rettv);
614static void f_hlID(typval_T *argvars, typval_T *rettv);
615static void f_hlexists(typval_T *argvars, typval_T *rettv);
616static void f_hostname(typval_T *argvars, typval_T *rettv);
617static void f_iconv(typval_T *argvars, typval_T *rettv);
618static void f_indent(typval_T *argvars, typval_T *rettv);
619static void f_index(typval_T *argvars, typval_T *rettv);
620static void f_input(typval_T *argvars, typval_T *rettv);
621static void f_inputdialog(typval_T *argvars, typval_T *rettv);
622static void f_inputlist(typval_T *argvars, typval_T *rettv);
623static void f_inputrestore(typval_T *argvars, typval_T *rettv);
624static void f_inputsave(typval_T *argvars, typval_T *rettv);
625static void f_inputsecret(typval_T *argvars, typval_T *rettv);
626static void f_insert(typval_T *argvars, typval_T *rettv);
627static void f_invert(typval_T *argvars, typval_T *rettv);
628static void f_isdirectory(typval_T *argvars, typval_T *rettv);
629static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100630#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
631static void f_isnan(typval_T *argvars, typval_T *rettv);
632#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100633static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100634#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100635# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100636static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100637# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100638static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100639static void f_job_start(typval_T *argvars, typval_T *rettv);
640static void f_job_stop(typval_T *argvars, typval_T *rettv);
641static void f_job_status(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100644static void f_js_decode(typval_T *argvars, typval_T *rettv);
645static void f_js_encode(typval_T *argvars, typval_T *rettv);
646static void f_json_decode(typval_T *argvars, typval_T *rettv);
647static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_keys(typval_T *argvars, typval_T *rettv);
649static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
650static void f_len(typval_T *argvars, typval_T *rettv);
651static void f_libcall(typval_T *argvars, typval_T *rettv);
652static void f_libcallnr(typval_T *argvars, typval_T *rettv);
653static void f_line(typval_T *argvars, typval_T *rettv);
654static void f_line2byte(typval_T *argvars, typval_T *rettv);
655static void f_lispindent(typval_T *argvars, typval_T *rettv);
656static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_log(typval_T *argvars, typval_T *rettv);
659static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200663#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100664static void f_map(typval_T *argvars, typval_T *rettv);
665static void f_maparg(typval_T *argvars, typval_T *rettv);
666static void f_mapcheck(typval_T *argvars, typval_T *rettv);
667static void f_match(typval_T *argvars, typval_T *rettv);
668static void f_matchadd(typval_T *argvars, typval_T *rettv);
669static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
670static void f_matcharg(typval_T *argvars, typval_T *rettv);
671static void f_matchdelete(typval_T *argvars, typval_T *rettv);
672static void f_matchend(typval_T *argvars, typval_T *rettv);
673static void f_matchlist(typval_T *argvars, typval_T *rettv);
674static void f_matchstr(typval_T *argvars, typval_T *rettv);
675static void f_max(typval_T *argvars, typval_T *rettv);
676static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100683#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
685static void f_nr2char(typval_T *argvars, typval_T *rettv);
686static void f_or(typval_T *argvars, typval_T *rettv);
687static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100690#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
695static void f_printf(typval_T *argvars, typval_T *rettv);
696static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
700#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_range(typval_T *argvars, typval_T *rettv);
704static void f_readfile(typval_T *argvars, typval_T *rettv);
705static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100706#ifdef FEAT_FLOAT
707static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_reltimestr(typval_T *argvars, typval_T *rettv);
710static void f_remote_expr(typval_T *argvars, typval_T *rettv);
711static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
712static void f_remote_peek(typval_T *argvars, typval_T *rettv);
713static void f_remote_read(typval_T *argvars, typval_T *rettv);
714static void f_remote_send(typval_T *argvars, typval_T *rettv);
715static void f_remove(typval_T *argvars, typval_T *rettv);
716static void f_rename(typval_T *argvars, typval_T *rettv);
717static void f_repeat(typval_T *argvars, typval_T *rettv);
718static void f_resolve(typval_T *argvars, typval_T *rettv);
719static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_screenattr(typval_T *argvars, typval_T *rettv);
724static void f_screenchar(typval_T *argvars, typval_T *rettv);
725static void f_screencol(typval_T *argvars, typval_T *rettv);
726static void f_screenrow(typval_T *argvars, typval_T *rettv);
727static void f_search(typval_T *argvars, typval_T *rettv);
728static void f_searchdecl(typval_T *argvars, typval_T *rettv);
729static void f_searchpair(typval_T *argvars, typval_T *rettv);
730static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
731static void f_searchpos(typval_T *argvars, typval_T *rettv);
732static void f_server2client(typval_T *argvars, typval_T *rettv);
733static void f_serverlist(typval_T *argvars, typval_T *rettv);
734static void f_setbufvar(typval_T *argvars, typval_T *rettv);
735static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
736static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
737static void f_setline(typval_T *argvars, typval_T *rettv);
738static void f_setloclist(typval_T *argvars, typval_T *rettv);
739static void f_setmatches(typval_T *argvars, typval_T *rettv);
740static void f_setpos(typval_T *argvars, typval_T *rettv);
741static void f_setqflist(typval_T *argvars, typval_T *rettv);
742static void f_setreg(typval_T *argvars, typval_T *rettv);
743static void f_settabvar(typval_T *argvars, typval_T *rettv);
744static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
745static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_shellescape(typval_T *argvars, typval_T *rettv);
750static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
751static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000752#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_sin(typval_T *argvars, typval_T *rettv);
754static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sort(typval_T *argvars, typval_T *rettv);
757static void f_soundfold(typval_T *argvars, typval_T *rettv);
758static void f_spellbadword(typval_T *argvars, typval_T *rettv);
759static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
760static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000761#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_sqrt(typval_T *argvars, typval_T *rettv);
763static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_str2nr(typval_T *argvars, typval_T *rettv);
766static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_stridx(typval_T *argvars, typval_T *rettv);
771static void f_string(typval_T *argvars, typval_T *rettv);
772static void f_strlen(typval_T *argvars, typval_T *rettv);
773static void f_strpart(typval_T *argvars, typval_T *rettv);
774static void f_strridx(typval_T *argvars, typval_T *rettv);
775static void f_strtrans(typval_T *argvars, typval_T *rettv);
776static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
777static void f_strwidth(typval_T *argvars, typval_T *rettv);
778static void f_submatch(typval_T *argvars, typval_T *rettv);
779static void f_substitute(typval_T *argvars, typval_T *rettv);
780static void f_synID(typval_T *argvars, typval_T *rettv);
781static void f_synIDattr(typval_T *argvars, typval_T *rettv);
782static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
783static void f_synstack(typval_T *argvars, typval_T *rettv);
784static void f_synconcealed(typval_T *argvars, typval_T *rettv);
785static void f_system(typval_T *argvars, typval_T *rettv);
786static void f_systemlist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
789static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
790static void f_taglist(typval_T *argvars, typval_T *rettv);
791static void f_tagfiles(typval_T *argvars, typval_T *rettv);
792static void f_tempname(typval_T *argvars, typval_T *rettv);
793static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200794#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100795static void f_tan(typval_T *argvars, typval_T *rettv);
796static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200797#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_tolower(typval_T *argvars, typval_T *rettv);
799static void f_toupper(typval_T *argvars, typval_T *rettv);
800static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_type(typval_T *argvars, typval_T *rettv);
805static void f_undofile(typval_T *argvars, typval_T *rettv);
806static void f_undotree(typval_T *argvars, typval_T *rettv);
807static void f_uniq(typval_T *argvars, typval_T *rettv);
808static void f_values(typval_T *argvars, typval_T *rettv);
809static void f_virtcol(typval_T *argvars, typval_T *rettv);
810static void f_visualmode(typval_T *argvars, typval_T *rettv);
811static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
812static void f_winbufnr(typval_T *argvars, typval_T *rettv);
813static void f_wincol(typval_T *argvars, typval_T *rettv);
814static void f_winheight(typval_T *argvars, typval_T *rettv);
815static void f_winline(typval_T *argvars, typval_T *rettv);
816static void f_winnr(typval_T *argvars, typval_T *rettv);
817static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
818static void f_winrestview(typval_T *argvars, typval_T *rettv);
819static void f_winsaveview(typval_T *argvars, typval_T *rettv);
820static void f_winwidth(typval_T *argvars, typval_T *rettv);
821static void f_writefile(typval_T *argvars, typval_T *rettv);
822static void f_wordcount(typval_T *argvars, typval_T *rettv);
823static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000824
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100825static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
826static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
827static int get_env_len(char_u **arg);
828static int get_id_len(char_u **arg);
829static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
830static 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 +0000831#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
832#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
833 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
835static int eval_isnamec(int c);
836static int eval_isnamec1(int c);
837static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
838static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static typval_T *alloc_string_tv(char_u *string);
840static void init_tv(typval_T *varp);
841static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100842#ifdef FEAT_FLOAT
843static float_T get_tv_float(typval_T *varp);
844#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100845static linenr_T get_tv_lnum(typval_T *argvars);
846static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
847static char_u *get_tv_string(typval_T *varp);
848static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
849static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
850static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
851static hashtab_T *find_var_ht(char_u *name, char_u **varname);
852static funccall_T *get_funccal(void);
853static void vars_clear_ext(hashtab_T *ht, int free_val);
854static void delete_var(hashtab_T *ht, hashitem_T *hi);
855static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
856static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
857static void set_var(char_u *name, typval_T *varp, int copy);
858static int var_check_ro(int flags, char_u *name, int use_gettext);
859static int var_check_fixed(int flags, char_u *name, int use_gettext);
860static int var_check_func_name(char_u *name, int new_var);
861static int valid_varname(char_u *varname);
862static int tv_check_lock(int lock, char_u *name, int use_gettext);
863static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
864static char_u *find_option_end(char_u **arg, int *opt_flags);
865static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
866static int eval_fname_script(char_u *p);
867static int eval_fname_sid(char_u *p);
868static void list_func_head(ufunc_T *fp, int indent);
869static ufunc_T *find_func(char_u *name);
870static int function_exists(char_u *name);
871static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static void func_do_profile(ufunc_T *fp);
874static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
875static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000876static int
877# ifdef __BORLANDC__
878 _RTLENTRYF
879# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000881static int
882# ifdef __BORLANDC__
883 _RTLENTRYF
884# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887static int script_autoload(char_u *name, int reload);
888static char_u *autoload_name(char_u *name);
889static void cat_func_name(char_u *buf, ufunc_T *fp);
890static void func_free(ufunc_T *fp);
891static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
892static int can_free_funccal(funccall_T *fc, int copyID) ;
893static void free_funccal(funccall_T *fc, int free_val);
894static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
895static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
896static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
897static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
898static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
899static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
900static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
901static int write_list(FILE *fd, list_T *list, int binary);
902static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904
905#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100906static int compare_func_name(const void *s1, const void *s2);
907static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908#endif
909
Bram Moolenaar33570922005-01-25 22:26:29 +0000910/*
911 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000912 */
913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000915{
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 int i;
917 struct vimvar *p;
918
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200919 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
920 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200921 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000922 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000923 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000924
925 for (i = 0; i < VV_LEN; ++i)
926 {
927 p = &vimvars[i];
928 STRCPY(p->vv_di.di_key, p->vv_name);
929 if (p->vv_flags & VV_RO)
930 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
931 else if (p->vv_flags & VV_RO_SBX)
932 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
933 else
934 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000935
936 /* add to v: scope dict, unless the value is not always available */
937 if (p->vv_type != VAR_UNKNOWN)
938 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000940 /* add to compat scope dict */
941 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100943 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
944
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000945 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100946 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200947 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100948 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100949
950 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
951 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
952 set_vim_var_nr(VV_NONE, VVAL_NONE);
953 set_vim_var_nr(VV_NULL, VVAL_NULL);
954
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200955 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200956
957#ifdef EBCDIC
958 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100959 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200960 */
961 sortFunctions();
962#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000963}
964
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965#if defined(EXITFREE) || defined(PROTO)
966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100967eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968{
969 int i;
970 struct vimvar *p;
971
972 for (i = 0; i < VV_LEN; ++i)
973 {
974 p = &vimvars[i];
975 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000976 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000977 vim_free(p->vv_str);
978 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000979 }
980 else if (p->vv_di.di_tv.v_type == VAR_LIST)
981 {
982 list_unref(p->vv_list);
983 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000985 }
986 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000987 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988 hash_clear(&compat_hashtab);
989
Bram Moolenaard9fba312005-06-26 22:34:35 +0000990 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100991# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200992 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100993# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000994
995 /* global variables */
996 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000998 /* autoloaded script names */
999 ga_clear_strings(&ga_loaded);
1000
Bram Moolenaarcca74132013-09-25 21:00:28 +02001001 /* Script-local variables. First clear all the variables and in a second
1002 * loop free the scriptvar_T, because a variable in one script might hold
1003 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 ga_clear(&ga_scripts);
1009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001010 /* unreferenced lists and dicts */
1011 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001012
1013 /* functions */
1014 free_all_functions();
1015 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001016}
1017#endif
1018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 * Return the name of the executed function.
1021 */
1022 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001023func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026}
1027
1028/*
1029 * Return the address holding the next breakpoint line for a funccall cookie.
1030 */
1031 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Return the address holding the debug tick for a funccall cookie.
1039 */
1040 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001041func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar33570922005-01-25 22:26:29 +00001043 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
1046/*
1047 * Return the nesting level for a funccall cookie.
1048 */
1049 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar33570922005-01-25 22:26:29 +00001052 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001056funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001058/* pointer to list of previously used funccal, still around because some
1059 * item in it is still being used. */
1060funccall_T *previous_funccal = NULL;
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/*
1063 * Return TRUE when a function was ended by a ":return" command.
1064 */
1065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001066current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 return current_funccal->returned;
1069}
1070
1071
1072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * Set an internal variable to a string value. Creates the variable if it does
1074 * not already exist.
1075 */
1076 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001079 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
1082 val = vim_strsave(value);
1083 if (val != NULL)
1084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092}
1093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001095static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096static char_u *redir_endp = NULL;
1097static char_u *redir_varname = NULL;
1098
1099/*
1100 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 * Returns OK if successfully completed the setup. FAIL otherwise.
1103 */
1104 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001105var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
1107 int save_emsg;
1108 int err;
1109 typval_T tv;
1110
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001111 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001112 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 {
1114 EMSG(_(e_invarg));
1115 return FAIL;
1116 }
1117
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 redir_varname = vim_strsave(name);
1120 if (redir_varname == NULL)
1121 return FAIL;
1122
1123 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1124 if (redir_lval == NULL)
1125 {
1126 var_redir_stop();
1127 return FAIL;
1128 }
1129
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 /* The output is stored in growarray "redir_ga" until redirection ends. */
1131 ga_init2(&redir_ga, (int)sizeof(char), 500);
1132
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001134 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001135 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1137 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001138 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_endp != NULL && *redir_endp != NUL)
1140 /* Trailing characters are present after the variable name */
1141 EMSG(_(e_trailing));
1142 else
1143 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
1146 return FAIL;
1147 }
1148
1149 /* check if we can write to the variable: set it to or append an empty
1150 * string */
1151 save_emsg = did_emsg;
1152 did_emsg = FALSE;
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = (char_u *)"";
1155 if (append)
1156 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1157 else
1158 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001161 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 if (err)
1163 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 var_redir_stop();
1166 return FAIL;
1167 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168
1169 return OK;
1170}
1171
1172/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 * Append "value[value_len]" to the variable set by var_redir_start().
1174 * The actual appending is postponed until redirection ends, because the value
1175 * appended may in fact be the string we write to, changing it may cause freed
1176 * memory to be used:
1177 * :redir => foo
1178 * :let foo
1179 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001182var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185
1186 if (redir_lval == NULL)
1187 return;
1188
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001195 {
1196 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001198 }
1199 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201}
1202
1203/*
1204 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 */
1207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001208var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 typval_T tv;
1211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 if (redir_lval != NULL)
1213 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001214 /* If there was no error: assign the text to the variable. */
1215 if (redir_endp != NULL)
1216 {
1217 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1218 tv.v_type = VAR_STRING;
1219 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001220 /* Call get_lval() again, if it's inside a Dict or List it may
1221 * have changed. */
1222 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001223 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001224 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1225 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1226 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001227 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 /* free the collected output */
1230 vim_free(redir_ga.ga_data);
1231 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 vim_free(redir_lval);
1234 redir_lval = NULL;
1235 }
1236 vim_free(redir_varname);
1237 redir_varname = NULL;
1238}
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240# if defined(FEAT_MBYTE) || defined(PROTO)
1241 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001242eval_charconvert(
1243 char_u *enc_from,
1244 char_u *enc_to,
1245 char_u *fname_from,
1246 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 int err = FALSE;
1249
1250 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1251 set_vim_var_string(VV_CC_TO, enc_to, -1);
1252 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1253 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1254 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1255 err = TRUE;
1256 set_vim_var_string(VV_CC_FROM, NULL, -1);
1257 set_vim_var_string(VV_CC_TO, NULL, -1);
1258 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260
1261 if (err)
1262 return FAIL;
1263 return OK;
1264}
1265# endif
1266
1267# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 int err = FALSE;
1272
1273 set_vim_var_string(VV_FNAME_IN, fname, -1);
1274 set_vim_var_string(VV_CMDARG, args, -1);
1275 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1276 err = TRUE;
1277 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1278 set_vim_var_string(VV_CMDARG, NULL, -1);
1279
1280 if (err)
1281 {
1282 mch_remove(fname);
1283 return FAIL;
1284 }
1285 return OK;
1286}
1287# endif
1288
1289# if defined(FEAT_DIFF) || defined(PROTO)
1290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291eval_diff(
1292 char_u *origfile,
1293 char_u *newfile,
1294 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int err = FALSE;
1297
1298 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1299 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1300 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1301 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1302 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1303 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1304 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1305}
1306
1307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_patch(
1309 char_u *origfile,
1310 char_u *difffile,
1311 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 int err;
1314
1315 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1316 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1317 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1318 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1319 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1320 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1321 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1322}
1323# endif
1324
1325/*
1326 * Top level evaluation function, returning a boolean.
1327 * Sets "error" to TRUE if there was an error.
1328 * Return TRUE or FALSE.
1329 */
1330 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001331eval_to_bool(
1332 char_u *arg,
1333 int *error,
1334 char_u **nextcmd,
1335 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 int retval = FALSE;
1339
1340 if (skip)
1341 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 else
1345 {
1346 *error = FALSE;
1347 if (!skip)
1348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001349 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352 }
1353 if (skip)
1354 --emsg_skip;
1355
1356 return retval;
1357}
1358
1359/*
1360 * Top level evaluation function, returning a string. If "skip" is TRUE,
1361 * only parsing to "nextcmd" is done, without reporting errors. Return
1362 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1363 */
1364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001365eval_to_string_skip(
1366 char_u *arg,
1367 char_u **nextcmd,
1368 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *retval;
1372
1373 if (skip)
1374 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 retval = NULL;
1377 else
1378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 retval = vim_strsave(get_tv_string(&tv));
1380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 if (skip)
1383 --emsg_skip;
1384
1385 return retval;
1386}
1387
1388/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001389 * Skip over an expression at "*pp".
1390 * Return FAIL for an error, OK otherwise.
1391 */
1392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001393skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394{
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396
1397 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001399}
1400
1401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001403 * When "convert" is TRUE convert a List into a sequence of lines and convert
1404 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Return pointer to allocated memory, or NULL for failure.
1406 */
1407 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001408eval_to_string(
1409 char_u *arg,
1410 char_u **nextcmd,
1411 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001416#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001417 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 retval = NULL;
1422 else
1423 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425 {
1426 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001427 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001428 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001429 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001430 if (tv.vval.v_list->lv_len > 0)
1431 ga_append(&ga, NL);
1432 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001433 ga_append(&ga, NUL);
1434 retval = (char_u *)ga.ga_data;
1435 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001436#ifdef FEAT_FLOAT
1437 else if (convert && tv.v_type == VAR_FLOAT)
1438 {
1439 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1440 retval = vim_strsave(numbuf);
1441 }
1442#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001443 else
1444 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447
1448 return retval;
1449}
1450
1451/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001452 * Call eval_to_string() without using current local variables and using
1453 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_string_safe(
1457 char_u *arg,
1458 char_u **nextcmd,
1459 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 char_u *retval;
1462 void *save_funccalp;
1463
1464 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001465 if (use_sandbox)
1466 ++sandbox;
1467 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 if (use_sandbox)
1470 --sandbox;
1471 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 restore_funccal(save_funccalp);
1473 return retval;
1474}
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476/*
1477 * Top level evaluation function, returning a number.
1478 * Evaluates "expr" silently.
1479 * Returns -1 for an error.
1480 */
1481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
Bram Moolenaar33570922005-01-25 22:26:29 +00001484 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001486 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 ++emsg_off;
1489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 retval = -1;
1492 else
1493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001494 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497 --emsg_off;
1498
1499 return retval;
1500}
1501
Bram Moolenaara40058a2005-07-11 22:42:07 +00001502/*
1503 * Prepare v: variable "idx" to be used.
1504 * Save the current typeval in "save_tv".
1505 * When not used yet add the variable to the v: hashtable.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509{
1510 *save_tv = vimvars[idx].vv_tv;
1511 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1512 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1513}
1514
1515/*
1516 * Restore v: variable "idx" to typeval "save_tv".
1517 * When no longer defined, remove the variable from the v: hashtable.
1518 */
1519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001521{
1522 hashitem_T *hi;
1523
Bram Moolenaara40058a2005-07-11 22:42:07 +00001524 vimvars[idx].vv_tv = *save_tv;
1525 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1526 {
1527 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1528 if (HASHITEM_EMPTY(hi))
1529 EMSG2(_(e_intern2), "restore_vimvar()");
1530 else
1531 hash_remove(&vimvarht, hi);
1532 }
1533}
1534
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001535#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536/*
1537 * Evaluate an expression to a list with suggestions.
1538 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001539 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 */
1541 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001542eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543{
1544 typval_T save_val;
1545 typval_T rettv;
1546 list_T *list = NULL;
1547 char_u *p = skipwhite(expr);
1548
1549 /* Set "v:val" to the bad word. */
1550 prepare_vimvar(VV_VAL, &save_val);
1551 vimvars[VV_VAL].vv_type = VAR_STRING;
1552 vimvars[VV_VAL].vv_str = badword;
1553 if (p_verbose == 0)
1554 ++emsg_off;
1555
1556 if (eval1(&p, &rettv, TRUE) == OK)
1557 {
1558 if (rettv.v_type != VAR_LIST)
1559 clear_tv(&rettv);
1560 else
1561 list = rettv.vval.v_list;
1562 }
1563
1564 if (p_verbose == 0)
1565 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566 restore_vimvar(VV_VAL, &save_val);
1567
1568 return list;
1569}
1570
1571/*
1572 * "list" is supposed to contain two items: a word and a number. Return the
1573 * word in "pp" and the number as the return value.
1574 * Return -1 if anything isn't right.
1575 * Used to get the good word and score from the eval_spell_expr() result.
1576 */
1577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 listitem_T *li;
1581
1582 li = list->lv_first;
1583 if (li == NULL)
1584 return -1;
1585 *pp = get_tv_string(&li->li_tv);
1586
1587 li = li->li_next;
1588 if (li == NULL)
1589 return -1;
1590 return get_tv_number(&li->li_tv);
1591}
1592#endif
1593
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001595 * Top level evaluation function.
1596 * Returns an allocated typval_T with the result.
1597 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598 */
1599 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601{
1602 typval_T *tv;
1603
1604 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001605 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606 {
1607 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001608 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001609 }
1610
1611 return tv;
1612}
1613
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617 * Uses argv[argc] for the function arguments. Only Number and String
1618 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001621 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622call_vim_function(
1623 char_u *func,
1624 int argc,
1625 char_u **argv,
1626 int safe, /* use the sandbox */
1627 int str_arg_only, /* all arguments are strings */
1628 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
Bram Moolenaar33570922005-01-25 22:26:29 +00001630 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 long n;
1632 int len;
1633 int i;
1634 int doesrange;
1635 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001638 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 for (i = 0; i < argc; i++)
1643 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001644 /* Pass a NULL or empty argument as an empty string */
1645 if (argv[i] == NULL || *argv[i] == NUL)
1646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001647 argvars[i].v_type = VAR_STRING;
1648 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001649 continue;
1650 }
1651
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001652 if (str_arg_only)
1653 len = 0;
1654 else
1655 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001656 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (len != 0 && len == (int)STRLEN(argv[i]))
1658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001659 argvars[i].v_type = VAR_NUMBER;
1660 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 else
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 argvars[i].v_type = VAR_STRING;
1665 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 }
1668
1669 if (safe)
1670 {
1671 save_funccalp = save_funccal();
1672 ++sandbox;
1673 }
1674
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1676 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (safe)
1680 {
1681 --sandbox;
1682 restore_funccal(save_funccalp);
1683 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 vim_free(argvars);
1685
1686 if (ret == FAIL)
1687 clear_tv(rettv);
1688
1689 return ret;
1690}
1691
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001692/*
1693 * Call vimL function "func" and return the result as a number.
1694 * Returns -1 when calling the function fails.
1695 * Uses argv[argc] for the function arguments.
1696 */
1697 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001698call_func_retnr(
1699 char_u *func,
1700 int argc,
1701 char_u **argv,
1702 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001703{
1704 typval_T rettv;
1705 long retval;
1706
1707 /* All arguments are passed as strings, no conversion to number. */
1708 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1709 return -1;
1710
1711 retval = get_tv_number_chk(&rettv, NULL);
1712 clear_tv(&rettv);
1713 return retval;
1714}
1715
1716#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1717 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1718
Bram Moolenaar4f688582007-07-24 12:34:30 +00001719# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001721 * Call vimL function "func" and return the result as a string.
1722 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 * Uses argv[argc] for the function arguments.
1724 */
1725 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726call_func_retstr(
1727 char_u *func,
1728 int argc,
1729 char_u **argv,
1730 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731{
1732 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001733 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001735 /* All arguments are passed as strings, no conversion to number. */
1736 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 return NULL;
1738
1739 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return retval;
1742}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001743# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001745/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001746 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001748 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 */
1750 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retlist(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756{
1757 typval_T rettv;
1758
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001759 /* All arguments are passed as strings, no conversion to number. */
1760 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 return NULL;
1762
1763 if (rettv.v_type != VAR_LIST)
1764 {
1765 clear_tv(&rettv);
1766 return NULL;
1767 }
1768
1769 return rettv.vval.v_list;
1770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
1772
1773/*
1774 * Save the current function call pointer, and set it to NULL.
1775 * Used when executing autocommands and for ":source".
1776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001780 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 current_funccal = NULL;
1783 return (void *)fc;
1784}
1785
1786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001789 funccall_T *fc = (funccall_T *)vfc;
1790
1791 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792}
1793
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794#if defined(FEAT_PROFILE) || defined(PROTO)
1795/*
1796 * Prepare profiling for entering a child or something else that is not
1797 * counted for the script/function itself.
1798 * Should always be called in pair with prof_child_exit().
1799 */
1800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001801prof_child_enter(
1802 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803{
1804 funccall_T *fc = current_funccal;
1805
1806 if (fc != NULL && fc->func->uf_profiling)
1807 profile_start(&fc->prof_child);
1808 script_prof_save(tm);
1809}
1810
1811/*
1812 * Take care of time spent in a child.
1813 * Should always be called after prof_child_enter().
1814 */
1815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816prof_child_exit(
1817 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001818{
1819 funccall_T *fc = current_funccal;
1820
1821 if (fc != NULL && fc->func->uf_profiling)
1822 {
1823 profile_end(&fc->prof_child);
1824 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1825 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1826 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1827 }
1828 script_prof_restore(tm);
1829}
1830#endif
1831
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_FOLDING
1834/*
1835 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1836 * it in "*cp". Doesn't give error messages.
1837 */
1838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int retval;
1843 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001844 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1845 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001848 if (use_sandbox)
1849 ++sandbox;
1850 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 retval = 0;
1854 else
1855 {
1856 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 if (tv.v_type == VAR_NUMBER)
1858 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001859 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 retval = 0;
1861 else
1862 {
1863 /* If the result is a string, check if there is a non-digit before
1864 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (!VIM_ISDIGIT(*s) && *s != '-')
1867 *cp = *s++;
1868 retval = atol((char *)s);
1869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001873 if (use_sandbox)
1874 --sandbox;
1875 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 return retval;
1878}
1879#endif
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 * ":let" list all variable values
1883 * ":let var1 var2" list variable values
1884 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 * ":let var += expr" assignment command.
1886 * ":let var -= expr" assignment command.
1887 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 */
1890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
1893 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 int var_count = 0;
1898 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001900 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
Bram Moolenaardb552d602006-03-23 22:59:57 +00001903 argend = skip_var_list(arg, &var_count, &semicolon);
1904 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001906 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1907 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 expr = skipwhite(argend);
1909 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1910 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001912 /*
1913 * ":let" without "=": list variables
1914 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 if (*arg == '[')
1916 EMSG(_(e_invarg));
1917 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_glob_vars(&first);
1924 list_buf_vars(&first);
1925 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001926#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001928#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_script_vars(&first);
1930 list_func_vars(&first);
1931 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 eap->nextcmd = check_nextcmd(arg);
1934 }
1935 else
1936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op[0] = '=';
1938 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1942 op[0] = *expr; /* +=, -= or .= */
1943 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 else
1946 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (eap->skip)
1949 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (eap->skip)
1952 {
1953 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 --emsg_skip;
1956 }
1957 else if (i != FAIL)
1958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 }
1964}
1965
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966/*
1967 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1968 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 * When "nextchars" is not NULL it points to a string with characters that
1970 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1971 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 * Returns OK or FAIL;
1973 */
1974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001975ex_let_vars(
1976 char_u *arg_start,
1977 typval_T *tv,
1978 int copy, /* copy values from "tv", don't move */
1979 int semicolon, /* from skip_var_list() */
1980 int var_count, /* from skip_var_list() */
1981 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982{
1983 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 listitem_T *item;
1987 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988
1989 if (*arg != '[')
1990 {
1991 /*
1992 * ":let var = expr" or ":for var in list"
1993 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 return FAIL;
1996 return OK;
1997 }
1998
1999 /*
2000 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2001 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002002 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 {
2004 EMSG(_(e_listreq));
2005 return FAIL;
2006 }
2007
2008 i = list_len(l);
2009 if (semicolon == 0 && var_count < i)
2010 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002011 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 return FAIL;
2013 }
2014 if (var_count - semicolon > i)
2015 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002016 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 return FAIL;
2018 }
2019
2020 item = l->lv_first;
2021 while (*arg != ']')
2022 {
2023 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 item = item->li_next;
2026 if (arg == NULL)
2027 return FAIL;
2028
2029 arg = skipwhite(arg);
2030 if (*arg == ';')
2031 {
2032 /* Put the rest of the list (may be empty) in the var after ';'.
2033 * Create a new list for this. */
2034 l = list_alloc();
2035 if (l == NULL)
2036 return FAIL;
2037 while (item != NULL)
2038 {
2039 list_append_tv(l, &item->li_tv);
2040 item = item->li_next;
2041 }
2042
2043 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002044 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 ltv.vval.v_list = l;
2046 l->lv_refcount = 1;
2047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2049 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 clear_tv(&ltv);
2051 if (arg == NULL)
2052 return FAIL;
2053 break;
2054 }
2055 else if (*arg != ',' && *arg != ']')
2056 {
2057 EMSG2(_(e_intern2), "ex_let_vars()");
2058 return FAIL;
2059 }
2060 }
2061
2062 return OK;
2063}
2064
2065/*
2066 * Skip over assignable variable "var" or list of variables "[var, var]".
2067 * Used for ":let varvar = expr" and ":for varvar in expr".
2068 * For "[var, var]" increment "*var_count" for each variable.
2069 * for "[var, var; var]" set "semicolon".
2070 * Return NULL for an error.
2071 */
2072 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002073skip_var_list(
2074 char_u *arg,
2075 int *var_count,
2076 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077{
2078 char_u *p, *s;
2079
2080 if (*arg == '[')
2081 {
2082 /* "[var, var]": find the matching ']'. */
2083 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002084 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 {
2086 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2087 s = skip_var_one(p);
2088 if (s == p)
2089 {
2090 EMSG2(_(e_invarg2), p);
2091 return NULL;
2092 }
2093 ++*var_count;
2094
2095 p = skipwhite(s);
2096 if (*p == ']')
2097 break;
2098 else if (*p == ';')
2099 {
2100 if (*semicolon == 1)
2101 {
2102 EMSG(_("Double ; in list of variables"));
2103 return NULL;
2104 }
2105 *semicolon = 1;
2106 }
2107 else if (*p != ',')
2108 {
2109 EMSG2(_(e_invarg2), p);
2110 return NULL;
2111 }
2112 }
2113 return p + 1;
2114 }
2115 else
2116 return skip_var_one(arg);
2117}
2118
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002120 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002121 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002124skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 if (*arg == '@' && arg[1] != NUL)
2127 return arg + 2;
2128 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2129 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130}
2131
Bram Moolenaara7043832005-01-21 11:56:39 +00002132/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 * List variables for hashtab "ht" with prefix "prefix".
2134 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002137list_hashtable_vars(
2138 hashtab_T *ht,
2139 char_u *prefix,
2140 int empty,
2141 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar33570922005-01-25 22:26:29 +00002143 hashitem_T *hi;
2144 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 int todo;
2146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002147 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2149 {
2150 if (!HASHITEM_EMPTY(hi))
2151 {
2152 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002153 di = HI2DI(hi);
2154 if (empty || di->di_tv.v_type != VAR_STRING
2155 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002157 }
2158 }
2159}
2160
2161/*
2162 * List global variables.
2163 */
2164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
2170/*
2171 * List buffer variables.
2172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002175{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 char_u numbuf[NUMBUFLEN];
2177
Bram Moolenaar429fa852013-04-15 12:27:36 +02002178 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180
2181 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2183 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184}
2185
2186/*
2187 * List window variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002192 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194}
2195
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196#ifdef FEAT_WINDOWS
2197/*
2198 * List tab page variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002203 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205}
2206#endif
2207
Bram Moolenaara7043832005-01-21 11:56:39 +00002208/*
2209 * List Vim variables.
2210 */
2211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215}
2216
2217/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218 * List script-local variables, if there is a script.
2219 */
2220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222{
2223 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002224 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2225 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226}
2227
2228/*
2229 * List function variables, if there is a function.
2230 */
2231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002232list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233{
2234 if (current_funccal != NULL)
2235 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002236 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237}
2238
2239/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 * List variables in "arg".
2241 */
2242 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244{
2245 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 char_u *name_start;
2249 char_u *arg_subsc;
2250 char_u *tofree;
2251 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252
2253 while (!ends_excmd(*arg) && !got_int)
2254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2259 {
2260 emsg_severe = TRUE;
2261 EMSG(_(e_trailing));
2262 break;
2263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* get_name_len() takes care of expanding curly braces */
2268 name_start = name = arg;
2269 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2270 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* This is mainly to keep test 49 working: when expanding
2273 * curly braces fails overrule the exception error message. */
2274 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 emsg_severe = TRUE;
2277 EMSG2(_(e_invarg2), arg);
2278 break;
2279 }
2280 error = TRUE;
2281 }
2282 else
2283 {
2284 if (tofree != NULL)
2285 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002286 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 else
2289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 /* handle d.key, l[idx], f(expr) */
2291 arg_subsc = arg;
2292 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 'g': list_glob_vars(first); break;
2301 case 'b': list_buf_vars(first); break;
2302 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 'v': list_vim_vars(first); break;
2307 case 's': list_script_vars(first); break;
2308 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 default:
2310 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
2313 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 {
2315 char_u numbuf[NUMBUFLEN];
2316 char_u *tf;
2317 int c;
2318 char_u *s;
2319
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002320 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 c = *arg;
2322 *arg = NUL;
2323 list_one_var_a((char_u *)"",
2324 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002325 tv.v_type,
2326 s == NULL ? (char_u *)"" : s,
2327 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 *arg = c;
2329 vim_free(tf);
2330 }
2331 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
2334 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335
2336 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002338
2339 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341
2342 return arg;
2343}
2344
2345/*
2346 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2347 * Returns a pointer to the char just after the var name.
2348 * Returns NULL if there is an error.
2349 */
2350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002351ex_let_one(
2352 char_u *arg, /* points to variable name */
2353 typval_T *tv, /* value to assign to variable */
2354 int copy, /* copy value from "tv" */
2355 char_u *endchars, /* valid chars after variable name or NULL */
2356 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357{
2358 int c1;
2359 char_u *name;
2360 char_u *p;
2361 char_u *arg_end = NULL;
2362 int len;
2363 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365
2366 /*
2367 * ":let $VAR = expr": Set environment variable.
2368 */
2369 if (*arg == '$')
2370 {
2371 /* Find the end of the name. */
2372 ++arg;
2373 name = arg;
2374 len = get_env_len(&arg);
2375 if (len == 0)
2376 EMSG2(_(e_invarg2), name - 1);
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 if (op != NULL && (*op == '+' || *op == '-'))
2380 EMSG2(_(e_letwrong), op);
2381 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002382 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002384 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 {
2386 c1 = name[len];
2387 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 p = get_tv_string_chk(tv);
2389 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 int mustfree = FALSE;
2392 char_u *s = vim_getenv(name, &mustfree);
2393
2394 if (s != NULL)
2395 {
2396 p = tofree = concat_str(s, p);
2397 if (mustfree)
2398 vim_free(s);
2399 }
2400 }
2401 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 if (STRICMP(name, "HOME") == 0)
2405 init_homedir();
2406 else if (didset_vim && STRICMP(name, "VIM") == 0)
2407 didset_vim = FALSE;
2408 else if (didset_vimruntime
2409 && STRICMP(name, "VIMRUNTIME") == 0)
2410 didset_vimruntime = FALSE;
2411 arg_end = arg;
2412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 }
2416 }
2417 }
2418
2419 /*
2420 * ":let &option = expr": Set option value.
2421 * ":let &l:option = expr": Set local option value.
2422 * ":let &g:option = expr": Set global option value.
2423 */
2424 else if (*arg == '&')
2425 {
2426 /* Find the end of the name. */
2427 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 if (p == NULL || (endchars != NULL
2429 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 EMSG(_(e_letunexp));
2431 else
2432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 long n;
2434 int opt_type;
2435 long numval;
2436 char_u *stringval = NULL;
2437 char_u *s;
2438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 c1 = *p;
2440 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441
2442 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 s = get_tv_string_chk(tv); /* != NULL if number or string */
2444 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 {
2446 opt_type = get_option_value(arg, &numval,
2447 &stringval, opt_flags);
2448 if ((opt_type == 1 && *op == '.')
2449 || (opt_type == 0 && *op != '.'))
2450 EMSG2(_(e_letwrong), op);
2451 else
2452 {
2453 if (opt_type == 1) /* number */
2454 {
2455 if (*op == '+')
2456 n = numval + n;
2457 else
2458 n = numval - n;
2459 }
2460 else if (opt_type == 0 && stringval != NULL) /* string */
2461 {
2462 s = concat_str(stringval, s);
2463 vim_free(stringval);
2464 stringval = s;
2465 }
2466 }
2467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 if (s != NULL)
2469 {
2470 set_option_value(arg, n, s, opt_flags);
2471 arg_end = p;
2472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476 }
2477
2478 /*
2479 * ":let @r = expr": Set register contents.
2480 */
2481 else if (*arg == '@')
2482 {
2483 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 if (op != NULL && (*op == '+' || *op == '-'))
2485 EMSG2(_(e_letwrong), op);
2486 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 EMSG(_(e_letunexp));
2489 else
2490 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 char_u *s;
2493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 p = get_tv_string_chk(tv);
2495 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002497 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 if (s != NULL)
2499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 vim_free(s);
2502 }
2503 }
2504 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 arg_end = arg + 1;
2508 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002509 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
2511 }
2512
2513 /*
2514 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002521 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2525 EMSG(_(e_letunexp));
2526 else
2527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 arg_end = p;
2530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
2534
2535 else
2536 EMSG2(_(e_invarg2), arg);
2537
2538 return arg_end;
2539}
2540
2541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2543 */
2544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002545check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546{
2547 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2548 {
2549 EMSG2(_(e_readonlyvar), arg);
2550 return TRUE;
2551 }
2552 return FALSE;
2553}
2554
2555/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Get an lval: variable, Dict item or List item that can be assigned a value
2557 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2558 * "name.key", "name.key[expr]" etc.
2559 * Indexing only works if "name" is an existing List or Dictionary.
2560 * "name" points to the start of the name.
2561 * If "rettv" is not NULL it points to the value to be assigned.
2562 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2563 * wrong; must end in space or cmd separator.
2564 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565 * flags:
2566 * GLV_QUIET: do not give error messages
2567 * GLV_NO_AUTOLOAD: do not use script autoloading
2568 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002570 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 */
2573 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002574get_lval(
2575 char_u *name,
2576 typval_T *rettv,
2577 lval_T *lp,
2578 int unlet,
2579 int skip,
2580 int flags, /* GLV_ values */
2581 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 char_u *expr_start, *expr_end;
2585 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 dictitem_T *v;
2587 typval_T var1;
2588 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002594 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598
2599 if (skip)
2600 {
2601 /* When skipping just find the end of the name. */
2602 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002603 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 }
2605
2606 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002607 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (expr_start != NULL)
2609 {
2610 /* Don't expand the name when we already know there is an error. */
2611 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2612 && *p != '[' && *p != '.')
2613 {
2614 EMSG(_(e_trailing));
2615 return NULL;
2616 }
2617
2618 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2619 if (lp->ll_exp_name == NULL)
2620 {
2621 /* Report an invalid expression in braces, unless the
2622 * expression evaluation has been cancelled due to an
2623 * aborting error, an interrupt, or an exception. */
2624 if (!aborting() && !quiet)
2625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002626 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 EMSG2(_(e_invarg2), name);
2628 return NULL;
2629 }
2630 }
2631 lp->ll_name = lp->ll_exp_name;
2632 }
2633 else
2634 lp->ll_name = name;
2635
2636 /* Without [idx] or .key we are done. */
2637 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2638 return p;
2639
2640 cc = *p;
2641 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002642 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (v == NULL && !quiet)
2644 EMSG2(_(e_undefvar), lp->ll_name);
2645 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 if (v == NULL)
2647 return NULL;
2648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /*
2650 * Loop until no more [idx] or .key is following.
2651 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002652 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2656 && !(lp->ll_tv->v_type == VAR_DICT
2657 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E689: Can only index a List or Dictionary"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E708: [:] must come last"));
2667 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 len = -1;
2671 if (*p == '.')
2672 {
2673 key = p + 1;
2674 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2675 ;
2676 if (len == 0)
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!quiet)
2679 EMSG(_(e_emptykey));
2680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = key + len;
2683 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (*p == ':')
2689 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 else
2691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 empty1 = FALSE;
2693 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var1) == NULL)
2696 {
2697 /* not a number or string */
2698 clear_tv(&var1);
2699 return NULL;
2700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 }
2702
2703 /* Optionally get the second index [ :expr]. */
2704 if (*p == ':')
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (rettv != NULL && (rettv->v_type != VAR_LIST
2715 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 p = skipwhite(p + 1);
2724 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 else
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (!empty1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 if (get_tv_string_chk(&var2) == NULL)
2736 {
2737 /* not a number or string */
2738 if (!empty1)
2739 clear_tv(&var1);
2740 clear_tv(&var2);
2741 return NULL;
2742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
2752 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (!empty1)
2754 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759
2760 /* Skip to past ']'. */
2761 ++p;
2762 }
2763
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
2766 if (len == -1)
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002769 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (*key == NUL)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (!quiet)
2773 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 }
2777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 lp->ll_list = NULL;
2779 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002782 /* When assigning to a scope dictionary check that a function and
2783 * variable name is valid (only variable name unless it is l: or
2784 * g: dictionary). Disallow overwriting a builtin function. */
2785 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 int prevval;
2788 int wrong;
2789
2790 if (len != -1)
2791 {
2792 prevval = key[len];
2793 key[len] = NUL;
2794 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002795 else
2796 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002797 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2798 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002800 || !valid_varname(key);
2801 if (len != -1)
2802 key[len] = prevval;
2803 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 return NULL;
2805 }
2806
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* Can't add "v:" variable. */
2810 if (lp->ll_dict == &vimvardict)
2811 {
2812 EMSG2(_(e_illvar), name);
2813 return NULL;
2814 }
2815
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002816 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (len == -1)
2822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 }
2825 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 if (len == -1)
2830 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 p = NULL;
2833 break;
2834 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002836 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 return NULL;
2838
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 if (len == -1)
2840 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843 else
2844 {
2845 /*
2846 * Get the number and item for the only or first index of the List.
2847 */
2848 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 else
2851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002852 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 clear_tv(&var1);
2854 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002855 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_list = lp->ll_tv->vval.v_list;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002860 if (lp->ll_n1 < 0)
2861 {
2862 lp->ll_n1 = 0;
2863 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2864 }
2865 }
2866 if (lp->ll_li == NULL)
2867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 }
2874
2875 /*
2876 * May need to find the item or absolute index for the second
2877 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 * When no index given: "lp->ll_empty2" is TRUE.
2879 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002883 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 {
2890 if (!quiet)
2891 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 }
2896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2898 if (lp->ll_n1 < 0)
2899 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2900 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 {
2902 if (!quiet)
2903 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 }
2911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 return p;
2913}
2914
2915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 */
2918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002919clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920{
2921 vim_free(lp->ll_exp_name);
2922 vim_free(lp->ll_newkey);
2923}
2924
2925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 */
2930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002931set_var_lval(
2932 lval_T *lp,
2933 char_u *endp,
2934 typval_T *rettv,
2935 int copy,
2936 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937{
2938 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *ri;
2940 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941
2942 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 cc = *endp;
2947 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 if (op != NULL && *op != '=')
2949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 if ((di == NULL
2958 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2959 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2960 FALSE)))
2961 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 set_var(lp->ll_name, &tv, FALSE);
2963 clear_tv(&tv);
2964 }
2965 }
2966 else
2967 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 else if (tv_check_lock(lp->ll_newkey == NULL
2972 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002974 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 else if (lp->ll_range)
2976 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 listitem_T *ll_li = lp->ll_li;
2978 int ll_n1 = lp->ll_n1;
2979
2980 /*
2981 * Check whether any of the list items is locked
2982 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002983 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002985 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 return;
2987 ri = ri->li_next;
2988 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2989 break;
2990 ll_li = ll_li->li_next;
2991 ++ll_n1;
2992 }
2993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /*
2995 * Assign the List values to the list items.
2996 */
2997 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 if (op != NULL && *op != '=')
3000 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3001 else
3002 {
3003 clear_tv(&lp->ll_li->li_tv);
3004 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = ri->li_next;
3007 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3008 break;
3009 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003012 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 ri = NULL;
3015 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 lp->ll_li = lp->ll_li->li_next;
3019 ++lp->ll_n1;
3020 }
3021 if (ri != NULL)
3022 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (lp->ll_empty2
3024 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 : lp->ll_n1 != lp->ll_n2)
3026 EMSG(_("E711: List value has not enough items"));
3027 }
3028 else
3029 {
3030 /*
3031 * Assign to a List or Dictionary item.
3032 */
3033 if (lp->ll_newkey != NULL)
3034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 if (op != NULL && *op != '=')
3036 {
3037 EMSG2(_(e_letwrong), op);
3038 return;
3039 }
3040
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 if (di == NULL)
3044 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3046 {
3047 vim_free(di);
3048 return;
3049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else if (op != NULL && *op != '=')
3053 {
3054 tv_op(lp->ll_tv, rettv, op);
3055 return;
3056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /*
3061 * Assign the value to the variable or list item.
3062 */
3063 if (copy)
3064 copy_tv(rettv, lp->ll_tv);
3065 else
3066 {
3067 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003068 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 }
3071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072}
3073
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3076 * Returns OK or FAIL.
3077 */
3078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080{
3081 long n;
3082 char_u numbuf[NUMBUFLEN];
3083 char_u *s;
3084
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003085 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3086 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3087 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 {
3089 switch (tv1->v_type)
3090 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003091 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 case VAR_DICT:
3093 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003094 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003095 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003096 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 break;
3098
3099 case VAR_LIST:
3100 if (*op != '+' || tv2->v_type != VAR_LIST)
3101 break;
3102 /* List += List */
3103 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3104 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3105 return OK;
3106
3107 case VAR_NUMBER:
3108 case VAR_STRING:
3109 if (tv2->v_type == VAR_LIST)
3110 break;
3111 if (*op == '+' || *op == '-')
3112 {
3113 /* nr += nr or nr -= nr*/
3114 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
3116 if (tv2->v_type == VAR_FLOAT)
3117 {
3118 float_T f = n;
3119
3120 if (*op == '+')
3121 f += tv2->vval.v_float;
3122 else
3123 f -= tv2->vval.v_float;
3124 clear_tv(tv1);
3125 tv1->v_type = VAR_FLOAT;
3126 tv1->vval.v_float = f;
3127 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129#endif
3130 {
3131 if (*op == '+')
3132 n += get_tv_number(tv2);
3133 else
3134 n -= get_tv_number(tv2);
3135 clear_tv(tv1);
3136 tv1->v_type = VAR_NUMBER;
3137 tv1->vval.v_number = n;
3138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 }
3140 else
3141 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 if (tv2->v_type == VAR_FLOAT)
3143 break;
3144
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 /* str .= str */
3146 s = get_tv_string(tv1);
3147 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3148 clear_tv(tv1);
3149 tv1->v_type = VAR_STRING;
3150 tv1->vval.v_string = s;
3151 }
3152 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153
3154#ifdef FEAT_FLOAT
3155 case VAR_FLOAT:
3156 {
3157 float_T f;
3158
3159 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3160 && tv2->v_type != VAR_NUMBER
3161 && tv2->v_type != VAR_STRING))
3162 break;
3163 if (tv2->v_type == VAR_FLOAT)
3164 f = tv2->vval.v_float;
3165 else
3166 f = get_tv_number(tv2);
3167 if (*op == '+')
3168 tv1->vval.v_float += f;
3169 else
3170 tv1->vval.v_float -= f;
3171 }
3172 return OK;
3173#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 }
3176
3177 EMSG2(_(e_letwrong), op);
3178 return FAIL;
3179}
3180
3181/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 * Add a watcher to a list.
3183 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003185list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186{
3187 lw->lw_next = l->lv_watch;
3188 l->lv_watch = lw;
3189}
3190
3191/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003192 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 * No warning when it isn't found...
3194 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197{
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199
3200 lwp = &l->lv_watch;
3201 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3202 {
3203 if (lw == lwrem)
3204 {
3205 *lwp = lw->lw_next;
3206 break;
3207 }
3208 lwp = &lw->lw_next;
3209 }
3210}
3211
3212/*
3213 * Just before removing an item from a list: advance watchers to the next
3214 * item.
3215 */
3216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003217list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218{
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220
3221 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3222 if (lw->lw_item == item)
3223 lw->lw_item = item->li_next;
3224}
3225
3226/*
3227 * Evaluate the expression used in a ":for var in expr" command.
3228 * "arg" points to "var".
3229 * Set "*errp" to TRUE for an error, FALSE otherwise;
3230 * Return a pointer that holds the info. Null when there is an error.
3231 */
3232 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003233eval_for_line(
3234 char_u *arg,
3235 int *errp,
3236 char_u **nextcmdp,
3237 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238{
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 typval_T tv;
3242 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243
3244 *errp = TRUE; /* default: there is an error */
3245
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 if (fi == NULL)
3248 return NULL;
3249
3250 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3251 if (expr == NULL)
3252 return fi;
3253
3254 expr = skipwhite(expr);
3255 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3256 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003257 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 return fi;
3259 }
3260
3261 if (skip)
3262 ++emsg_skip;
3263 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3264 {
3265 *errp = FALSE;
3266 if (!skip)
3267 {
3268 l = tv.vval.v_list;
3269 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003271 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 clear_tv(&tv);
3273 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 else
3275 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003276 /* No need to increment the refcount, it's already set for the
3277 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 fi->fi_list = l;
3279 list_add_watch(l, &fi->fi_lw);
3280 fi->fi_lw.lw_item = l->lv_first;
3281 }
3282 }
3283 }
3284 if (skip)
3285 --emsg_skip;
3286
3287 return fi;
3288}
3289
3290/*
3291 * Use the first item in a ":for" list. Advance to the next.
3292 * Assign the values to the variable (list). "arg" points to the first one.
3293 * Return TRUE when a valid item was found, FALSE when at end of list or
3294 * something wrong.
3295 */
3296 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003297next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003299 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003301 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302
3303 item = fi->fi_lw.lw_item;
3304 if (item == NULL)
3305 result = FALSE;
3306 else
3307 {
3308 fi->fi_lw.lw_item = item->li_next;
3309 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3310 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3311 }
3312 return result;
3313}
3314
3315/*
3316 * Free the structure used to store info used by ":for".
3317 */
3318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320{
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003323 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 list_unref(fi->fi_list);
3327 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 vim_free(fi);
3329}
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3332
3333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334set_context_for_expression(
3335 expand_T *xp,
3336 char_u *arg,
3337 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 int got_eq = FALSE;
3340 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 if (cmdidx == CMD_let)
3344 {
3345 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003346 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 {
3348 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003349 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 {
3351 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353 if (vim_iswhite(*p))
3354 break;
3355 }
3356 return;
3357 }
3358 }
3359 else
3360 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3361 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 while ((xp->xp_pattern = vim_strpbrk(arg,
3363 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3364 {
3365 c = *xp->xp_pattern;
3366 if (c == '&')
3367 {
3368 c = xp->xp_pattern[1];
3369 if (c == '&')
3370 {
3371 ++xp->xp_pattern;
3372 xp->xp_context = cmdidx != CMD_let || got_eq
3373 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3374 }
3375 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3379 xp->xp_pattern += 2;
3380
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 else if (c == '$')
3384 {
3385 /* environment variable */
3386 xp->xp_context = EXPAND_ENV_VARS;
3387 }
3388 else if (c == '=')
3389 {
3390 got_eq = TRUE;
3391 xp->xp_context = EXPAND_EXPRESSION;
3392 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 && xp->xp_context == EXPAND_FUNCTIONS
3395 && vim_strchr(xp->xp_pattern, '(') == NULL)
3396 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003397 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 break;
3399 }
3400 else if (cmdidx != CMD_let || got_eq)
3401 {
3402 if (c == '"') /* string */
3403 {
3404 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3405 if (c == '\\' && xp->xp_pattern[1] != NUL)
3406 ++xp->xp_pattern;
3407 xp->xp_context = EXPAND_NOTHING;
3408 }
3409 else if (c == '\'') /* literal string */
3410 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003411 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3413 /* skip */ ;
3414 xp->xp_context = EXPAND_NOTHING;
3415 }
3416 else if (c == '|')
3417 {
3418 if (xp->xp_pattern[1] == '|')
3419 {
3420 ++xp->xp_pattern;
3421 xp->xp_context = EXPAND_EXPRESSION;
3422 }
3423 else
3424 xp->xp_context = EXPAND_COMMANDS;
3425 }
3426 else
3427 xp->xp_context = EXPAND_EXPRESSION;
3428 }
3429 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003430 /* Doesn't look like something valid, expand as an expression
3431 * anyway. */
3432 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 arg = xp->xp_pattern;
3434 if (*arg != NUL)
3435 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3436 /* skip */ ;
3437 }
3438 xp->xp_pattern = arg;
3439}
3440
3441#endif /* FEAT_CMDL_COMPL */
3442
3443/*
3444 * ":1,25call func(arg1, arg2)" function call.
3445 */
3446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003447ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 char_u *arg = eap->arg;
3450 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003452 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 linenr_T lnum;
3456 int doesrange;
3457 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003460 if (eap->skip)
3461 {
3462 /* trans_function_name() doesn't work well when skipping, use eval0()
3463 * instead to skip to any following command, e.g. for:
3464 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003465 ++emsg_skip;
3466 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3467 clear_tv(&rettv);
3468 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 return;
3470 }
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003473 if (fudi.fd_newkey != NULL)
3474 {
3475 /* Still need to give an error message for missing key. */
3476 EMSG2(_(e_dictkey), fudi.fd_newkey);
3477 vim_free(fudi.fd_newkey);
3478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 if (tofree == NULL)
3480 return;
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 /* Increase refcount on dictionary, it could get deleted when evaluating
3483 * the arguments. */
3484 if (fudi.fd_dict != NULL)
3485 ++fudi.fd_dict->dv_refcount;
3486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003489 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 /* Skip white space to allow ":call func ()". Not good, but required for
3492 * backward compatibility. */
3493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (*startarg != '(')
3497 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003498 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 goto end;
3500 }
3501
3502 /*
3503 * When skipping, evaluate the function once, to find the end of the
3504 * arguments.
3505 * When the function takes a range, this is discovered after the first
3506 * call, and the loop is broken.
3507 */
3508 if (eap->skip)
3509 {
3510 ++emsg_skip;
3511 lnum = eap->line2; /* do it once, also with an invalid range */
3512 }
3513 else
3514 lnum = eap->line1;
3515 for ( ; lnum <= eap->line2; ++lnum)
3516 {
3517 if (!eap->skip && eap->addr_count > 0)
3518 {
3519 curwin->w_cursor.lnum = lnum;
3520 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003521#ifdef FEAT_VIRTUALEDIT
3522 curwin->w_cursor.coladd = 0;
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 eap->line1, eap->line2, &doesrange,
3528 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003533
3534 /* Handle a function returning a Funcref, Dictionary or List. */
3535 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3536 {
3537 failed = TRUE;
3538 break;
3539 }
3540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (doesrange || eap->skip)
3543 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (aborting())
3550 break;
3551 }
3552 if (eap->skip)
3553 --emsg_skip;
3554
3555 if (!failed)
3556 {
3557 /* Check for trailing illegal characters and a following command. */
3558 if (!ends_excmd(*arg))
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 else
3564 eap->nextcmd = check_nextcmd(arg);
3565 }
3566
3567end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * ":unlet[!] var1 ... " command.
3574 */
3575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003576ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 ex_unletlock(eap, eap->arg, 0);
3579}
3580
3581/*
3582 * ":lockvar" and ":unlockvar" commands
3583 */
3584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588 int deep = 2;
3589
3590 if (eap->forceit)
3591 deep = -1;
3592 else if (vim_isdigit(*arg))
3593 {
3594 deep = getdigits(&arg);
3595 arg = skipwhite(arg);
3596 }
3597
3598 ex_unletlock(eap, arg, deep);
3599}
3600
3601/*
3602 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3603 */
3604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003605ex_unletlock(
3606 exarg_T *eap,
3607 char_u *argstart,
3608 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609{
3610 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 do
3616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003618 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003619 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 if (lv.ll_name == NULL)
3621 error = TRUE; /* error but continue parsing */
3622 if (name_end == NULL || (!vim_iswhite(*name_end)
3623 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 if (name_end != NULL)
3626 {
3627 emsg_severe = TRUE;
3628 EMSG(_(e_trailing));
3629 }
3630 if (!(eap->skip || error))
3631 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 break;
3633 }
3634
3635 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636 {
3637 if (eap->cmdidx == CMD_unlet)
3638 {
3639 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3640 error = TRUE;
3641 }
3642 else
3643 {
3644 if (do_lock_var(&lv, name_end, deep,
3645 eap->cmdidx == CMD_lockvar) == FAIL)
3646 error = TRUE;
3647 }
3648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (!eap->skip)
3651 clear_lval(&lv);
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 arg = skipwhite(name_end);
3654 } while (!ends_excmd(*arg));
3655
3656 eap->nextcmd = check_nextcmd(arg);
3657}
3658
Bram Moolenaar8c711452005-01-14 21:53:12 +00003659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003660do_unlet_var(
3661 lval_T *lp,
3662 char_u *name_end,
3663 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 int ret = OK;
3666 int cc;
3667
3668 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 cc = *name_end;
3671 *name_end = NUL;
3672
3673 /* Normal name or expanded name. */
3674 if (check_changedtick(lp->ll_name))
3675 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003676 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 else if (lp->ll_range)
3686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003689 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690
3691 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3692 {
3693 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003694 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 return FAIL;
3696 ll_li = li;
3697 ++ll_n1;
3698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699
3700 /* Delete a range of List items. */
3701 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3702 {
3703 li = lp->ll_li->li_next;
3704 listitem_remove(lp->ll_list, lp->ll_li);
3705 lp->ll_li = li;
3706 ++lp->ll_n1;
3707 }
3708 }
3709 else
3710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 /* unlet a List item. */
3713 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003716 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 }
3718
3719 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722/*
3723 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003724 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003727do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaar33570922005-01-25 22:26:29 +00003729 hashtab_T *ht;
3730 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003731 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003733 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaar33570922005-01-25 22:26:29 +00003735 ht = find_var_ht(name, &varname);
3736 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003738 if (ht == &globvarht)
3739 d = &globvardict;
3740 else if (current_funccal != NULL
3741 && ht == &current_funccal->l_vars.dv_hashtab)
3742 d = &current_funccal->l_vars;
3743 else if (ht == &compat_hashtab)
3744 d = &vimvardict;
3745 else
3746 {
3747 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3748 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3749 }
3750 if (d == NULL)
3751 {
3752 EMSG2(_(e_intern2), "do_unlet()");
3753 return FAIL;
3754 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003755 hi = hash_find(ht, varname);
3756 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003757 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003758 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003759 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003760 || var_check_ro(di->di_flags, name, FALSE)
3761 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003762 return FAIL;
3763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 delete_var(ht, hi);
3765 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 if (forceit)
3769 return OK;
3770 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return FAIL;
3772}
3773
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774/*
3775 * Lock or unlock variable indicated by "lp".
3776 * "deep" is the levels to go (-1 for unlimited);
3777 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3778 */
3779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003780do_lock_var(
3781 lval_T *lp,
3782 char_u *name_end,
3783 int deep,
3784 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003785{
3786 int ret = OK;
3787 int cc;
3788 dictitem_T *di;
3789
3790 if (deep == 0) /* nothing to do */
3791 return OK;
3792
3793 if (lp->ll_tv == NULL)
3794 {
3795 cc = *name_end;
3796 *name_end = NUL;
3797
3798 /* Normal name or expanded name. */
3799 if (check_changedtick(lp->ll_name))
3800 ret = FAIL;
3801 else
3802 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003803 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804 if (di == NULL)
3805 ret = FAIL;
3806 else
3807 {
3808 if (lock)
3809 di->di_flags |= DI_FLAGS_LOCK;
3810 else
3811 di->di_flags &= ~DI_FLAGS_LOCK;
3812 item_lock(&di->di_tv, deep, lock);
3813 }
3814 }
3815 *name_end = cc;
3816 }
3817 else if (lp->ll_range)
3818 {
3819 listitem_T *li = lp->ll_li;
3820
3821 /* (un)lock a range of List items. */
3822 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3823 {
3824 item_lock(&li->li_tv, deep, lock);
3825 li = li->li_next;
3826 ++lp->ll_n1;
3827 }
3828 }
3829 else if (lp->ll_list != NULL)
3830 /* (un)lock a List item. */
3831 item_lock(&lp->ll_li->li_tv, deep, lock);
3832 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003833 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003834 item_lock(&lp->ll_di->di_tv, deep, lock);
3835
3836 return ret;
3837}
3838
3839/*
3840 * Lock or unlock an item. "deep" is nr of levels to go.
3841 */
3842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003843item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003844{
3845 static int recurse = 0;
3846 list_T *l;
3847 listitem_T *li;
3848 dict_T *d;
3849 hashitem_T *hi;
3850 int todo;
3851
3852 if (recurse >= DICT_MAXNEST)
3853 {
3854 EMSG(_("E743: variable nested too deep for (un)lock"));
3855 return;
3856 }
3857 if (deep == 0)
3858 return;
3859 ++recurse;
3860
3861 /* lock/unlock the item itself */
3862 if (lock)
3863 tv->v_lock |= VAR_LOCKED;
3864 else
3865 tv->v_lock &= ~VAR_LOCKED;
3866
3867 switch (tv->v_type)
3868 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003869 case VAR_UNKNOWN:
3870 case VAR_NUMBER:
3871 case VAR_STRING:
3872 case VAR_FUNC:
3873 case VAR_FLOAT:
3874 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003875 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003876 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003877 break;
3878
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003879 case VAR_LIST:
3880 if ((l = tv->vval.v_list) != NULL)
3881 {
3882 if (lock)
3883 l->lv_lock |= VAR_LOCKED;
3884 else
3885 l->lv_lock &= ~VAR_LOCKED;
3886 if (deep < 0 || deep > 1)
3887 /* recursive: lock/unlock the items the List contains */
3888 for (li = l->lv_first; li != NULL; li = li->li_next)
3889 item_lock(&li->li_tv, deep - 1, lock);
3890 }
3891 break;
3892 case VAR_DICT:
3893 if ((d = tv->vval.v_dict) != NULL)
3894 {
3895 if (lock)
3896 d->dv_lock |= VAR_LOCKED;
3897 else
3898 d->dv_lock &= ~VAR_LOCKED;
3899 if (deep < 0 || deep > 1)
3900 {
3901 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003902 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3904 {
3905 if (!HASHITEM_EMPTY(hi))
3906 {
3907 --todo;
3908 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3909 }
3910 }
3911 }
3912 }
3913 }
3914 --recurse;
3915}
3916
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003918 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3919 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003920 */
3921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003922tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003923{
3924 return (tv->v_lock & VAR_LOCKED)
3925 || (tv->v_type == VAR_LIST
3926 && tv->vval.v_list != NULL
3927 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3928 || (tv->v_type == VAR_DICT
3929 && tv->vval.v_dict != NULL
3930 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3931}
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3934/*
3935 * Delete all "menutrans_" variables.
3936 */
3937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003938del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003941 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003944 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 {
3947 if (!HASHITEM_EMPTY(hi))
3948 {
3949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3951 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003952 }
3953 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955}
3956#endif
3957
3958#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3959
3960/*
3961 * Local string buffer for the next two functions to store a variable name
3962 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3963 * get_user_var_name().
3964 */
3965
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003966static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
3968static char_u *varnamebuf = NULL;
3969static int varnamebuflen = 0;
3970
3971/*
3972 * Function to concatenate a prefix and a variable name.
3973 */
3974 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 int len;
3978
3979 len = (int)STRLEN(name) + 3;
3980 if (len > varnamebuflen)
3981 {
3982 vim_free(varnamebuf);
3983 len += 10; /* some additional space */
3984 varnamebuf = alloc(len);
3985 if (varnamebuf == NULL)
3986 {
3987 varnamebuflen = 0;
3988 return NULL;
3989 }
3990 varnamebuflen = len;
3991 }
3992 *varnamebuf = prefix;
3993 varnamebuf[1] = ':';
3994 STRCPY(varnamebuf + 2, name);
3995 return varnamebuf;
3996}
3997
3998/*
3999 * Function given to ExpandGeneric() to obtain the list of user defined
4000 * (global/buffer/window/built-in) variable names.
4001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004003get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004005 static long_u gdone;
4006 static long_u bdone;
4007 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004008#ifdef FEAT_WINDOWS
4009 static long_u tdone;
4010#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004011 static int vidx;
4012 static hashitem_T *hi;
4013 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004017 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018#ifdef FEAT_WINDOWS
4019 tdone = 0;
4020#endif
4021 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004022
4023 /* Global variables */
4024 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004027 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004028 else
4029 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 while (HASHITEM_EMPTY(hi))
4031 ++hi;
4032 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4033 return cat_prefix_varname('g', hi->hi_key);
4034 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004036
4037 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004038 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004039 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004043 else
4044 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 while (HASHITEM_EMPTY(hi))
4046 ++hi;
4047 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return (char_u *)"b:changedtick";
4053 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004054
4055 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004056 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004057 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004059 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004061 else
4062 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 while (HASHITEM_EMPTY(hi))
4064 ++hi;
4065 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004067
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068#ifdef FEAT_WINDOWS
4069 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004070 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071 if (tdone < ht->ht_used)
4072 {
4073 if (tdone++ == 0)
4074 hi = ht->ht_array;
4075 else
4076 ++hi;
4077 while (HASHITEM_EMPTY(hi))
4078 ++hi;
4079 return cat_prefix_varname('t', hi->hi_key);
4080 }
4081#endif
4082
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 /* v: variables */
4084 if (vidx < VV_LEN)
4085 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087 vim_free(varnamebuf);
4088 varnamebuf = NULL;
4089 varnamebuflen = 0;
4090 return NULL;
4091}
4092
4093#endif /* FEAT_CMDL_COMPL */
4094
4095/*
4096 * types for expressions.
4097 */
4098typedef enum
4099{
4100 TYPE_UNKNOWN = 0
4101 , TYPE_EQUAL /* == */
4102 , TYPE_NEQUAL /* != */
4103 , TYPE_GREATER /* > */
4104 , TYPE_GEQUAL /* >= */
4105 , TYPE_SMALLER /* < */
4106 , TYPE_SEQUAL /* <= */
4107 , TYPE_MATCH /* =~ */
4108 , TYPE_NOMATCH /* !~ */
4109} exptype_T;
4110
4111/*
4112 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4115 */
4116
4117/*
4118 * Handle zero level expression.
4119 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004121 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Return OK or FAIL.
4123 */
4124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004125eval0(
4126 char_u *arg,
4127 typval_T *rettv,
4128 char_u **nextcmd,
4129 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 int ret;
4132 char_u *p;
4133
4134 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (ret == FAIL || !ends_excmd(*p))
4137 {
4138 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 /*
4141 * Report the invalid expression unless the expression evaluation has
4142 * been cancelled due to an aborting error, an interrupt, or an
4143 * exception.
4144 */
4145 if (!aborting())
4146 EMSG2(_(e_invexpr2), arg);
4147 ret = FAIL;
4148 }
4149 if (nextcmd != NULL)
4150 *nextcmd = check_nextcmd(p);
4151
4152 return ret;
4153}
4154
4155/*
4156 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004157 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 *
4159 * "arg" must point to the first non-white of the expression.
4160 * "arg" is advanced to the next non-white after the recognized expression.
4161 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004162 * Note: "rettv.v_lock" is not set.
4163 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 * Return OK or FAIL.
4165 */
4166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004167eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004170 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /*
4173 * Get the first variable.
4174 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return FAIL;
4177
4178 if ((*arg)[0] == '?')
4179 {
4180 result = FALSE;
4181 if (evaluate)
4182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 int error = FALSE;
4184
4185 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191
4192 /*
4193 * Get the second variable.
4194 */
4195 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 return FAIL;
4198
4199 /*
4200 * Check for the ":".
4201 */
4202 if ((*arg)[0] != ':')
4203 {
4204 EMSG(_("E109: Missing ':' after '?'"));
4205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208 }
4209
4210 /*
4211 * Get the third variable.
4212 */
4213 *arg = skipwhite(*arg + 1);
4214 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4215 {
4216 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return FAIL;
4219 }
4220 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004221 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
4224 return OK;
4225}
4226
4227/*
4228 * Handle first level expression:
4229 * expr2 || expr2 || expr2 logical OR
4230 *
4231 * "arg" must point to the first non-white of the expression.
4232 * "arg" is advanced to the next non-white after the recognized expression.
4233 *
4234 * Return OK or FAIL.
4235 */
4236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004237eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "||".
4252 */
4253 first = TRUE;
4254 result = FALSE;
4255 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && !result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle second level expression:
4297 * expr3 && expr3 && expr3 logical AND
4298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004305eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 long result;
4309 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 /*
4319 * Repeat until there is no following "&&".
4320 */
4321 first = TRUE;
4322 result = TRUE;
4323 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4324 {
4325 if (evaluate && first)
4326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (error)
4331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 first = FALSE;
4333 }
4334
4335 /*
4336 * Get the second variable.
4337 */
4338 *arg = skipwhite(*arg + 2);
4339 if (eval4(arg, &var2, evaluate && result) == FAIL)
4340 return FAIL;
4341
4342 /*
4343 * Compute the result.
4344 */
4345 if (evaluate && result)
4346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004347 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (error)
4351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 if (evaluate)
4354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 rettv->v_type = VAR_NUMBER;
4356 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358 }
4359
4360 return OK;
4361}
4362
4363/*
4364 * Handle third level expression:
4365 * var1 == var2
4366 * var1 =~ var2
4367 * var1 != var2
4368 * var1 !~ var2
4369 * var1 > var2
4370 * var1 >= var2
4371 * var1 < var2
4372 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004373 * var1 is var2
4374 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004382eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 char_u *p;
4386 int i;
4387 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int len = 2;
4390 long n1, n2;
4391 char_u *s1, *s2;
4392 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4393 regmatch_T regmatch;
4394 int ic;
4395 char_u *save_cpo;
4396
4397 /*
4398 * Get the first variable.
4399 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 return FAIL;
4402
4403 p = *arg;
4404 switch (p[0])
4405 {
4406 case '=': if (p[1] == '=')
4407 type = TYPE_EQUAL;
4408 else if (p[1] == '~')
4409 type = TYPE_MATCH;
4410 break;
4411 case '!': if (p[1] == '=')
4412 type = TYPE_NEQUAL;
4413 else if (p[1] == '~')
4414 type = TYPE_NOMATCH;
4415 break;
4416 case '>': if (p[1] != '=')
4417 {
4418 type = TYPE_GREATER;
4419 len = 1;
4420 }
4421 else
4422 type = TYPE_GEQUAL;
4423 break;
4424 case '<': if (p[1] != '=')
4425 {
4426 type = TYPE_SMALLER;
4427 len = 1;
4428 }
4429 else
4430 type = TYPE_SEQUAL;
4431 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 case 'i': if (p[1] == 's')
4433 {
4434 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4435 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004436 i = p[len];
4437 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004438 {
4439 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4440 type_is = TRUE;
4441 }
4442 }
4443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
4446 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004447 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
4449 if (type != TYPE_UNKNOWN)
4450 {
4451 /* extra question mark appended: ignore case */
4452 if (p[len] == '?')
4453 {
4454 ic = TRUE;
4455 ++len;
4456 }
4457 /* extra '#' appended: match case */
4458 else if (p[len] == '#')
4459 {
4460 ic = FALSE;
4461 ++len;
4462 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 else
4465 ic = p_ic;
4466
4467 /*
4468 * Get the second variable.
4469 */
4470 *arg = skipwhite(p + len);
4471 if (eval5(arg, &var2, evaluate) == FAIL)
4472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 return FAIL;
4475 }
4476
4477 if (evaluate)
4478 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 if (type_is && rettv->v_type != var2.v_type)
4480 {
4481 /* For "is" a different type always means FALSE, for "notis"
4482 * it means TRUE. */
4483 n1 = (type == TYPE_NEQUAL);
4484 }
4485 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4486 {
4487 if (type_is)
4488 {
4489 n1 = (rettv->v_type == var2.v_type
4490 && rettv->vval.v_list == var2.vval.v_list);
4491 if (type == TYPE_NEQUAL)
4492 n1 = !n1;
4493 }
4494 else if (rettv->v_type != var2.v_type
4495 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4496 {
4497 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004498 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004500 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 clear_tv(rettv);
4502 clear_tv(&var2);
4503 return FAIL;
4504 }
4505 else
4506 {
4507 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004508 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4509 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004510 if (type == TYPE_NEQUAL)
4511 n1 = !n1;
4512 }
4513 }
4514
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004515 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4516 {
4517 if (type_is)
4518 {
4519 n1 = (rettv->v_type == var2.v_type
4520 && rettv->vval.v_dict == var2.vval.v_dict);
4521 if (type == TYPE_NEQUAL)
4522 n1 = !n1;
4523 }
4524 else if (rettv->v_type != var2.v_type
4525 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4526 {
4527 if (rettv->v_type != var2.v_type)
4528 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4529 else
4530 EMSG(_("E736: Invalid operation for Dictionary"));
4531 clear_tv(rettv);
4532 clear_tv(&var2);
4533 return FAIL;
4534 }
4535 else
4536 {
4537 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004538 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4539 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004540 if (type == TYPE_NEQUAL)
4541 n1 = !n1;
4542 }
4543 }
4544
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004545 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4546 {
4547 if (rettv->v_type != var2.v_type
4548 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4549 {
4550 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004553 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 clear_tv(rettv);
4555 clear_tv(&var2);
4556 return FAIL;
4557 }
4558 else
4559 {
4560 /* Compare two Funcrefs for being equal or unequal. */
4561 if (rettv->vval.v_string == NULL
4562 || var2.vval.v_string == NULL)
4563 n1 = FALSE;
4564 else
4565 n1 = STRCMP(rettv->vval.v_string,
4566 var2.vval.v_string) == 0;
4567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
4569 }
4570 }
4571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004572#ifdef FEAT_FLOAT
4573 /*
4574 * If one of the two variables is a float, compare as a float.
4575 * When using "=~" or "!~", always compare as string.
4576 */
4577 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4578 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4579 {
4580 float_T f1, f2;
4581
4582 if (rettv->v_type == VAR_FLOAT)
4583 f1 = rettv->vval.v_float;
4584 else
4585 f1 = get_tv_number(rettv);
4586 if (var2.v_type == VAR_FLOAT)
4587 f2 = var2.vval.v_float;
4588 else
4589 f2 = get_tv_number(&var2);
4590 n1 = FALSE;
4591 switch (type)
4592 {
4593 case TYPE_EQUAL: n1 = (f1 == f2); break;
4594 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4595 case TYPE_GREATER: n1 = (f1 > f2); break;
4596 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4597 case TYPE_SMALLER: n1 = (f1 < f2); break;
4598 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4599 case TYPE_UNKNOWN:
4600 case TYPE_MATCH:
4601 case TYPE_NOMATCH: break; /* avoid gcc warning */
4602 }
4603 }
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 /*
4607 * If one of the two variables is a number, compare as a number.
4608 * When using "=~" or "!~", always compare as string.
4609 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004610 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004613 n1 = get_tv_number(rettv);
4614 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 switch (type)
4616 {
4617 case TYPE_EQUAL: n1 = (n1 == n2); break;
4618 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4619 case TYPE_GREATER: n1 = (n1 > n2); break;
4620 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4621 case TYPE_SMALLER: n1 = (n1 < n2); break;
4622 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4623 case TYPE_UNKNOWN:
4624 case TYPE_MATCH:
4625 case TYPE_NOMATCH: break; /* avoid gcc warning */
4626 }
4627 }
4628 else
4629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 s1 = get_tv_string_buf(rettv, buf1);
4631 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4633 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4634 else
4635 i = 0;
4636 n1 = FALSE;
4637 switch (type)
4638 {
4639 case TYPE_EQUAL: n1 = (i == 0); break;
4640 case TYPE_NEQUAL: n1 = (i != 0); break;
4641 case TYPE_GREATER: n1 = (i > 0); break;
4642 case TYPE_GEQUAL: n1 = (i >= 0); break;
4643 case TYPE_SMALLER: n1 = (i < 0); break;
4644 case TYPE_SEQUAL: n1 = (i <= 0); break;
4645
4646 case TYPE_MATCH:
4647 case TYPE_NOMATCH:
4648 /* avoid 'l' flag in 'cpoptions' */
4649 save_cpo = p_cpo;
4650 p_cpo = (char_u *)"";
4651 regmatch.regprog = vim_regcomp(s2,
4652 RE_MAGIC + RE_STRING);
4653 regmatch.rm_ic = ic;
4654 if (regmatch.regprog != NULL)
4655 {
4656 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004657 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (type == TYPE_NOMATCH)
4659 n1 = !n1;
4660 }
4661 p_cpo = save_cpo;
4662 break;
4663
4664 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4665 }
4666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 clear_tv(rettv);
4668 clear_tv(&var2);
4669 rettv->v_type = VAR_NUMBER;
4670 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
4672 }
4673
4674 return OK;
4675}
4676
4677/*
4678 * Handle fourth level expression:
4679 * + number addition
4680 * - number subtraction
4681 * . string concatenation
4682 *
4683 * "arg" must point to the first non-white of the expression.
4684 * "arg" is advanced to the next non-white after the recognized expression.
4685 *
4686 * Return OK or FAIL.
4687 */
4688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004689eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 typval_T var2;
4692 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 int op;
4694 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004695#ifdef FEAT_FLOAT
4696 float_T f1 = 0, f2 = 0;
4697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 char_u *s1, *s2;
4699 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4700 char_u *p;
4701
4702 /*
4703 * Get the first variable.
4704 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004705 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return FAIL;
4707
4708 /*
4709 * Repeat computing, until no '+', '-' or '.' is following.
4710 */
4711 for (;;)
4712 {
4713 op = **arg;
4714 if (op != '+' && op != '-' && op != '.')
4715 break;
4716
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004717 if ((op != '+' || rettv->v_type != VAR_LIST)
4718#ifdef FEAT_FLOAT
4719 && (op == '.' || rettv->v_type != VAR_FLOAT)
4720#endif
4721 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 {
4723 /* For "list + ...", an illegal use of the first operand as
4724 * a number cannot be determined before evaluating the 2nd
4725 * operand: if this is also a list, all is ok.
4726 * For "something . ...", "something - ..." or "non-list + ...",
4727 * we know that the first operand needs to be a string or number
4728 * without evaluating the 2nd operand. So check before to avoid
4729 * side effects after an error. */
4730 if (evaluate && get_tv_string_chk(rettv) == NULL)
4731 {
4732 clear_tv(rettv);
4733 return FAIL;
4734 }
4735 }
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /*
4738 * Get the second variable.
4739 */
4740 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 return FAIL;
4745 }
4746
4747 if (evaluate)
4748 {
4749 /*
4750 * Compute the result.
4751 */
4752 if (op == '.')
4753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4755 s2 = get_tv_string_buf_chk(&var2, buf2);
4756 if (s2 == NULL) /* type error ? */
4757 {
4758 clear_tv(rettv);
4759 clear_tv(&var2);
4760 return FAIL;
4761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004762 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
4764 rettv->v_type = VAR_STRING;
4765 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004767 else if (op == '+' && rettv->v_type == VAR_LIST
4768 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004769 {
4770 /* concatenate Lists */
4771 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4772 &var3) == FAIL)
4773 {
4774 clear_tv(rettv);
4775 clear_tv(&var2);
4776 return FAIL;
4777 }
4778 clear_tv(rettv);
4779 *rettv = var3;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 int error = FALSE;
4784
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785#ifdef FEAT_FLOAT
4786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 f1 = rettv->vval.v_float;
4789 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 else
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 n1 = get_tv_number_chk(rettv, &error);
4795 if (error)
4796 {
4797 /* This can only happen for "list + non-list". For
4798 * "non-list + ..." or "something - ...", we returned
4799 * before evaluating the 2nd operand. */
4800 clear_tv(rettv);
4801 return FAIL;
4802 }
4803#ifdef FEAT_FLOAT
4804 if (var2.v_type == VAR_FLOAT)
4805 f1 = n1;
4806#endif
4807 }
4808#ifdef FEAT_FLOAT
4809 if (var2.v_type == VAR_FLOAT)
4810 {
4811 f2 = var2.vval.v_float;
4812 n2 = 0;
4813 }
4814 else
4815#endif
4816 {
4817 n2 = get_tv_number_chk(&var2, &error);
4818 if (error)
4819 {
4820 clear_tv(rettv);
4821 clear_tv(&var2);
4822 return FAIL;
4823 }
4824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 f2 = n2;
4827#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830
4831#ifdef FEAT_FLOAT
4832 /* If there is a float on either side the result is a float. */
4833 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4834 {
4835 if (op == '+')
4836 f1 = f1 + f2;
4837 else
4838 f1 = f1 - f2;
4839 rettv->v_type = VAR_FLOAT;
4840 rettv->vval.v_float = f1;
4841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843#endif
4844 {
4845 if (op == '+')
4846 n1 = n1 + n2;
4847 else
4848 n1 = n1 - n2;
4849 rettv->v_type = VAR_NUMBER;
4850 rettv->vval.v_number = n1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 }
4856 return OK;
4857}
4858
4859/*
4860 * Handle fifth level expression:
4861 * * number multiplication
4862 * / number division
4863 * % number modulo
4864 *
4865 * "arg" must point to the first non-white of the expression.
4866 * "arg" is advanced to the next non-white after the recognized expression.
4867 *
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004871eval6(
4872 char_u **arg,
4873 typval_T *rettv,
4874 int evaluate,
4875 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar33570922005-01-25 22:26:29 +00004877 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 int op;
4879 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#ifdef FEAT_FLOAT
4881 int use_float = FALSE;
4882 float_T f1 = 0, f2;
4883#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Get the first variable.
4888 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004889 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return FAIL;
4891
4892 /*
4893 * Repeat computing, until no '*', '/' or '%' is following.
4894 */
4895 for (;;)
4896 {
4897 op = **arg;
4898 if (op != '*' && op != '/' && op != '%')
4899 break;
4900
4901 if (evaluate)
4902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 if (rettv->v_type == VAR_FLOAT)
4905 {
4906 f1 = rettv->vval.v_float;
4907 use_float = TRUE;
4908 n1 = 0;
4909 }
4910 else
4911#endif
4912 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914 if (error)
4915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 n1 = 0;
4919
4920 /*
4921 * Get the second variable.
4922 */
4923 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004924 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (var2.v_type == VAR_FLOAT)
4931 {
4932 if (!use_float)
4933 {
4934 f1 = n1;
4935 use_float = TRUE;
4936 }
4937 f2 = var2.vval.v_float;
4938 n2 = 0;
4939 }
4940 else
4941#endif
4942 {
4943 n2 = get_tv_number_chk(&var2, &error);
4944 clear_tv(&var2);
4945 if (error)
4946 return FAIL;
4947#ifdef FEAT_FLOAT
4948 if (use_float)
4949 f2 = n2;
4950#endif
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 /*
4954 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 if (op == '*')
4961 f1 = f1 * f2;
4962 else if (op == '/')
4963 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004964# ifdef VMS
4965 /* VMS crashes on divide by zero, work around it */
4966 if (f2 == 0.0)
4967 {
4968 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 }
4975 else
4976 f1 = f1 / f2;
4977# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 /* We rely on the floating point library to handle divide
4979 * by zero to result in "inf" and not a crash. */
4980 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004985 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 return FAIL;
4987 }
4988 rettv->v_type = VAR_FLOAT;
4989 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 if (op == '*')
4995 n1 = n1 * n2;
4996 else if (op == '/')
4997 {
4998 if (n2 == 0) /* give an error message? */
4999 {
5000 if (n1 == 0)
5001 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5002 else if (n1 < 0)
5003 n1 = -0x7fffffffL;
5004 else
5005 n1 = 0x7fffffffL;
5006 }
5007 else
5008 n1 = n1 / n2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012 if (n2 == 0) /* give an error message? */
5013 n1 = 0;
5014 else
5015 n1 = n1 % n2;
5016 }
5017 rettv->v_type = VAR_NUMBER;
5018 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021 }
5022
5023 return OK;
5024}
5025
5026/*
5027 * Handle sixth level expression:
5028 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005029 * "string" string constant
5030 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * &option-name option value
5032 * @r register contents
5033 * identifier variable value
5034 * function() function call
5035 * $VAR environment variable
5036 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005037 * [expr, expr] List
5038 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *
5040 * Also handle:
5041 * ! in front logical NOT
5042 * - in front unary minus
5043 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 * trailing [] subscript in String or List
5045 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * "arg" must point to the first non-white of the expression.
5048 * "arg" is advanced to the next non-white after the recognized expression.
5049 *
5050 * Return OK or FAIL.
5051 */
5052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005053eval7(
5054 char_u **arg,
5055 typval_T *rettv,
5056 int evaluate,
5057 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 long n;
5060 int len;
5061 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *start_leader, *end_leader;
5063 int ret = OK;
5064 char_u *alias;
5065
5066 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005068 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005070 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 /*
5073 * Skip '!' and '-' characters. They are handled later.
5074 */
5075 start_leader = *arg;
5076 while (**arg == '!' || **arg == '-' || **arg == '+')
5077 *arg = skipwhite(*arg + 1);
5078 end_leader = *arg;
5079
5080 switch (**arg)
5081 {
5082 /*
5083 * Number constant.
5084 */
5085 case '0':
5086 case '1':
5087 case '2':
5088 case '3':
5089 case '4':
5090 case '5':
5091 case '6':
5092 case '7':
5093 case '8':
5094 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 {
5096#ifdef FEAT_FLOAT
5097 char_u *p = skipdigits(*arg + 1);
5098 int get_float = FALSE;
5099
5100 /* We accept a float when the format matches
5101 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005102 * strict to avoid backwards compatibility problems.
5103 * Don't look for a float after the "." operator, so that
5104 * ":let vers = 1.2.3" doesn't fail. */
5105 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005107 get_float = TRUE;
5108 p = skipdigits(p + 2);
5109 if (*p == 'e' || *p == 'E')
5110 {
5111 ++p;
5112 if (*p == '-' || *p == '+')
5113 ++p;
5114 if (!vim_isdigit(*p))
5115 get_float = FALSE;
5116 else
5117 p = skipdigits(p + 1);
5118 }
5119 if (ASCII_ISALPHA(*p) || *p == '.')
5120 get_float = FALSE;
5121 }
5122 if (get_float)
5123 {
5124 float_T f;
5125
5126 *arg += string2float(*arg, &f);
5127 if (evaluate)
5128 {
5129 rettv->v_type = VAR_FLOAT;
5130 rettv->vval.v_float = f;
5131 }
5132 }
5133 else
5134#endif
5135 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005136 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 *arg += len;
5138 if (evaluate)
5139 {
5140 rettv->v_type = VAR_NUMBER;
5141 rettv->vval.v_number = n;
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 /*
5148 * String constant: "string".
5149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 break;
5158
5159 /*
5160 * List: [expr, expr]
5161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
5164
5165 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 * Dictionary: {key: val, key: val}
5167 */
5168 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5169 break;
5170
5171 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 /*
5178 * Environment variable: $VAR.
5179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182
5183 /*
5184 * Register contents: @r.
5185 */
5186 case '@': ++*arg;
5187 if (evaluate)
5188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005190 rettv->vval.v_string = get_reg_contents(**arg,
5191 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (**arg != NUL)
5194 ++*arg;
5195 break;
5196
5197 /*
5198 * nested expression: (expression).
5199 */
5200 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (**arg == ')')
5203 ++*arg;
5204 else if (ret == OK)
5205 {
5206 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ret = FAIL;
5209 }
5210 break;
5211
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005215
5216 if (ret == NOTDONE)
5217 {
5218 /*
5219 * Must be a variable or function name.
5220 * Can also be a curly-braces kind of name: {expr}.
5221 */
5222 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (alias != NULL)
5225 s = alias;
5226
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 ret = FAIL;
5229 else
5230 {
5231 if (**arg == '(') /* recursive! */
5232 {
5233 /* If "s" is the name of a variable of type VAR_FUNC
5234 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005235 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /* Invoke the function. */
5238 ret = get_func_tv(s, len, rettv, arg,
5239 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005240 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005241
5242 /* If evaluate is FALSE rettv->v_type was not set in
5243 * get_func_tv, but it's needed in handle_subscript() to parse
5244 * what follows. So set it here. */
5245 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5246 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005247 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248 rettv->v_type = VAR_FUNC;
5249 }
5250
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /* Stop the expression evaluation when immediately
5252 * aborting on error, or when an interrupt occurred or
5253 * an exception was thrown but not caught. */
5254 if (aborting())
5255 {
5256 if (ret == OK)
5257 clear_tv(rettv);
5258 ret = FAIL;
5259 }
5260 }
5261 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005262 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005263 else
5264 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005266 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 *arg = skipwhite(*arg);
5270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5272 * expr(expr). */
5273 if (ret == OK)
5274 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /*
5277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5278 */
5279 if (ret == OK && evaluate && end_leader > start_leader)
5280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 int val = 0;
5283#ifdef FEAT_FLOAT
5284 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005286 if (rettv->v_type == VAR_FLOAT)
5287 f = rettv->vval.v_float;
5288 else
5289#endif
5290 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 clear_tv(rettv);
5294 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else
5297 {
5298 while (end_leader > start_leader)
5299 {
5300 --end_leader;
5301 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 {
5303#ifdef FEAT_FLOAT
5304 if (rettv->v_type == VAR_FLOAT)
5305 f = !f;
5306 else
5307#endif
5308 val = !val;
5309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 {
5312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 f = -f;
5315 else
5316#endif
5317 val = -val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 {
5323 clear_tv(rettv);
5324 rettv->vval.v_float = f;
5325 }
5326 else
5327#endif
5328 {
5329 clear_tv(rettv);
5330 rettv->v_type = VAR_NUMBER;
5331 rettv->vval.v_number = val;
5332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
5336 return ret;
5337}
5338
5339/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005340 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5341 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5343 */
5344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005345eval_index(
5346 char_u **arg,
5347 typval_T *rettv,
5348 int evaluate,
5349 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350{
5351 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 long len = -1;
5355 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 case VAR_FUNC:
5362 if (verbose)
5363 EMSG(_("E695: Cannot index a Funcref"));
5364 return FAIL;
5365 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005367 if (verbose)
5368 EMSG(_(e_float_as_string));
5369 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005370#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005371 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005372 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005373 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 if (verbose)
5375 EMSG(_("E909: Cannot index a special variable"));
5376 return FAIL;
5377 case VAR_UNKNOWN:
5378 if (evaluate)
5379 return FAIL;
5380 /* FALLTHROUGH */
5381
5382 case VAR_STRING:
5383 case VAR_NUMBER:
5384 case VAR_LIST:
5385 case VAR_DICT:
5386 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005387 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005389 init_tv(&var1);
5390 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 /*
5394 * dict.name
5395 */
5396 key = *arg + 1;
5397 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5398 ;
5399 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 }
5403 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 /*
5406 * something[idx]
5407 *
5408 * Get the (first) variable from inside the [].
5409 */
5410 *arg = skipwhite(*arg + 1);
5411 if (**arg == ':')
5412 empty1 = TRUE;
5413 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5414 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005415 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5416 {
5417 /* not a number or string */
5418 clear_tv(&var1);
5419 return FAIL;
5420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421
5422 /*
5423 * Get the second variable from inside the [:].
5424 */
5425 if (**arg == ':')
5426 {
5427 range = TRUE;
5428 *arg = skipwhite(*arg + 1);
5429 if (**arg == ']')
5430 empty2 = TRUE;
5431 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005433 if (!empty1)
5434 clear_tv(&var1);
5435 return FAIL;
5436 }
5437 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5438 {
5439 /* not a number or string */
5440 if (!empty1)
5441 clear_tv(&var1);
5442 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005443 return FAIL;
5444 }
5445 }
5446
5447 /* Check for the ']'. */
5448 if (**arg != ']')
5449 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005450 if (verbose)
5451 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 clear_tv(&var1);
5453 if (range)
5454 clear_tv(&var2);
5455 return FAIL;
5456 }
5457 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459
5460 if (evaluate)
5461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005462 n1 = 0;
5463 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 n1 = get_tv_number(&var1);
5466 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 if (range)
5469 {
5470 if (empty2)
5471 n2 = -1;
5472 else
5473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 n2 = get_tv_number(&var2);
5475 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
5477 }
5478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005481 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005482 case VAR_FUNC:
5483 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005484 case VAR_SPECIAL:
5485 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005486 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005487 break; /* not evaluating, skipping over subscript */
5488
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 case VAR_NUMBER:
5490 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005491 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 len = (long)STRLEN(s);
5493 if (range)
5494 {
5495 /* The resulting variable is a substring. If the indexes
5496 * are out of range the result is empty. */
5497 if (n1 < 0)
5498 {
5499 n1 = len + n1;
5500 if (n1 < 0)
5501 n1 = 0;
5502 }
5503 if (n2 < 0)
5504 n2 = len + n2;
5505 else if (n2 >= len)
5506 n2 = len;
5507 if (n1 >= len || n2 < 0 || n1 > n2)
5508 s = NULL;
5509 else
5510 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5511 }
5512 else
5513 {
5514 /* The resulting variable is a string of a single
5515 * character. If the index is too big or negative the
5516 * result is empty. */
5517 if (n1 >= len || n1 < 0)
5518 s = NULL;
5519 else
5520 s = vim_strnsave(s + n1, 1);
5521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 clear_tv(rettv);
5523 rettv->v_type = VAR_STRING;
5524 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 break;
5526
5527 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 if (n1 < 0)
5530 n1 = len + n1;
5531 if (!empty1 && (n1 < 0 || n1 >= len))
5532 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005533 /* For a range we allow invalid values and return an empty
5534 * list. A list index out of range is an error. */
5535 if (!range)
5536 {
5537 if (verbose)
5538 EMSGN(_(e_listidx), n1);
5539 return FAIL;
5540 }
5541 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 }
5543 if (range)
5544 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 list_T *l;
5546 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547
5548 if (n2 < 0)
5549 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005550 else if (n2 >= len)
5551 n2 = len - 1;
5552 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005553 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 l = list_alloc();
5555 if (l == NULL)
5556 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 n1 <= n2; ++n1)
5559 {
5560 if (list_append_tv(l, &item->li_tv) == FAIL)
5561 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005562 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 return FAIL;
5564 }
5565 item = item->li_next;
5566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 clear_tv(rettv);
5568 rettv->v_type = VAR_LIST;
5569 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005570 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 else
5573 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005574 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 clear_tv(rettv);
5576 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
5578 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579
5580 case VAR_DICT:
5581 if (range)
5582 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005583 if (verbose)
5584 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585 if (len == -1)
5586 clear_tv(&var1);
5587 return FAIL;
5588 }
5589 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005590 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005591
5592 if (len == -1)
5593 {
5594 key = get_tv_string(&var1);
5595 if (*key == NUL)
5596 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005597 if (verbose)
5598 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 clear_tv(&var1);
5600 return FAIL;
5601 }
5602 }
5603
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005604 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005606 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005607 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 if (len == -1)
5609 clear_tv(&var1);
5610 if (item == NULL)
5611 return FAIL;
5612
5613 copy_tv(&item->di_tv, &var1);
5614 clear_tv(rettv);
5615 *rettv = var1;
5616 }
5617 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 }
5619 }
5620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 return OK;
5622}
5623
5624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 * Get an option value.
5626 * "arg" points to the '&' or '+' before the option name.
5627 * "arg" is advanced to character after the option name.
5628 * Return OK or FAIL.
5629 */
5630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005631get_option_tv(
5632 char_u **arg,
5633 typval_T *rettv, /* when NULL, only check if option exists */
5634 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 char_u *option_end;
5637 long numval;
5638 char_u *stringval;
5639 int opt_type;
5640 int c;
5641 int working = (**arg == '+'); /* has("+option") */
5642 int ret = OK;
5643 int opt_flags;
5644
5645 /*
5646 * Isolate the option name and find its value.
5647 */
5648 option_end = find_option_end(arg, &opt_flags);
5649 if (option_end == NULL)
5650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 EMSG2(_("E112: Option name missing: %s"), *arg);
5653 return FAIL;
5654 }
5655
5656 if (!evaluate)
5657 {
5658 *arg = option_end;
5659 return OK;
5660 }
5661
5662 c = *option_end;
5663 *option_end = NUL;
5664 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 if (opt_type == -3) /* invalid name */
5668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 EMSG2(_("E113: Unknown option: %s"), *arg);
5671 ret = FAIL;
5672 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
5675 if (opt_type == -2) /* hidden string option */
5676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 rettv->v_type = VAR_STRING;
5678 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else if (opt_type == -1) /* hidden number option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_NUMBER;
5683 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else if (opt_type == 1) /* number option */
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->v_type = VAR_NUMBER;
5688 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else /* string option */
5691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv->v_type = VAR_STRING;
5693 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 }
5696 else if (working && (opt_type == -2 || opt_type == -1))
5697 ret = FAIL;
5698
5699 *option_end = c; /* put back for error messages */
5700 *arg = option_end;
5701
5702 return ret;
5703}
5704
5705/*
5706 * Allocate a variable for a string constant.
5707 * Return OK or FAIL.
5708 */
5709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005710get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 char_u *p;
5713 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 int extra = 0;
5715
5716 /*
5717 * Find the end of the string, skipping backslashed characters.
5718 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005719 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 if (*p == '\\' && p[1] != NUL)
5722 {
5723 ++p;
5724 /* A "\<x>" form occupies at least 4 characters, and produces up
5725 * to 6 characters: reserve space for 2 extra */
5726 if (*p == '<')
5727 extra += 2;
5728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730
5731 if (*p != '"')
5732 {
5733 EMSG2(_("E114: Missing quote: %s"), *arg);
5734 return FAIL;
5735 }
5736
5737 /* If only parsing, set *arg and return here */
5738 if (!evaluate)
5739 {
5740 *arg = p + 1;
5741 return OK;
5742 }
5743
5744 /*
5745 * Copy the string into allocated memory, handling backslashed
5746 * characters.
5747 */
5748 name = alloc((unsigned)(p - *arg + extra));
5749 if (name == NULL)
5750 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 rettv->v_type = VAR_STRING;
5752 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 if (*p == '\\')
5757 {
5758 switch (*++p)
5759 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 case 'b': *name++ = BS; ++p; break;
5761 case 'e': *name++ = ESC; ++p; break;
5762 case 'f': *name++ = FF; ++p; break;
5763 case 'n': *name++ = NL; ++p; break;
5764 case 'r': *name++ = CAR; ++p; break;
5765 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 case 'X': /* hex: "\x1", "\x12" */
5768 case 'x':
5769 case 'u': /* Unicode: "\u0023" */
5770 case 'U':
5771 if (vim_isxdigit(p[1]))
5772 {
5773 int n, nr;
5774 int c = toupper(*p);
5775
5776 if (c == 'X')
5777 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else
5781 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 nr = 0;
5783 while (--n >= 0 && vim_isxdigit(p[1]))
5784 {
5785 ++p;
5786 nr = (nr << 4) + hex2nr(*p);
5787 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#ifdef FEAT_MBYTE
5790 /* For "\u" store the number according to
5791 * 'encoding'. */
5792 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 else
5795#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 break;
5799
5800 /* octal: "\1", "\12", "\123" */
5801 case '0':
5802 case '1':
5803 case '2':
5804 case '3':
5805 case '4':
5806 case '5':
5807 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 case '7': *name = *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *name = (*name << 3) + *p++ - '0';
5812 if (*p >= '0' && *p <= '7')
5813 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 break;
5817
5818 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (extra != 0)
5821 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 }
5825 /* FALLTHROUGH */
5826
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 break;
5829 }
5830 }
5831 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 *arg = p + 1;
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return OK;
5839}
5840
5841/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 * Return OK or FAIL.
5844 */
5845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 int reduce = 0;
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 break;
5861 ++reduce;
5862 ++p;
5863 }
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 return FAIL;
5870 }
5871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 if (!evaluate)
5874 {
5875 *arg = p + 1;
5876 return OK;
5877 }
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 str = alloc((unsigned)((p - *arg) - reduce));
5883 if (str == NULL)
5884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++p;
5895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 *arg = p + 1;
5900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Allocate a variable for a List and fill it from "*arg".
5906 * Return OK or FAIL.
5907 */
5908 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910{
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 list_T *l = NULL;
5912 typval_T tv;
5913 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914
5915 if (evaluate)
5916 {
5917 l = list_alloc();
5918 if (l == NULL)
5919 return FAIL;
5920 }
5921
5922 *arg = skipwhite(*arg + 1);
5923 while (**arg != ']' && **arg != NUL)
5924 {
5925 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5926 goto failret;
5927 if (evaluate)
5928 {
5929 item = listitem_alloc();
5930 if (item != NULL)
5931 {
5932 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005933 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 list_append(l, item);
5935 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005936 else
5937 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938 }
5939
5940 if (**arg == ']')
5941 break;
5942 if (**arg != ',')
5943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 goto failret;
5946 }
5947 *arg = skipwhite(*arg + 1);
5948 }
5949
5950 if (**arg != ']')
5951 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953failret:
5954 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956 return FAIL;
5957 }
5958
5959 *arg = skipwhite(*arg + 1);
5960 if (evaluate)
5961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 rettv->v_type = VAR_LIST;
5963 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 ++l->lv_refcount;
5965 }
5966
5967 return OK;
5968}
5969
5970/*
5971 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005972 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005974 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005977 list_T *l;
5978
5979 l = (list_T *)alloc_clear(sizeof(list_T));
5980 if (l != NULL)
5981 {
5982 /* Prepend the list to the list of lists for garbage collection. */
5983 if (first_list != NULL)
5984 first_list->lv_used_prev = l;
5985 l->lv_used_prev = NULL;
5986 l->lv_used_next = first_list;
5987 first_list = l;
5988 }
5989 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005993 * Allocate an empty list for a return value.
5994 * Returns OK or FAIL.
5995 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005998{
5999 list_T *l = list_alloc();
6000
6001 if (l == NULL)
6002 return FAIL;
6003
6004 rettv->vval.v_list = l;
6005 rettv->v_type = VAR_LIST;
6006 ++l->lv_refcount;
6007 return OK;
6008}
6009
6010/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 * Unreference a list: decrement the reference count and free it when it
6012 * becomes zero.
6013 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006015list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006017 if (l != NULL && --l->lv_refcount <= 0)
6018 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019}
6020
6021/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006022 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 * Ignores the reference count.
6024 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006026list_free(
6027 list_T *l,
6028 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029{
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006032 /* Remove the list from the list of lists for garbage collection. */
6033 if (l->lv_used_prev == NULL)
6034 first_list = l->lv_used_next;
6035 else
6036 l->lv_used_prev->lv_used_next = l->lv_used_next;
6037 if (l->lv_used_next != NULL)
6038 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6039
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 /* Remove the item before deleting it. */
6043 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006044 if (recurse || (item->li_tv.v_type != VAR_LIST
6045 && item->li_tv.v_type != VAR_DICT))
6046 clear_tv(&item->li_tv);
6047 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 }
6049 vim_free(l);
6050}
6051
6052/*
6053 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006054 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006056 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006057listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058{
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060}
6061
6062/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006063 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006065 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006068 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 vim_free(item);
6070}
6071
6072/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073 * Remove a list item from a List and free it. Also clears the value.
6074 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006078 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079 listitem_free(item);
6080}
6081
6082/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 * Get the number of items in a list.
6084 */
6085 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006086list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 if (l == NULL)
6089 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006090 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091}
6092
6093/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094 * Return TRUE when two lists have exactly the same values.
6095 */
6096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097list_equal(
6098 list_T *l1,
6099 list_T *l2,
6100 int ic, /* ignore case for strings */
6101 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102{
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006105 if (l1 == NULL || l2 == NULL)
6106 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006107 if (l1 == l2)
6108 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006109 if (list_len(l1) != list_len(l2))
6110 return FALSE;
6111
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 for (item1 = l1->lv_first, item2 = l2->lv_first;
6113 item1 != NULL && item2 != NULL;
6114 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116 return FALSE;
6117 return item1 == NULL && item2 == NULL;
6118}
6119
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006120/*
6121 * Return the dictitem that an entry in a hashtable points to.
6122 */
6123 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125{
6126 return HI2DI(hi);
6127}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006128
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 * Return TRUE when two dictionaries have exactly the same key/values.
6131 */
6132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006133dict_equal(
6134 dict_T *d1,
6135 dict_T *d2,
6136 int ic, /* ignore case for strings */
6137 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138{
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 hashitem_T *hi;
6140 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006143 if (d1 == NULL || d2 == NULL)
6144 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006145 if (d1 == d2)
6146 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147 if (dict_len(d1) != dict_len(d2))
6148 return FALSE;
6149
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006150 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006153 if (!HASHITEM_EMPTY(hi))
6154 {
6155 item2 = dict_find(d2, hi->hi_key, -1);
6156 if (item2 == NULL)
6157 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006158 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 return FALSE;
6160 --todo;
6161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162 }
6163 return TRUE;
6164}
6165
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166static int tv_equal_recurse_limit;
6167
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006168/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006169 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006170 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006171 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172 */
6173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006174tv_equal(
6175 typval_T *tv1,
6176 typval_T *tv2,
6177 int ic, /* ignore case */
6178 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006179{
6180 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006181 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006182 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006183 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006185 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006186 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 * recursiveness to a limit. We guess they are equal then.
6190 * A fixed limit has the problem of still taking an awful long time.
6191 * Reduce the limit every time running into it. That should work fine for
6192 * deeply linked structures that are not recursively linked and catch
6193 * recursiveness quickly. */
6194 if (!recursive)
6195 tv_equal_recurse_limit = 1000;
6196 if (recursive_cnt >= tv_equal_recurse_limit)
6197 {
6198 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006199 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201
6202 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 ++recursive_cnt;
6206 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6207 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006209
6210 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006211 ++recursive_cnt;
6212 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6213 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006214 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215
6216 case VAR_FUNC:
6217 return (tv1->vval.v_string != NULL
6218 && tv2->vval.v_string != NULL
6219 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6220
6221 case VAR_NUMBER:
6222 return tv1->vval.v_number == tv2->vval.v_number;
6223
6224 case VAR_STRING:
6225 s1 = get_tv_string_buf(tv1, buf1);
6226 s2 = get_tv_string_buf(tv2, buf2);
6227 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006228
6229 case VAR_SPECIAL:
6230 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006231
6232 case VAR_FLOAT:
6233#ifdef FEAT_FLOAT
6234 return tv1->vval.v_float == tv2->vval.v_float;
6235#endif
6236 case VAR_JOB:
6237#ifdef FEAT_JOB
6238 return tv1->vval.v_job == tv2->vval.v_job;
6239#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006240 case VAR_CHANNEL:
6241#ifdef FEAT_CHANNEL
6242 return tv1->vval.v_channel == tv2->vval.v_channel;
6243#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006244 case VAR_UNKNOWN:
6245 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
Bram Moolenaara03f2332016-02-06 18:09:59 +01006248 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6249 * does not equal anything, not even itself. */
6250 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006251}
6252
6253/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006254 * Locate item with index "n" in list "l" and return it.
6255 * A negative index is counted from the end; -1 is the last item.
6256 * Returns NULL when "n" is out of range.
6257 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006258 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006259list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260{
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 long idx;
6263
6264 if (l == NULL)
6265 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006266
6267 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006268 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006269 n = l->lv_len + n;
6270
6271 /* Check for index out of range. */
6272 if (n < 0 || n >= l->lv_len)
6273 return NULL;
6274
6275 /* When there is a cached index may start search from there. */
6276 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 if (n < l->lv_idx / 2)
6279 {
6280 /* closest to the start of the list */
6281 item = l->lv_first;
6282 idx = 0;
6283 }
6284 else if (n > (l->lv_idx + l->lv_len) / 2)
6285 {
6286 /* closest to the end of the list */
6287 item = l->lv_last;
6288 idx = l->lv_len - 1;
6289 }
6290 else
6291 {
6292 /* closest to the cached index */
6293 item = l->lv_idx_item;
6294 idx = l->lv_idx;
6295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 }
6297 else
6298 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006299 if (n < l->lv_len / 2)
6300 {
6301 /* closest to the start of the list */
6302 item = l->lv_first;
6303 idx = 0;
6304 }
6305 else
6306 {
6307 /* closest to the end of the list */
6308 item = l->lv_last;
6309 idx = l->lv_len - 1;
6310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312
6313 while (n > idx)
6314 {
6315 /* search forward */
6316 item = item->li_next;
6317 ++idx;
6318 }
6319 while (n < idx)
6320 {
6321 /* search backward */
6322 item = item->li_prev;
6323 --idx;
6324 }
6325
6326 /* cache the used index */
6327 l->lv_idx = idx;
6328 l->lv_idx_item = item;
6329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 return item;
6331}
6332
6333/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006334 * Get list item "l[idx]" as a number.
6335 */
6336 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006337list_find_nr(
6338 list_T *l,
6339 long idx,
6340 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006341{
6342 listitem_T *li;
6343
6344 li = list_find(l, idx);
6345 if (li == NULL)
6346 {
6347 if (errorp != NULL)
6348 *errorp = TRUE;
6349 return -1L;
6350 }
6351 return get_tv_number_chk(&li->li_tv, errorp);
6352}
6353
6354/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006355 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6356 */
6357 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006358list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006359{
6360 listitem_T *li;
6361
6362 li = list_find(l, idx - 1);
6363 if (li == NULL)
6364 {
6365 EMSGN(_(e_listidx), idx);
6366 return NULL;
6367 }
6368 return get_tv_string(&li->li_tv);
6369}
6370
6371/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006372 * Locate "item" list "l" and return its index.
6373 * Returns -1 when "item" is not in the list.
6374 */
6375 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006376list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006377{
6378 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006379 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006380
6381 if (l == NULL)
6382 return -1;
6383 idx = 0;
6384 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6385 ++idx;
6386 if (li == NULL)
6387 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006388 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006389}
6390
6391/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 * Append item "item" to the end of list "l".
6393 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006395list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396{
6397 if (l->lv_last == NULL)
6398 {
6399 /* empty list */
6400 l->lv_first = item;
6401 l->lv_last = item;
6402 item->li_prev = NULL;
6403 }
6404 else
6405 {
6406 l->lv_last->li_next = item;
6407 item->li_prev = l->lv_last;
6408 l->lv_last = item;
6409 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006410 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 item->li_next = NULL;
6412}
6413
6414/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006415 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006416 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006419list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 copy_tv(tv, &li->li_tv);
6426 list_append(l, li);
6427 return OK;
6428}
6429
6430/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006431 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 * Return FAIL when out of memory.
6433 */
6434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006435list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436{
6437 listitem_T *li = listitem_alloc();
6438
6439 if (li == NULL)
6440 return FAIL;
6441 li->li_tv.v_type = VAR_DICT;
6442 li->li_tv.v_lock = 0;
6443 li->li_tv.vval.v_dict = dict;
6444 list_append(list, li);
6445 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 return OK;
6447}
6448
6449/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006451 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Returns FAIL when out of memory.
6453 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006455list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006456{
6457 listitem_T *li = listitem_alloc();
6458
6459 if (li == NULL)
6460 return FAIL;
6461 list_append(l, li);
6462 li->li_tv.v_type = VAR_STRING;
6463 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006464 if (str == NULL)
6465 li->li_tv.vval.v_string = NULL;
6466 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006467 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006468 return FAIL;
6469 return OK;
6470}
6471
6472/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006473 * Append "n" to list "l".
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006477list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006478{
6479 listitem_T *li;
6480
6481 li = listitem_alloc();
6482 if (li == NULL)
6483 return FAIL;
6484 li->li_tv.v_type = VAR_NUMBER;
6485 li->li_tv.v_lock = 0;
6486 li->li_tv.vval.v_number = n;
6487 list_append(l, li);
6488 return OK;
6489}
6490
6491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006492 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 * If "item" is NULL append at the end.
6494 * Return FAIL when out of memory.
6495 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006497list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498{
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500
6501 if (ni == NULL)
6502 return FAIL;
6503 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006504 list_insert(l, ni, item);
6505 return OK;
6506}
6507
6508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006510{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 if (item == NULL)
6512 /* Append new item at end of list. */
6513 list_append(l, ni);
6514 else
6515 {
6516 /* Insert new item before existing item. */
6517 ni->li_prev = item->li_prev;
6518 ni->li_next = item;
6519 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006521 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 ++l->lv_idx;
6523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 l->lv_idx_item = NULL;
6528 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006530 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532}
6533
6534/*
6535 * Extend "l1" with "l2".
6536 * If "bef" is NULL append at the end, otherwise insert before this item.
6537 * Returns FAIL when out of memory.
6538 */
6539 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541{
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006543 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006545 /* We also quit the loop when we have inserted the original item count of
6546 * the list, avoid a hang when we extend a list with itself. */
6547 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6549 return FAIL;
6550 return OK;
6551}
6552
6553/*
6554 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6555 * Return FAIL when out of memory.
6556 */
6557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559{
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006562 if (l1 == NULL || l2 == NULL)
6563 return FAIL;
6564
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006566 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 if (l == NULL)
6568 return FAIL;
6569 tv->v_type = VAR_LIST;
6570 tv->vval.v_list = l;
6571
6572 /* append all items from the second list */
6573 return list_extend(l, l2, NULL);
6574}
6575
6576/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 * Returns NULL when out of memory.
6581 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006582 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *copy;
6586 listitem_T *item;
6587 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 if (orig == NULL)
6590 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591
6592 copy = list_alloc();
6593 if (copy != NULL)
6594 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 if (copyID != 0)
6596 {
6597 /* Do this before adding the items, because one of the items may
6598 * refer back to this list. */
6599 orig->lv_copyID = copyID;
6600 orig->lv_copylist = copy;
6601 }
6602 for (item = orig->lv_first; item != NULL && !got_int;
6603 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604 {
6605 ni = listitem_alloc();
6606 if (ni == NULL)
6607 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006609 {
6610 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6611 {
6612 vim_free(ni);
6613 break;
6614 }
6615 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006617 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 list_append(copy, ni);
6619 }
6620 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 if (item != NULL)
6622 {
6623 list_unref(copy);
6624 copy = NULL;
6625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 }
6627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 return copy;
6629}
6630
6631/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006633 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006634 * This used to be called list_remove, but that conflicts with a Sun header
6635 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006638vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639{
Bram Moolenaar33570922005-01-25 22:26:29 +00006640 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 /* notify watchers */
6643 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006645 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646 list_fix_watch(l, ip);
6647 if (ip == item2)
6648 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650
6651 if (item2->li_next == NULL)
6652 l->lv_last = item->li_prev;
6653 else
6654 item2->li_next->li_prev = item->li_prev;
6655 if (item->li_prev == NULL)
6656 l->lv_first = item2->li_next;
6657 else
6658 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006659 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660}
6661
6662/*
6663 * Return an allocated string with the string representation of a list.
6664 * May return NULL.
6665 */
6666 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006667list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668{
6669 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670
6671 if (tv->vval.v_list == NULL)
6672 return NULL;
6673 ga_init2(&ga, (int)sizeof(char), 80);
6674 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006675 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006676 {
6677 vim_free(ga.ga_data);
6678 return NULL;
6679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 ga_append(&ga, ']');
6681 ga_append(&ga, NUL);
6682 return (char_u *)ga.ga_data;
6683}
6684
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006685typedef struct join_S {
6686 char_u *s;
6687 char_u *tofree;
6688} join_T;
6689
6690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006691list_join_inner(
6692 garray_T *gap, /* to store the result in */
6693 list_T *l,
6694 char_u *sep,
6695 int echo_style,
6696 int copyID,
6697 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006698{
6699 int i;
6700 join_T *p;
6701 int len;
6702 int sumlen = 0;
6703 int first = TRUE;
6704 char_u *tofree;
6705 char_u numbuf[NUMBUFLEN];
6706 listitem_T *item;
6707 char_u *s;
6708
6709 /* Stringify each item in the list. */
6710 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6711 {
6712 if (echo_style)
6713 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6714 else
6715 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6716 if (s == NULL)
6717 return FAIL;
6718
6719 len = (int)STRLEN(s);
6720 sumlen += len;
6721
Bram Moolenaarcde88542015-08-11 19:14:00 +02006722 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6724 if (tofree != NULL || s != numbuf)
6725 {
6726 p->s = s;
6727 p->tofree = tofree;
6728 }
6729 else
6730 {
6731 p->s = vim_strnsave(s, len);
6732 p->tofree = p->s;
6733 }
6734
6735 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006736 if (did_echo_string_emsg) /* recursion error, bail out */
6737 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738 }
6739
6740 /* Allocate result buffer with its total size, avoid re-allocation and
6741 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6742 if (join_gap->ga_len >= 2)
6743 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6744 if (ga_grow(gap, sumlen + 2) == FAIL)
6745 return FAIL;
6746
6747 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6748 {
6749 if (first)
6750 first = FALSE;
6751 else
6752 ga_concat(gap, sep);
6753 p = ((join_T *)join_gap->ga_data) + i;
6754
6755 if (p->s != NULL)
6756 ga_concat(gap, p->s);
6757 line_breakcheck();
6758 }
6759
6760 return OK;
6761}
6762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006764 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006765 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006769list_join(
6770 garray_T *gap,
6771 list_T *l,
6772 char_u *sep,
6773 int echo_style,
6774 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776 garray_T join_ga;
6777 int retval;
6778 join_T *p;
6779 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780
Bram Moolenaard39a7512015-04-16 22:51:22 +02006781 if (l->lv_len < 1)
6782 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6784 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6785
6786 /* Dispose each item in join_ga. */
6787 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789 p = (join_T *)join_ga.ga_data;
6790 for (i = 0; i < join_ga.ga_len; ++i)
6791 {
6792 vim_free(p->tofree);
6793 ++p;
6794 }
6795 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006796 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006797
6798 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799}
6800
6801/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006802 * Return the next (unique) copy ID.
6803 * Used for serializing nested structures.
6804 */
6805 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006806get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006807{
6808 current_copyID += COPYID_INC;
6809 return current_copyID;
6810}
6811
6812/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813 * Garbage collection for lists and dictionaries.
6814 *
6815 * We use reference counts to be able to free most items right away when they
6816 * are no longer used. But for composite items it's possible that it becomes
6817 * unused while the reference count is > 0: When there is a recursive
6818 * reference. Example:
6819 * :let l = [1, 2, 3]
6820 * :let d = {9: l}
6821 * :let l[1] = d
6822 *
6823 * Since this is quite unusual we handle this with garbage collection: every
6824 * once in a while find out which lists and dicts are not referenced from any
6825 * variable.
6826 *
6827 * Here is a good reference text about garbage collection (refers to Python
6828 * but it applies to all reference-counting mechanisms):
6829 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831
6832/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 * Do garbage collection for lists and dicts.
6834 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006840 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 buf_T *buf;
6842 win_T *wp;
6843 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006844 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006845 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006846 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006847#ifdef FEAT_WINDOWS
6848 tabpage_T *tp;
6849#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006851 /* Only do this once. */
6852 want_garbage_collect = FALSE;
6853 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006854 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006855
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856 /* We advance by two because we add one for items referenced through
6857 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006858 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006860 /*
6861 * 1. Go through all accessible variables and mark all lists and dicts
6862 * with copyID.
6863 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864
6865 /* Don't free variables in the previous_funccal list unless they are only
6866 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006867 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6869 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006870 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6871 NULL);
6872 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6873 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 }
6875
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 /* script-local variables */
6877 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879
6880 /* buffer-local variables */
6881 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6883 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
6885 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006886 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006889#ifdef FEAT_AUTOCMD
6890 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6892 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006893#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006895#ifdef FEAT_WINDOWS
6896 /* tabpage-local variables */
6897 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900#endif
6901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
6905 /* function-local variables */
6906 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6907 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6909 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 }
6911
Bram Moolenaard812df62008-11-09 12:46:09 +00006912 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006914
Bram Moolenaar1dced572012-04-05 16:54:08 +02006915#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006917#endif
6918
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#endif
6922
6923#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006925#endif
6926
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006927#ifdef FEAT_CHANNEL
6928 abort = abort || set_ref_in_channel(copyID);
6929#endif
6930
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006932 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 /*
6934 * 2. Free lists and dictionaries that are not referenced.
6935 */
6936 did_free = free_unref_items(copyID);
6937
6938 /*
6939 * 3. Check if any funccal can be freed now.
6940 */
6941 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006942 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 if (can_free_funccal(*pfc, copyID))
6944 {
6945 fc = *pfc;
6946 *pfc = fc->caller;
6947 free_funccal(fc, TRUE);
6948 did_free = TRUE;
6949 did_free_funccal = TRUE;
6950 }
6951 else
6952 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 if (did_free_funccal)
6955 /* When a funccal was freed some more items might be garbage
6956 * collected, so run again. */
6957 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 else if (p_verbose > 0)
6960 {
6961 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6962 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963
6964 return did_free;
6965}
6966
6967/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006968 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006969 */
6970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006971free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006973 dict_T *dd, *dd_next;
6974 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 int did_free = FALSE;
6976
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 */
6980 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006981 {
6982 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006985 /* Free the Dictionary and ordinary items it contains, but don't
6986 * recurse into Lists and Dictionaries, they will be in the list
6987 * of dicts or list of lists. */
6988 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006991 dd = dd_next;
6992 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
6994 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995 * Go through the list of lists and free items without the copyID.
6996 * But don't free a list that has a watcher (used in a for loop), these
6997 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 */
6999 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 {
7001 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7003 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007005 /* Free the List and ordinary items it contains, but don't recurse
7006 * into Lists and Dictionaries, they will be in the list of dicts
7007 * or list of lists. */
7008 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007011 ll = ll_next;
7012 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 return did_free;
7015}
7016
7017/*
7018 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 * "list_stack" is used to add lists to be marked. Can be NULL.
7020 *
7021 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007024set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025{
7026 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 hashtab_T *cur_ht;
7030 ht_stack_T *ht_stack = NULL;
7031 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 cur_ht = ht;
7034 for (;;)
7035 {
7036 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 /* Mark each item in the hashtab. If the item contains a hashtab
7039 * it is added to ht_stack, if it contains a list it is added to
7040 * list_stack. */
7041 todo = (int)cur_ht->ht_used;
7042 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7043 if (!HASHITEM_EMPTY(hi))
7044 {
7045 --todo;
7046 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7047 &ht_stack, list_stack);
7048 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050
7051 if (ht_stack == NULL)
7052 break;
7053
7054 /* take an item from the stack */
7055 cur_ht = ht_stack->ht;
7056 tempitem = ht_stack;
7057 ht_stack = ht_stack->prev;
7058 free(tempitem);
7059 }
7060
7061 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062}
7063
7064/*
7065 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7067 *
7068 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007071set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 listitem_T *li;
7074 int abort = FALSE;
7075 list_T *cur_l;
7076 list_stack_T *list_stack = NULL;
7077 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 cur_l = l;
7080 for (;;)
7081 {
7082 if (!abort)
7083 /* Mark each item in the list. If the item contains a hashtab
7084 * it is added to ht_stack, if it contains a list it is added to
7085 * list_stack. */
7086 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7087 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7088 ht_stack, &list_stack);
7089 if (list_stack == NULL)
7090 break;
7091
7092 /* take an item from the stack */
7093 cur_l = list_stack->list;
7094 tempitem = list_stack;
7095 list_stack = list_stack->prev;
7096 free(tempitem);
7097 }
7098
7099 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100}
7101
7102/*
7103 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 * "list_stack" is used to add lists to be marked. Can be NULL.
7105 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7106 *
7107 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110set_ref_in_item(
7111 typval_T *tv,
7112 int copyID,
7113 ht_stack_T **ht_stack,
7114 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115{
7116 dict_T *dd;
7117 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119
Bram Moolenaara03f2332016-02-06 18:09:59 +01007120 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 dd = tv->vval.v_dict;
7123 if (dd != NULL && dd->dv_copyID != copyID)
7124 {
7125 /* Didn't see this dict yet. */
7126 dd->dv_copyID = copyID;
7127 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007129 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7130 }
7131 else
7132 {
7133 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7134 if (newitem == NULL)
7135 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 else
7137 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007138 newitem->ht = &dd->dv_hashtab;
7139 newitem->prev = *ht_stack;
7140 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007142 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 }
7144 }
7145 else if (tv->v_type == VAR_LIST)
7146 {
7147 ll = tv->vval.v_list;
7148 if (ll != NULL && ll->lv_copyID != copyID)
7149 {
7150 /* Didn't see this list yet. */
7151 ll->lv_copyID = copyID;
7152 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 abort = set_ref_in_list(ll, copyID, ht_stack);
7155 }
7156 else
7157 {
7158 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007160 if (newitem == NULL)
7161 abort = TRUE;
7162 else
7163 {
7164 newitem->list = ll;
7165 newitem->prev = *list_stack;
7166 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172}
7173
7174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 * Allocate an empty header for a dictionary.
7176 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007177 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179{
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007184 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007185 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186 if (first_dict != NULL)
7187 first_dict->dv_used_prev = d;
7188 d->dv_used_next = first_dict;
7189 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007190 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007194 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007196 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199}
7200
7201/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007202 * Allocate an empty dict for a return value.
7203 * Returns OK or FAIL.
7204 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007206rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007207{
7208 dict_T *d = dict_alloc();
7209
7210 if (d == NULL)
7211 return FAIL;
7212
7213 rettv->vval.v_dict = d;
7214 rettv->v_type = VAR_DICT;
7215 ++d->dv_refcount;
7216 return OK;
7217}
7218
7219
7220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 * Unreference a Dictionary: decrement the reference count and free it when it
7222 * becomes zero.
7223 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007227 if (d != NULL && --d->dv_refcount <= 0)
7228 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229}
7230
7231/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007232 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 * Ignores the reference count.
7234 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236dict_free(
7237 dict_T *d,
7238 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007240 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007242 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007244 /* Remove the dict from the list of dicts for garbage collection. */
7245 if (d->dv_used_prev == NULL)
7246 first_dict = d->dv_used_next;
7247 else
7248 d->dv_used_prev->dv_used_next = d->dv_used_next;
7249 if (d->dv_used_next != NULL)
7250 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7251
7252 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007253 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007254 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007257 if (!HASHITEM_EMPTY(hi))
7258 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007259 /* Remove the item before deleting it, just in case there is
7260 * something recursive causing trouble. */
7261 di = HI2DI(hi);
7262 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007263 if (recurse || (di->di_tv.v_type != VAR_LIST
7264 && di->di_tv.v_type != VAR_DICT))
7265 clear_tv(&di->di_tv);
7266 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 --todo;
7268 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 vim_free(d);
7272}
7273
7274/*
7275 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 * The "key" is copied to the new item.
7277 * Note that the value of the item "di_tv" still needs to be initialized!
7278 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007280 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007281dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007289 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007290 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Make a copy of a Dictionary item.
7296 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007298dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299{
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007302 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7303 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 if (di != NULL)
7305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007307 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 copy_tv(&org->di_tv, &di->di_tv);
7309 }
7310 return di;
7311}
7312
7313/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 * Remove item "item" from Dictionary "dict" and free it.
7315 */
7316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318{
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 if (HASHITEM_EMPTY(hi))
7323 EMSG2(_(e_intern2), "dictitem_remove()");
7324 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 dictitem_free(item);
7327}
7328
7329/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 * Free a dict item. Also clears the value.
7331 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007333dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007336 if (item->di_flags & DI_FLAGS_ALLOC)
7337 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338}
7339
7340/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007341 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7342 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007343 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 * Returns NULL when out of memory.
7345 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007347dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348{
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 dict_T *copy;
7350 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353
7354 if (orig == NULL)
7355 return NULL;
7356
7357 copy = dict_alloc();
7358 if (copy != NULL)
7359 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007360 if (copyID != 0)
7361 {
7362 orig->dv_copyID = copyID;
7363 orig->dv_copydict = copy;
7364 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007365 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007366 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 --todo;
7371
7372 di = dictitem_alloc(hi->hi_key);
7373 if (di == NULL)
7374 break;
7375 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007376 {
7377 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7378 copyID) == FAIL)
7379 {
7380 vim_free(di);
7381 break;
7382 }
7383 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384 else
7385 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7386 if (dict_add(copy, di) == FAIL)
7387 {
7388 dictitem_free(di);
7389 break;
7390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 if (todo > 0)
7396 {
7397 dict_unref(copy);
7398 copy = NULL;
7399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 }
7401
7402 return copy;
7403}
7404
7405/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007407 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007410dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411{
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413}
7414
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007416 * Add a number or string entry to dictionary "d".
7417 * When "str" is NULL use number "nr", otherwise use "str".
7418 * Returns FAIL when out of memory and when key already exists.
7419 */
7420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007421dict_add_nr_str(
7422 dict_T *d,
7423 char *key,
7424 long nr,
7425 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007426{
7427 dictitem_T *item;
7428
7429 item = dictitem_alloc((char_u *)key);
7430 if (item == NULL)
7431 return FAIL;
7432 item->di_tv.v_lock = 0;
7433 if (str == NULL)
7434 {
7435 item->di_tv.v_type = VAR_NUMBER;
7436 item->di_tv.vval.v_number = nr;
7437 }
7438 else
7439 {
7440 item->di_tv.v_type = VAR_STRING;
7441 item->di_tv.vval.v_string = vim_strsave(str);
7442 }
7443 if (dict_add(d, item) == FAIL)
7444 {
7445 dictitem_free(item);
7446 return FAIL;
7447 }
7448 return OK;
7449}
7450
7451/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007452 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007453 * Returns FAIL when out of memory and when key already exists.
7454 */
7455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007456dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007457{
7458 dictitem_T *item;
7459
7460 item = dictitem_alloc((char_u *)key);
7461 if (item == NULL)
7462 return FAIL;
7463 item->di_tv.v_lock = 0;
7464 item->di_tv.v_type = VAR_LIST;
7465 item->di_tv.vval.v_list = list;
7466 if (dict_add(d, item) == FAIL)
7467 {
7468 dictitem_free(item);
7469 return FAIL;
7470 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007471 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007472 return OK;
7473}
7474
7475/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 * Get the number of items in a Dictionary.
7477 */
7478 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481 if (d == NULL)
7482 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007483 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007484}
7485
7486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 * Find item "key[len]" in Dictionary "d".
7488 * If "len" is negative use strlen(key).
7489 * Returns NULL when not found.
7490 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007491 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007492dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494#define AKEYLEN 200
7495 char_u buf[AKEYLEN];
7496 char_u *akey;
7497 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 if (len < 0)
7501 akey = key;
7502 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007503 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 tofree = akey = vim_strnsave(key, len);
7505 if (akey == NULL)
7506 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007507 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 else
7509 {
7510 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007511 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 akey = buf;
7513 }
7514
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 vim_free(tofree);
7517 if (HASHITEM_EMPTY(hi))
7518 return NULL;
7519 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520}
7521
7522/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007523 * Get a string item from a dictionary.
7524 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007525 * Returns NULL if the entry doesn't exist or out of memory.
7526 */
7527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007528get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007529{
7530 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007531 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532
7533 di = dict_find(d, key, -1);
7534 if (di == NULL)
7535 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007536 s = get_tv_string(&di->di_tv);
7537 if (save && s != NULL)
7538 s = vim_strsave(s);
7539 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007540}
7541
7542/*
7543 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007544 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007545 */
7546 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007548{
7549 dictitem_T *di;
7550
7551 di = dict_find(d, key, -1);
7552 if (di == NULL)
7553 return 0;
7554 return get_tv_number(&di->di_tv);
7555}
7556
7557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 * Return an allocated string with the string representation of a Dictionary.
7559 * May return NULL.
7560 */
7561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007562dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563{
7564 garray_T ga;
7565 int first = TRUE;
7566 char_u *tofree;
7567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 return NULL;
7575 ga_init2(&ga, (int)sizeof(char), 80);
7576 ga_append(&ga, '{');
7577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007578 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007579 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 --todo;
7584
7585 if (first)
7586 first = FALSE;
7587 else
7588 ga_concat(&ga, (char_u *)", ");
7589
7590 tofree = string_quote(hi->hi_key, FALSE);
7591 if (tofree != NULL)
7592 {
7593 ga_concat(&ga, tofree);
7594 vim_free(tofree);
7595 }
7596 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 if (s != NULL)
7599 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007601 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007602 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007603 line_breakcheck();
7604
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007607 if (todo > 0)
7608 {
7609 vim_free(ga.ga_data);
7610 return NULL;
7611 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612
7613 ga_append(&ga, '}');
7614 ga_append(&ga, NUL);
7615 return (char_u *)ga.ga_data;
7616}
7617
7618/*
7619 * Allocate a variable for a Dictionary and fill it from "*arg".
7620 * Return OK or FAIL. Returns NOTDONE for {expr}.
7621 */
7622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624{
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 dict_T *d = NULL;
7626 typval_T tvkey;
7627 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007628 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632
7633 /*
7634 * First check if it's not a curly-braces thing: {expr}.
7635 * Must do this without evaluating, otherwise a function may be called
7636 * twice. Unfortunately this means we need to call eval1() twice for the
7637 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 if (*start != '}')
7641 {
7642 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7643 return FAIL;
7644 if (*start == '}')
7645 return NOTDONE;
7646 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647
7648 if (evaluate)
7649 {
7650 d = dict_alloc();
7651 if (d == NULL)
7652 return FAIL;
7653 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 tvkey.v_type = VAR_UNKNOWN;
7655 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
7657 *arg = skipwhite(*arg + 1);
7658 while (**arg != '}' && **arg != NUL)
7659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 goto failret;
7662 if (**arg != ':')
7663 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007664 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 goto failret;
7667 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007668 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007670 key = get_tv_string_buf_chk(&tvkey, buf);
7671 if (key == NULL || *key == NUL)
7672 {
7673 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7674 if (key != NULL)
7675 EMSG(_(e_emptykey));
7676 clear_tv(&tvkey);
7677 goto failret;
7678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680
7681 *arg = skipwhite(*arg + 1);
7682 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7683 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007684 if (evaluate)
7685 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 goto failret;
7687 }
7688 if (evaluate)
7689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 if (item != NULL)
7692 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007693 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 clear_tv(&tv);
7696 goto failret;
7697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 item = dictitem_alloc(key);
7699 clear_tv(&tvkey);
7700 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007703 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007704 if (dict_add(d, item) == FAIL)
7705 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706 }
7707 }
7708
7709 if (**arg == '}')
7710 break;
7711 if (**arg != ',')
7712 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007713 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 goto failret;
7715 }
7716 *arg = skipwhite(*arg + 1);
7717 }
7718
7719 if (**arg != '}')
7720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007721 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722failret:
7723 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007724 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 return FAIL;
7726 }
7727
7728 *arg = skipwhite(*arg + 1);
7729 if (evaluate)
7730 {
7731 rettv->v_type = VAR_DICT;
7732 rettv->vval.v_dict = d;
7733 ++d->dv_refcount;
7734 }
7735
7736 return OK;
7737}
7738
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007739#if defined(FEAT_CHANNEL) || defined(PROTO)
7740/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007741 * Decrement the reference count on "channel" and maybe free it when it goes
7742 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007743 * Returns TRUE when the channel was freed.
7744 */
7745 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007746channel_unref(channel_T *channel)
7747{
7748 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007749 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007750 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007751 return TRUE;
7752 }
7753 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007754}
7755#endif
7756
Bram Moolenaar65edff82016-02-21 16:40:11 +01007757#if defined(FEAT_JOB) || defined(PROTO)
7758static job_T *first_job = NULL;
7759
Bram Moolenaar835dc632016-02-07 14:27:38 +01007760 static void
7761job_free(job_T *job)
7762{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007763# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 if (job->jv_channel != NULL)
7765 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007766 /* The link from the channel to the job doesn't count as a reference,
7767 * thus don't decrement the refcount of the job. The reference from
7768 * the job to the channel does count the refrence, decrement it and
7769 * NULL the reference. We don't set ch_job_killed, unreferencing the
7770 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 job->jv_channel->ch_job = NULL;
7772 channel_unref(job->jv_channel);
7773 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007774# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007775 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007776
7777 if (job->jv_next != NULL)
7778 job->jv_next->jv_prev = job->jv_prev;
7779 if (job->jv_prev == NULL)
7780 first_job = job->jv_next;
7781 else
7782 job->jv_prev->jv_next = job->jv_next;
7783
7784 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007785 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 vim_free(job);
7787}
7788
7789 static void
7790job_unref(job_T *job)
7791{
7792 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007793 {
7794 /* Do not free the job when it has not ended yet and there is a
7795 * "stoponexit" flag or an exit callback. */
7796 if (job->jv_status != JOB_STARTED
7797 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7798 job_free(job);
7799 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007800}
7801
7802/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007803 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007804 */
7805 static job_T *
7806job_alloc(void)
7807{
7808 job_T *job;
7809
7810 job = (job_T *)alloc_clear(sizeof(job_T));
7811 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007812 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007813 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007814 job->jv_stoponexit = vim_strsave((char_u *)"term");
7815
7816 if (first_job != NULL)
7817 {
7818 first_job->jv_prev = job;
7819 job->jv_next = first_job;
7820 }
7821 first_job = job;
7822 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007823 return job;
7824}
7825
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 static void
7827job_set_options(job_T *job, jobopt_T *opt)
7828{
7829 if (opt->jo_set & JO_STOPONEXIT)
7830 {
7831 vim_free(job->jv_stoponexit);
7832 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7833 job->jv_stoponexit = NULL;
7834 else
7835 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7836 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007837 if (opt->jo_set & JO_EXIT_CB)
7838 {
7839 vim_free(job->jv_exit_cb);
7840 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7841 job->jv_exit_cb = NULL;
7842 else
7843 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7844 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007845}
7846
7847/*
7848 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7849 */
7850 void
7851job_stop_on_exit()
7852{
7853 job_T *job;
7854
7855 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007856 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007857 mch_stop_job(job, job->jv_stoponexit);
7858}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007859#endif
7860
Bram Moolenaar17a13432016-01-24 14:22:10 +01007861 static char *
7862get_var_special_name(int nr)
7863{
7864 switch (nr)
7865 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007866 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007867 case VVAL_TRUE: return "v:true";
7868 case VVAL_NONE: return "v:none";
7869 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007870 }
7871 EMSG2(_(e_intern2), "get_var_special_name()");
7872 return "42";
7873}
7874
Bram Moolenaar8c711452005-01-14 21:53:12 +00007875/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007876 * Return a string with the string representation of a variable.
7877 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007878 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007880 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007881 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007882 */
7883 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007884echo_string(
7885 typval_T *tv,
7886 char_u **tofree,
7887 char_u *numbuf,
7888 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 static int recurse = 0;
7891 char_u *r = NULL;
7892
Bram Moolenaar33570922005-01-25 22:26:29 +00007893 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007894 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007895 if (!did_echo_string_emsg)
7896 {
7897 /* Only give this message once for a recursive call to avoid
7898 * flooding the user with errors. And stop iterating over lists
7899 * and dicts. */
7900 did_echo_string_emsg = TRUE;
7901 EMSG(_("E724: variable nested too deep for displaying"));
7902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007903 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007904 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 }
7906 ++recurse;
7907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 switch (tv->v_type)
7909 {
7910 case VAR_FUNC:
7911 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 r = tv->vval.v_string;
7913 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007916 if (tv->vval.v_list == NULL)
7917 {
7918 *tofree = NULL;
7919 r = NULL;
7920 }
7921 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7922 {
7923 *tofree = NULL;
7924 r = (char_u *)"[...]";
7925 }
7926 else
7927 {
7928 tv->vval.v_list->lv_copyID = copyID;
7929 *tofree = list2string(tv, copyID);
7930 r = *tofree;
7931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007933
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007935 if (tv->vval.v_dict == NULL)
7936 {
7937 *tofree = NULL;
7938 r = NULL;
7939 }
7940 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7941 {
7942 *tofree = NULL;
7943 r = (char_u *)"{...}";
7944 }
7945 else
7946 {
7947 tv->vval.v_dict->dv_copyID = copyID;
7948 *tofree = dict2string(tv, copyID);
7949 r = *tofree;
7950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007951 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007952
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953 case VAR_STRING:
7954 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007955 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007956 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007957 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 *tofree = NULL;
7959 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007960 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007961
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007962 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007963#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964 *tofree = NULL;
7965 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7966 r = numbuf;
7967 break;
7968#endif
7969
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007970 case VAR_SPECIAL:
7971 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007972 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007973 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007975
Bram Moolenaar8502c702014-06-17 12:51:16 +02007976 if (--recurse == 0)
7977 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007979}
7980
7981/*
7982 * Return a string with the string representation of a variable.
7983 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7984 * "numbuf" is used for a number.
7985 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007986 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987 */
7988 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007989tv2string(
7990 typval_T *tv,
7991 char_u **tofree,
7992 char_u *numbuf,
7993 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994{
7995 switch (tv->v_type)
7996 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 case VAR_FUNC:
7998 *tofree = string_quote(tv->vval.v_string, TRUE);
7999 return *tofree;
8000 case VAR_STRING:
8001 *tofree = string_quote(tv->vval.v_string, FALSE);
8002 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
8004 case VAR_FLOAT:
8005 *tofree = NULL;
8006 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8007 return numbuf;
8008#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008012 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008013 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008014 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008015 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008018 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019}
8020
8021/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008022 * Return string "str" in ' quotes, doubling ' characters.
8023 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008024 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 */
8026 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008027string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028{
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030 char_u *p, *r, *s;
8031
Bram Moolenaar33570922005-01-25 22:26:29 +00008032 len = (function ? 13 : 3);
8033 if (str != NULL)
8034 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008035 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 for (p = str; *p != NUL; mb_ptr_adv(p))
8037 if (*p == '\'')
8038 ++len;
8039 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040 s = r = alloc(len);
8041 if (r != NULL)
8042 {
8043 if (function)
8044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008046 r += 10;
8047 }
8048 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 if (str != NULL)
8051 for (p = str; *p != NUL; )
8052 {
8053 if (*p == '\'')
8054 *r++ = '\'';
8055 MB_COPY_CHAR(p, r);
8056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 if (function)
8059 *r++ = ')';
8060 *r++ = NUL;
8061 }
8062 return s;
8063}
8064
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008065#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008066/*
8067 * Convert the string "text" to a floating point number.
8068 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8069 * this always uses a decimal point.
8070 * Returns the length of the text that was consumed.
8071 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008073string2float(
8074 char_u *text,
8075 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076{
8077 char *s = (char *)text;
8078 float_T f;
8079
8080 f = strtod(s, &s);
8081 *value = f;
8082 return (int)((char_u *)s - text);
8083}
8084#endif
8085
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 * Get the value of an environment variable.
8088 * "arg" is pointing to the '$'. It is advanced to after the name.
8089 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008090 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 */
8092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008093get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 char_u *string = NULL;
8096 int len;
8097 int cc;
8098 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008099 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100
8101 ++*arg;
8102 name = *arg;
8103 len = get_env_len(arg);
8104 if (evaluate)
8105 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008106 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008107 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008108
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008109 cc = name[len];
8110 name[len] = NUL;
8111 /* first try vim_getenv(), fast for normal environment vars */
8112 string = vim_getenv(name, &mustfree);
8113 if (string != NULL && *string != NUL)
8114 {
8115 if (!mustfree)
8116 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 else
8119 {
8120 if (mustfree)
8121 vim_free(string);
8122
8123 /* next try expanding things like $VIM and ${HOME} */
8124 string = expand_env_save(name - 1);
8125 if (string != NULL && *string == '$')
8126 {
8127 vim_free(string);
8128 string = NULL;
8129 }
8130 }
8131 name[len] = cc;
8132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 rettv->v_type = VAR_STRING;
8134 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
8136
8137 return OK;
8138}
8139
8140/*
8141 * Array with names and number of arguments of all internal functions
8142 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8143 */
8144static struct fst
8145{
8146 char *f_name; /* function name */
8147 char f_min_argc; /* minimal number of arguments */
8148 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008149 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008150 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151} functions[] =
8152{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008153#ifdef FEAT_FLOAT
8154 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008155 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008156#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008157 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008158 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008159 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"append", 2, 2, f_append},
8161 {"argc", 0, 0, f_argc},
8162 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008163 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008164 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008165#ifdef FEAT_FLOAT
8166 {"asin", 1, 1, f_asin}, /* WJMc */
8167#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008168 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008169 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008170 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008171 {"assert_false", 1, 2, f_assert_false},
8172 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173#ifdef FEAT_FLOAT
8174 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008175 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008178 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"bufexists", 1, 1, f_bufexists},
8180 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8181 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8182 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8183 {"buflisted", 1, 1, f_buflisted},
8184 {"bufloaded", 1, 1, f_bufloaded},
8185 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008186 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"bufwinnr", 1, 1, f_bufwinnr},
8188 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008189 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008190 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008191 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008192#ifdef FEAT_FLOAT
8193 {"ceil", 1, 1, f_ceil},
8194#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008195#ifdef FEAT_CHANNEL
8196 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008197 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8198 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008199 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008200# ifdef FEAT_JOB
8201 {"ch_getjob", 1, 1, f_ch_getjob},
8202# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008203 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008204 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008205 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008206 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008207 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008208 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8209 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008210 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008211 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008212#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008213 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008214 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008216 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008218#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008219 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008220 {"complete_add", 1, 1, f_complete_add},
8221 {"complete_check", 0, 0, f_complete_check},
8222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008224 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008225#ifdef FEAT_FLOAT
8226 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008227 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008228#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008231 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008232 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008233 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008235 {"diff_filler", 1, 1, f_diff_filler},
8236 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008237 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008238 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"eventhandler", 0, 0, f_eventhandler},
8242 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008243 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008245#ifdef FEAT_FLOAT
8246 {"exp", 1, 1, f_exp},
8247#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008248 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008249 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008250 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8252 {"filereadable", 1, 1, f_filereadable},
8253 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008254 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008255 {"finddir", 1, 3, f_finddir},
8256 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008257#ifdef FEAT_FLOAT
8258 {"float2nr", 1, 1, f_float2nr},
8259 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008260 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008262 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"fnamemodify", 2, 2, f_fnamemodify},
8264 {"foldclosed", 1, 1, f_foldclosed},
8265 {"foldclosedend", 1, 1, f_foldclosedend},
8266 {"foldlevel", 1, 1, f_foldlevel},
8267 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008268 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008271 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008273 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008274 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"getchar", 0, 1, f_getchar},
8276 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008277 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"getcmdline", 0, 0, f_getcmdline},
8279 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008280 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008281 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008282 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008283 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008284 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008285 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getfsize", 1, 1, f_getfsize},
8287 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008288 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008289 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008290 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008291 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008292 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008293 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008294 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008295 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008297 {"gettabvar", 2, 3, f_gettabvar},
8298 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"getwinposx", 0, 0, f_getwinposx},
8300 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008301 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008302 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008303 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008304 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008307 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008308 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8310 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8311 {"histadd", 2, 2, f_histadd},
8312 {"histdel", 1, 2, f_histdel},
8313 {"histget", 1, 2, f_histget},
8314 {"histnr", 1, 1, f_histnr},
8315 {"hlID", 1, 1, f_hlID},
8316 {"hlexists", 1, 1, f_hlexists},
8317 {"hostname", 0, 0, f_hostname},
8318 {"iconv", 3, 3, f_iconv},
8319 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008320 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008321 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008323 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"inputrestore", 0, 0, f_inputrestore},
8325 {"inputsave", 0, 0, f_inputsave},
8326 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008327 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008328 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008330 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008331#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8332 {"isnan", 1, 1, f_isnan},
8333#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008334 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008335#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008336# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008337 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008338# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008339 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008340 {"job_start", 1, 2, f_job_start},
8341 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008342 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008343#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008344 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008345 {"js_decode", 1, 1, f_js_decode},
8346 {"js_encode", 1, 1, f_js_encode},
8347 {"json_decode", 1, 1, f_json_decode},
8348 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008351 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"libcall", 3, 3, f_libcall},
8353 {"libcallnr", 3, 3, f_libcallnr},
8354 {"line", 1, 1, f_line},
8355 {"line2byte", 1, 1, f_line2byte},
8356 {"lispindent", 1, 1, f_lispindent},
8357 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008358#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008359 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008360 {"log10", 1, 1, f_log10},
8361#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008362#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008363 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008364#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008365 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008366 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008367 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008368 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008369 {"matchadd", 2, 5, f_matchadd},
8370 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008371 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008372 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008373 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008374 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008375 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008376 {"max", 1, 1, f_max},
8377 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008378#ifdef vim_mkdir
8379 {"mkdir", 1, 3, f_mkdir},
8380#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008382#ifdef FEAT_MZSCHEME
8383 {"mzeval", 1, 1, f_mzeval},
8384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008386 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008387 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008388 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008389#ifdef FEAT_PERL
8390 {"perleval", 1, 1, f_perleval},
8391#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008392#ifdef FEAT_FLOAT
8393 {"pow", 2, 2, f_pow},
8394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008396 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008397 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008398#ifdef FEAT_PYTHON3
8399 {"py3eval", 1, 1, f_py3eval},
8400#endif
8401#ifdef FEAT_PYTHON
8402 {"pyeval", 1, 1, f_pyeval},
8403#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008404 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008405 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008406 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008407 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008408 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"remote_expr", 2, 3, f_remote_expr},
8410 {"remote_foreground", 1, 1, f_remote_foreground},
8411 {"remote_peek", 1, 2, f_remote_peek},
8412 {"remote_read", 1, 1, f_remote_read},
8413 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008414 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008416 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008418 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#ifdef FEAT_FLOAT
8420 {"round", 1, 1, f_round},
8421#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008422 {"screenattr", 2, 2, f_screenattr},
8423 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008424 {"screencol", 0, 0, f_screencol},
8425 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008426 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008427 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008428 {"searchpair", 3, 7, f_searchpair},
8429 {"searchpairpos", 3, 7, f_searchpairpos},
8430 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"server2client", 2, 2, f_server2client},
8432 {"serverlist", 0, 0, f_serverlist},
8433 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008434 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"setcmdpos", 1, 1, f_setcmdpos},
8436 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008437 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008438 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008439 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008440 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008442 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008443 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008445#ifdef FEAT_CRYPT
8446 {"sha256", 1, 1, f_sha256},
8447#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008448 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008449 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008451#ifdef FEAT_FLOAT
8452 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008453 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008454#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008455 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008456 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008457 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008458 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008459 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460#ifdef FEAT_FLOAT
8461 {"sqrt", 1, 1, f_sqrt},
8462 {"str2float", 1, 1, f_str2float},
8463#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008464 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008465 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008466 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#ifdef HAVE_STRFTIME
8468 {"strftime", 1, 2, f_strftime},
8469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008470 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008471 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {"strlen", 1, 1, f_strlen},
8473 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008474 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008476 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008477 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {"substitute", 4, 4, f_substitute},
8479 {"synID", 3, 3, f_synID},
8480 {"synIDattr", 2, 3, f_synIDattr},
8481 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008482 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008483 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008484 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008485 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008486 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008487 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008488 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008489 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008490 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008491#ifdef FEAT_FLOAT
8492 {"tan", 1, 1, f_tan},
8493 {"tanh", 1, 1, f_tanh},
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008496 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {"tolower", 1, 1, f_tolower},
8498 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008499 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500#ifdef FEAT_FLOAT
8501 {"trunc", 1, 1, f_trunc},
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008504 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008505 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008506 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008507 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"virtcol", 1, 1, f_virtcol},
8509 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008510 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"winbufnr", 1, 1, f_winbufnr},
8512 {"wincol", 0, 0, f_wincol},
8513 {"winheight", 1, 1, f_winheight},
8514 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008515 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008517 {"winrestview", 1, 1, f_winrestview},
8518 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008520 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008521 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008522 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523};
8524
8525#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8526
8527/*
8528 * Function given to ExpandGeneric() to obtain the list of internal
8529 * or user defined function names.
8530 */
8531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008532get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 static int intidx = -1;
8535 char_u *name;
8536
8537 if (idx == 0)
8538 intidx = -1;
8539 if (intidx < 0)
8540 {
8541 name = get_user_func_name(xp, idx);
8542 if (name != NULL)
8543 return name;
8544 }
8545 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8546 {
8547 STRCPY(IObuff, functions[intidx].f_name);
8548 STRCAT(IObuff, "(");
8549 if (functions[intidx].f_max_argc == 0)
8550 STRCAT(IObuff, ")");
8551 return IObuff;
8552 }
8553
8554 return NULL;
8555}
8556
8557/*
8558 * Function given to ExpandGeneric() to obtain the list of internal or
8559 * user defined variable or function names.
8560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008562get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 static int intidx = -1;
8565 char_u *name;
8566
8567 if (idx == 0)
8568 intidx = -1;
8569 if (intidx < 0)
8570 {
8571 name = get_function_name(xp, idx);
8572 if (name != NULL)
8573 return name;
8574 }
8575 return get_user_var_name(xp, ++intidx);
8576}
8577
8578#endif /* FEAT_CMDL_COMPL */
8579
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008580#if defined(EBCDIC) || defined(PROTO)
8581/*
8582 * Compare struct fst by function name.
8583 */
8584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008585compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008586{
8587 struct fst *p1 = (struct fst *)s1;
8588 struct fst *p2 = (struct fst *)s2;
8589
8590 return STRCMP(p1->f_name, p2->f_name);
8591}
8592
8593/*
8594 * Sort the function table by function name.
8595 * The sorting of the table above is ASCII dependant.
8596 * On machines using EBCDIC we have to sort it.
8597 */
8598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008599sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008600{
8601 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8602
8603 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8604}
8605#endif
8606
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608/*
8609 * Find internal function in table above.
8610 * Return index, or -1 if not found
8611 */
8612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008613find_internal_func(
8614 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
8616 int first = 0;
8617 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8618 int cmp;
8619 int x;
8620
8621 /*
8622 * Find the function name in the table. Binary search.
8623 */
8624 while (first <= last)
8625 {
8626 x = first + ((unsigned)(last - first) >> 1);
8627 cmp = STRCMP(name, functions[x].f_name);
8628 if (cmp < 0)
8629 last = x - 1;
8630 else if (cmp > 0)
8631 first = x + 1;
8632 else
8633 return x;
8634 }
8635 return -1;
8636}
8637
8638/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008639 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8640 * name it contains, otherwise return "name".
8641 */
8642 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008643deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644{
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008646 int cc;
8647
8648 cc = name[*lenp];
8649 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008650 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 {
8656 *lenp = 0;
8657 return (char_u *)""; /* just in case */
8658 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008659 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 }
8662
8663 return name;
8664}
8665
8666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 * Allocate a variable for the result of a function.
8668 * Return OK or FAIL.
8669 */
8670 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008671get_func_tv(
8672 char_u *name, /* name of the function */
8673 int len, /* length of "name" */
8674 typval_T *rettv,
8675 char_u **arg, /* argument, pointing to the '(' */
8676 linenr_T firstline, /* first line of range */
8677 linenr_T lastline, /* last line of range */
8678 int *doesrange, /* return: function handled range */
8679 int evaluate,
8680 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 char_u *argp;
8683 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008684 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 int argcount = 0; /* number of arguments found */
8686
8687 /*
8688 * Get the arguments.
8689 */
8690 argp = *arg;
8691 while (argcount < MAX_FUNC_ARGS)
8692 {
8693 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8694 if (*argp == ')' || *argp == ',' || *argp == NUL)
8695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8697 {
8698 ret = FAIL;
8699 break;
8700 }
8701 ++argcount;
8702 if (*argp != ',')
8703 break;
8704 }
8705 if (*argp == ')')
8706 ++argp;
8707 else
8708 ret = FAIL;
8709
8710 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008712 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 {
8715 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008716 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008718 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
8721 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 *arg = skipwhite(argp);
8725 return ret;
8726}
8727
8728
8729/*
8730 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008731 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008732 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008735call_func(
8736 char_u *funcname, /* name of the function */
8737 int len, /* length of "name" */
8738 typval_T *rettv, /* return value goes here */
8739 int argcount, /* number of "argvars" */
8740 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008741 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008742 linenr_T firstline, /* first line of range */
8743 linenr_T lastline, /* last line of range */
8744 int *doesrange, /* return: function handled range */
8745 int evaluate,
8746 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#define ERROR_UNKNOWN 0
8750#define ERROR_TOOMANY 1
8751#define ERROR_TOOFEW 2
8752#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753#define ERROR_DICT 4
8754#define ERROR_NONE 5
8755#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 int error = ERROR_NONE;
8757 int i;
8758 int llen;
8759 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760#define FLEN_FIXED 40
8761 char_u fname_buf[FLEN_FIXED + 1];
8762 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008763 char_u *name;
8764
8765 /* Make a copy of the name, if it comes from a funcref variable it could
8766 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008767 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008768 if (name == NULL)
8769 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770
8771 /*
8772 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8773 * Change <SNR>123_name() to K_SNR 123_name().
8774 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 llen = eval_fname_script(name);
8777 if (llen > 0)
8778 {
8779 fname_buf[0] = K_SPECIAL;
8780 fname_buf[1] = KS_EXTRA;
8781 fname_buf[2] = (int)KE_SNR;
8782 i = 3;
8783 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8784 {
8785 if (current_SID <= 0)
8786 error = ERROR_SCRIPT;
8787 else
8788 {
8789 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8790 i = (int)STRLEN(fname_buf);
8791 }
8792 }
8793 if (i + STRLEN(name + llen) < FLEN_FIXED)
8794 {
8795 STRCPY(fname_buf + i, name + llen);
8796 fname = fname_buf;
8797 }
8798 else
8799 {
8800 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8801 if (fname == NULL)
8802 error = ERROR_OTHER;
8803 else
8804 {
8805 mch_memmove(fname, fname_buf, (size_t)i);
8806 STRCPY(fname + i, name + llen);
8807 }
8808 }
8809 }
8810 else
8811 fname = name;
8812
8813 *doesrange = FALSE;
8814
8815
8816 /* execute the function if no errors detected and executing */
8817 if (evaluate && error == ERROR_NONE)
8818 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008819 char_u *rfname = fname;
8820
8821 /* Ignore "g:" before a function name. */
8822 if (fname[0] == 'g' && fname[1] == ':')
8823 rfname = fname + 2;
8824
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008825 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8826 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 error = ERROR_UNKNOWN;
8828
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008829 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
8831 /*
8832 * User defined function.
8833 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008834 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008837 /* Trigger FuncUndefined event, may load the function. */
8838 if (fp == NULL
8839 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008843 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008844 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
8846#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008847 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008848 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008849 {
8850 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008851 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 }
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (fp != NULL)
8855 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008856 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008858 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008862 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008863 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 else
8865 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008866 int did_save_redo = FALSE;
8867
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 /*
8869 * Call the user function.
8870 * Save and restore search patterns, script variables and
8871 * redo buffer.
8872 */
8873 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008874#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008875 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008876#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008877 {
8878 saveRedobuff();
8879 did_save_redo = TRUE;
8880 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008881 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008882 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008884 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8885 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8886 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008887 /* Function was unreferenced while being used, free it
8888 * now. */
8889 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 if (did_save_redo)
8891 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 restore_search_patterns();
8893 error = ERROR_NONE;
8894 }
8895 }
8896 }
8897 else
8898 {
8899 /*
8900 * Find the function name in the table, call its implementation.
8901 */
8902 i = find_internal_func(fname);
8903 if (i >= 0)
8904 {
8905 if (argcount < functions[i].f_min_argc)
8906 error = ERROR_TOOFEW;
8907 else if (argcount > functions[i].f_max_argc)
8908 error = ERROR_TOOMANY;
8909 else
8910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008911 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 error = ERROR_NONE;
8914 }
8915 }
8916 }
8917 /*
8918 * The function call (or "FuncUndefined" autocommand sequence) might
8919 * have been aborted by an error, an interrupt, or an explicitly thrown
8920 * exception that has not been caught so far. This situation can be
8921 * tested for by calling aborting(). For an error in an internal
8922 * function or for the "E132" error in call_user_func(), however, the
8923 * throw point at which the "force_abort" flag (temporarily reset by
8924 * emsg()) is normally updated has not been reached yet. We need to
8925 * update that flag first to make aborting() reliable.
8926 */
8927 update_force_abort();
8928 }
8929 if (error == ERROR_NONE)
8930 ret = OK;
8931
8932 /*
8933 * Report an error unless the argument evaluation or function call has been
8934 * cancelled due to an aborting error, an interrupt, or an exception.
8935 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008936 if (!aborting())
8937 {
8938 switch (error)
8939 {
8940 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008941 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008942 break;
8943 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008944 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008945 break;
8946 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008947 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008948 name);
8949 break;
8950 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008951 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008952 name);
8953 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008955 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008956 name);
8957 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008958 }
8959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 if (fname != name && fname != fname_buf)
8962 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008963 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
8965 return ret;
8966}
8967
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008968/*
8969 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008970 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008971 */
8972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008973emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008974{
8975 char_u *p;
8976
8977 if (*name == K_SPECIAL)
8978 p = concat_str((char_u *)"<SNR>", name + 3);
8979 else
8980 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008981 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008982 if (p != name)
8983 vim_free(p);
8984}
8985
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008986/*
8987 * Return TRUE for a non-zero Number and a non-empty String.
8988 */
8989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008990non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008991{
8992 return ((argvars[0].v_type == VAR_NUMBER
8993 && argvars[0].vval.v_number != 0)
8994 || (argvars[0].v_type == VAR_STRING
8995 && argvars[0].vval.v_string != NULL
8996 && *argvars[0].vval.v_string != NUL));
8997}
8998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999/*********************************************
9000 * Implementation of the built-in functions
9001 */
9002
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009003#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009004static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009005
9006/*
9007 * Get the float value of "argvars[0]" into "f".
9008 * Returns FAIL when the argument is not a Number or Float.
9009 */
9010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009012{
9013 if (argvars[0].v_type == VAR_FLOAT)
9014 {
9015 *f = argvars[0].vval.v_float;
9016 return OK;
9017 }
9018 if (argvars[0].v_type == VAR_NUMBER)
9019 {
9020 *f = (float_T)argvars[0].vval.v_number;
9021 return OK;
9022 }
9023 EMSG(_("E808: Number or Float required"));
9024 return FAIL;
9025}
9026
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009027/*
9028 * "abs(expr)" function
9029 */
9030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009031f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009032{
9033 if (argvars[0].v_type == VAR_FLOAT)
9034 {
9035 rettv->v_type = VAR_FLOAT;
9036 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9037 }
9038 else
9039 {
9040 varnumber_T n;
9041 int error = FALSE;
9042
9043 n = get_tv_number_chk(&argvars[0], &error);
9044 if (error)
9045 rettv->vval.v_number = -1;
9046 else if (n > 0)
9047 rettv->vval.v_number = n;
9048 else
9049 rettv->vval.v_number = -n;
9050 }
9051}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009052
9053/*
9054 * "acos()" function
9055 */
9056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009058{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009059 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009060
9061 rettv->v_type = VAR_FLOAT;
9062 if (get_float_arg(argvars, &f) == OK)
9063 rettv->vval.v_float = acos(f);
9064 else
9065 rettv->vval.v_float = 0.0;
9066}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009067#endif
9068
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 */
9072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009073f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
Bram Moolenaar33570922005-01-25 22:26:29 +00009075 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009078 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009080 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009081 && !tv_check_lock(l->lv_lock,
9082 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009083 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009085 }
9086 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009087 EMSG(_(e_listreq));
9088}
9089
9090/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009091 * "alloc_fail(id, countdown, repeat)" function
9092 */
9093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009094f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009095{
9096 if (argvars[0].v_type != VAR_NUMBER
9097 || argvars[0].vval.v_number <= 0
9098 || argvars[1].v_type != VAR_NUMBER
9099 || argvars[1].vval.v_number < 0
9100 || argvars[2].v_type != VAR_NUMBER)
9101 EMSG(_(e_invarg));
9102 else
9103 {
9104 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009105 if (alloc_fail_id >= aid_last)
9106 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009107 alloc_fail_countdown = argvars[1].vval.v_number;
9108 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009109 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009110 }
9111}
9112
9113/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009114 * "and(expr, expr)" function
9115 */
9116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009117f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009118{
9119 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9120 & get_tv_number_chk(&argvars[1], NULL);
9121}
9122
9123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009124 * "append(lnum, string/list)" function
9125 */
9126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009127f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128{
9129 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 list_T *l = NULL;
9132 listitem_T *li = NULL;
9133 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009134 long added = 0;
9135
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009136 /* When coming here from Insert mode, sync undo, so that this can be
9137 * undone separately from what was previously inserted. */
9138 if (u_sync_once == 2)
9139 {
9140 u_sync_once = 1; /* notify that u_sync() was called */
9141 u_sync(TRUE);
9142 }
9143
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 lnum = get_tv_lnum(argvars);
9145 if (lnum >= 0
9146 && lnum <= curbuf->b_ml.ml_line_count
9147 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009151 l = argvars[1].vval.v_list;
9152 if (l == NULL)
9153 return;
9154 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009156 for (;;)
9157 {
9158 if (l == NULL)
9159 tv = &argvars[1]; /* append a string */
9160 else if (li == NULL)
9161 break; /* end of list */
9162 else
9163 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 line = get_tv_string_chk(tv);
9165 if (line == NULL) /* type error */
9166 {
9167 rettv->vval.v_number = 1; /* Failed */
9168 break;
9169 }
9170 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 ++added;
9172 if (l == NULL)
9173 break;
9174 li = li->li_next;
9175 }
9176
9177 appended_lines_mark(lnum, added);
9178 if (curwin->w_cursor.lnum > lnum)
9179 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 else
9182 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183}
9184
9185/*
9186 * "argc()" function
9187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
9195 * "argidx()" function
9196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009198f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
9203/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009204 * "arglistid()" function
9205 */
9206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009207f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009208{
9209 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009210
9211 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009212 wp = find_tabwin(&argvars[0], &argvars[1]);
9213 if (wp != NULL)
9214 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009215}
9216
9217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 * "argv(nr)" function
9219 */
9220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009221f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 int idx;
9224
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009225 if (argvars[0].v_type != VAR_UNKNOWN)
9226 {
9227 idx = get_tv_number_chk(&argvars[0], NULL);
9228 if (idx >= 0 && idx < ARGCOUNT)
9229 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9230 else
9231 rettv->vval.v_string = NULL;
9232 rettv->v_type = VAR_STRING;
9233 }
9234 else if (rettv_list_alloc(rettv) == OK)
9235 for (idx = 0; idx < ARGCOUNT; ++idx)
9236 list_append_string(rettv->vval.v_list,
9237 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238}
9239
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009240static void prepare_assert_error(garray_T*gap);
9241static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9242static void assert_error(garray_T *gap);
9243static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244
9245/*
9246 * Prepare "gap" for an assert error and add the sourcing position.
9247 */
9248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009249prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009250{
9251 char buf[NUMBUFLEN];
9252
9253 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009254 if (sourcing_name != NULL)
9255 {
9256 ga_concat(gap, sourcing_name);
9257 if (sourcing_lnum > 0)
9258 ga_concat(gap, (char_u *)" ");
9259 }
9260 if (sourcing_lnum > 0)
9261 {
9262 sprintf(buf, "line %ld", (long)sourcing_lnum);
9263 ga_concat(gap, (char_u *)buf);
9264 }
9265 if (sourcing_name != NULL || sourcing_lnum > 0)
9266 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267}
9268
9269/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009270 * Append "str" to "gap", escaping unprintable characters.
9271 * Changes NL to \n, CR to \r, etc.
9272 */
9273 static void
9274ga_concat_esc(garray_T *gap, char_u *str)
9275{
9276 char_u *p;
9277 char_u buf[NUMBUFLEN];
9278
9279 for (p = str; *p != NUL; ++p)
9280 switch (*p)
9281 {
9282 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9283 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9284 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9285 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9286 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9287 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9288 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9289 default:
9290 if (*p < ' ')
9291 {
9292 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9293 ga_concat(gap, buf);
9294 }
9295 else
9296 ga_append(gap, *p);
9297 break;
9298 }
9299}
9300
9301/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009302 * Fill "gap" with information about an assert error.
9303 */
9304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009305fill_assert_error(
9306 garray_T *gap,
9307 typval_T *opt_msg_tv,
9308 char_u *exp_str,
9309 typval_T *exp_tv,
9310 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009311{
9312 char_u numbuf[NUMBUFLEN];
9313 char_u *tofree;
9314
9315 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9316 {
9317 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9318 vim_free(tofree);
9319 }
9320 else
9321 {
9322 ga_concat(gap, (char_u *)"Expected ");
9323 if (exp_str == NULL)
9324 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009325 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009326 vim_free(tofree);
9327 }
9328 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009329 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009330 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009331 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009332 vim_free(tofree);
9333 }
9334}
Bram Moolenaar43345542015-11-29 17:35:35 +01009335
9336/*
9337 * Add an assert error to v:errors.
9338 */
9339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009340assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009341{
9342 struct vimvar *vp = &vimvars[VV_ERRORS];
9343
9344 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9345 /* Make sure v:errors is a list. */
9346 set_vim_var_list(VV_ERRORS, list_alloc());
9347 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9348}
9349
Bram Moolenaar43345542015-11-29 17:35:35 +01009350/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009351 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009352 */
9353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009354f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009355{
9356 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009357
9358 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9359 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360 prepare_assert_error(&ga);
9361 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9362 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009363 ga_clear(&ga);
9364 }
9365}
9366
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009367/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009368 * "assert_exception(string[, msg])" function
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009372{
9373 garray_T ga;
9374 char *error;
9375
9376 error = (char *)get_tv_string_chk(&argvars[0]);
9377 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9378 {
9379 prepare_assert_error(&ga);
9380 ga_concat(&ga, (char_u *)"v:exception is not set");
9381 assert_error(&ga);
9382 ga_clear(&ga);
9383 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009384 else if (error != NULL
9385 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009386 {
9387 prepare_assert_error(&ga);
9388 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9389 &vimvars[VV_EXCEPTION].vv_tv);
9390 assert_error(&ga);
9391 ga_clear(&ga);
9392 }
9393}
9394
9395/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009396 * "assert_fails(cmd [, error])" function
9397 */
9398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009399f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009400{
9401 char_u *cmd = get_tv_string_chk(&argvars[0]);
9402 garray_T ga;
9403
9404 called_emsg = FALSE;
9405 suppress_errthrow = TRUE;
9406 emsg_silent = TRUE;
9407 do_cmdline_cmd(cmd);
9408 if (!called_emsg)
9409 {
9410 prepare_assert_error(&ga);
9411 ga_concat(&ga, (char_u *)"command did not fail: ");
9412 ga_concat(&ga, cmd);
9413 assert_error(&ga);
9414 ga_clear(&ga);
9415 }
9416 else if (argvars[1].v_type != VAR_UNKNOWN)
9417 {
9418 char_u buf[NUMBUFLEN];
9419 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9420
9421 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9422 {
9423 prepare_assert_error(&ga);
9424 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9425 &vimvars[VV_ERRMSG].vv_tv);
9426 assert_error(&ga);
9427 ga_clear(&ga);
9428 }
9429 }
9430
9431 called_emsg = FALSE;
9432 suppress_errthrow = FALSE;
9433 emsg_silent = FALSE;
9434 emsg_on_display = FALSE;
9435 set_vim_var_string(VV_ERRMSG, NULL, 0);
9436}
9437
9438/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009439 * Common for assert_true() and assert_false().
9440 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009443{
9444 int error = FALSE;
9445 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009446
Bram Moolenaar37127922016-02-06 20:29:28 +01009447 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009448 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009449 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009450 if (argvars[0].v_type != VAR_NUMBER
9451 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9452 || error)
9453 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 prepare_assert_error(&ga);
9455 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009456 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009457 NULL, &argvars[0]);
9458 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009459 ga_clear(&ga);
9460 }
9461}
9462
9463/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009464 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009468{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009470}
9471
9472/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009473 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 */
9475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009477{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009478 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009479}
9480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009484 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009487{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009488 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009489
9490 rettv->v_type = VAR_FLOAT;
9491 if (get_float_arg(argvars, &f) == OK)
9492 rettv->vval.v_float = asin(f);
9493 else
9494 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495}
9496
9497/*
9498 * "atan()" function
9499 */
9500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009503 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = atan(f);
9508 else
9509 rettv->vval.v_float = 0.0;
9510}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009511
9512/*
9513 * "atan2()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009517{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009518 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &fx) == OK
9522 && get_float_arg(&argvars[1], &fy) == OK)
9523 rettv->vval.v_float = atan2(fx, fy);
9524 else
9525 rettv->vval.v_float = 0.0;
9526}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527#endif
9528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529/*
9530 * "browse(save, title, initdir, default)" function
9531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535#ifdef FEAT_BROWSE
9536 int save;
9537 char_u *title;
9538 char_u *initdir;
9539 char_u *defname;
9540 char_u buf[NUMBUFLEN];
9541 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 save = get_tv_number_chk(&argvars[0], &error);
9545 title = get_tv_string_chk(&argvars[1]);
9546 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9547 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 if (error || title == NULL || initdir == NULL || defname == NULL)
9550 rettv->vval.v_string = NULL;
9551 else
9552 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 do_browse(save ? BROWSE_SAVE : 0,
9554 title, defname, NULL, initdir, NULL, curbuf);
9555#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559}
9560
9561/*
9562 * "browsedir(title, initdir)" function
9563 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009566{
9567#ifdef FEAT_BROWSE
9568 char_u *title;
9569 char_u *initdir;
9570 char_u buf[NUMBUFLEN];
9571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 title = get_tv_string_chk(&argvars[0]);
9573 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (title == NULL || initdir == NULL)
9576 rettv->vval.v_string = NULL;
9577 else
9578 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009586static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588/*
9589 * Find a buffer by number or exact name.
9590 */
9591 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (avar->v_type == VAR_NUMBER)
9597 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009598 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009600 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009601 if (buf == NULL)
9602 {
9603 /* No full path name match, try a match with a URL or a "nofile"
9604 * buffer, these don't use the full path. */
9605 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9606 if (buf->b_fname != NULL
9607 && (path_with_url(buf->b_fname)
9608#ifdef FEAT_QUICKFIX
9609 || bt_nofile(buf)
9610#endif
9611 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009613 break;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 }
9616 return buf;
9617}
9618
9619/*
9620 * "bufexists(expr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
9628/*
9629 * "buflisted(expr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 buf_T *buf;
9635
9636 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
9641 * "bufloaded(expr)" function
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 buf_T *buf;
9647
9648 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650}
9651
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009652static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009653
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654/*
9655 * Get buffer by number or pattern.
9656 */
9657 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 int save_magic;
9662 char_u *save_cpo;
9663 buf_T *buf;
9664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 if (tv->v_type == VAR_NUMBER)
9666 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009667 if (tv->v_type != VAR_STRING)
9668 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 if (name == NULL || *name == NUL)
9670 return curbuf;
9671 if (name[0] == '$' && name[1] == NUL)
9672 return lastbuf;
9673
9674 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9675 save_magic = p_magic;
9676 p_magic = TRUE;
9677 save_cpo = p_cpo;
9678 p_cpo = (char_u *)"";
9679
9680 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009681 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 p_magic = save_magic;
9684 p_cpo = save_cpo;
9685
9686 /* If not found, try expanding the name, like done for bufexists(). */
9687 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689
9690 return buf;
9691}
9692
9693/*
9694 * "bufname(expr)" function
9695 */
9696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699 buf_T *buf;
9700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009701 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009703 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 --emsg_off;
9710}
9711
9712/*
9713 * "bufnr(expr)" function
9714 */
9715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009716f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009719 int error = FALSE;
9720 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009724 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 --emsg_off;
9726
9727 /* If the buffer isn't found and the second argument is not zero create a
9728 * new buffer. */
9729 if (buf == NULL
9730 && argvars[1].v_type != VAR_UNKNOWN
9731 && get_tv_number_chk(&argvars[1], &error) != 0
9732 && !error
9733 && (name = get_tv_string_chk(&argvars[0])) != NULL
9734 && !error)
9735 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9736
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
9744 * "bufwinnr(nr)" function
9745 */
9746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifdef FEAT_WINDOWS
9750 win_T *wp;
9751 int winnr = 0;
9752#endif
9753 buf_T *buf;
9754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009757 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758#ifdef FEAT_WINDOWS
9759 for (wp = firstwin; wp; wp = wp->w_next)
9760 {
9761 ++winnr;
9762 if (wp->w_buffer == buf)
9763 break;
9764 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#endif
9769 --emsg_off;
9770}
9771
9772/*
9773 * "byte2line(byte)" function
9774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009776f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#else
9781 long boff = 0;
9782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 (linenr_T)0, &boff);
9789#endif
9790}
9791
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009793byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009794{
9795#ifdef FEAT_MBYTE
9796 char_u *t;
9797#endif
9798 char_u *str;
9799 long idx;
9800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801 str = get_tv_string_chk(&argvars[0]);
9802 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009805 return;
9806
9807#ifdef FEAT_MBYTE
9808 t = str;
9809 for ( ; idx > 0; idx--)
9810 {
9811 if (*t == NUL) /* EOL reached */
9812 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009813 if (enc_utf8 && comp)
9814 t += utf_ptr2len(t);
9815 else
9816 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009818 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009819#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009820 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009822#endif
9823}
9824
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009825/*
9826 * "byteidx()" function
9827 */
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009830{
9831 byteidx(argvars, rettv, FALSE);
9832}
9833
9834/*
9835 * "byteidxcomp()" function
9836 */
9837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009838f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009839{
9840 byteidx(argvars, rettv, TRUE);
9841}
9842
Bram Moolenaardb913952012-06-29 12:54:53 +02009843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844func_call(
9845 char_u *name,
9846 typval_T *args,
9847 dict_T *selfdict,
9848 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009849{
9850 listitem_T *item;
9851 typval_T argv[MAX_FUNC_ARGS + 1];
9852 int argc = 0;
9853 int dummy;
9854 int r = 0;
9855
9856 for (item = args->vval.v_list->lv_first; item != NULL;
9857 item = item->li_next)
9858 {
9859 if (argc == MAX_FUNC_ARGS)
9860 {
9861 EMSG(_("E699: Too many arguments"));
9862 break;
9863 }
9864 /* Make a copy of each argument. This is needed to be able to set
9865 * v_lock to VAR_FIXED in the copy without changing the original list.
9866 */
9867 copy_tv(&item->li_tv, &argv[argc++]);
9868 }
9869
9870 if (item == NULL)
9871 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9872 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9873 &dummy, TRUE, selfdict);
9874
9875 /* Free the arguments. */
9876 while (argc > 0)
9877 clear_tv(&argv[--argc]);
9878
9879 return r;
9880}
9881
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009882/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883 * "call(func, arglist)" function
9884 */
9885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009886f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887{
9888 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009890
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891 if (argvars[1].v_type != VAR_LIST)
9892 {
9893 EMSG(_(e_listreq));
9894 return;
9895 }
9896 if (argvars[1].vval.v_list == NULL)
9897 return;
9898
9899 if (argvars[0].v_type == VAR_FUNC)
9900 func = argvars[0].vval.v_string;
9901 else
9902 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 if (*func == NUL)
9904 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905
Bram Moolenaare9a41262005-01-15 22:18:47 +00009906 if (argvars[2].v_type != VAR_UNKNOWN)
9907 {
9908 if (argvars[2].v_type != VAR_DICT)
9909 {
9910 EMSG(_(e_dictreq));
9911 return;
9912 }
9913 selfdict = argvars[2].vval.v_dict;
9914 }
9915
Bram Moolenaardb913952012-06-29 12:54:53 +02009916 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917}
9918
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009919#ifdef FEAT_FLOAT
9920/*
9921 * "ceil({float})" function
9922 */
9923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009924f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009925{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009926 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927
9928 rettv->v_type = VAR_FLOAT;
9929 if (get_float_arg(argvars, &f) == OK)
9930 rettv->vval.v_float = ceil(f);
9931 else
9932 rettv->vval.v_float = 0.0;
9933}
9934#endif
9935
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009936#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009937/*
9938 * Get a callback from "arg". It can be a Funcref or a function name.
9939 * When "arg" is zero return an empty string.
9940 * Return NULL for an invalid argument.
9941 */
9942 static char_u *
9943get_callback(typval_T *arg)
9944{
9945 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9946 return arg->vval.v_string;
9947 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9948 return (char_u *)"";
9949 EMSG(_("E999: Invalid callback argument"));
9950 return NULL;
9951}
9952
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009953 static int
9954handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9955{
9956 char_u *val = get_tv_string(item);
9957
9958 opt->jo_set |= jo;
9959 if (STRCMP(val, "nl") == 0)
9960 *modep = MODE_NL;
9961 else if (STRCMP(val, "raw") == 0)
9962 *modep = MODE_RAW;
9963 else if (STRCMP(val, "js") == 0)
9964 *modep = MODE_JS;
9965 else if (STRCMP(val, "json") == 0)
9966 *modep = MODE_JSON;
9967 else
9968 {
9969 EMSG2(_(e_invarg2), val);
9970 return FAIL;
9971 }
9972 return OK;
9973}
9974
Bram Moolenaar187db502016-02-27 14:44:26 +01009975 static int
9976handle_io(typval_T *item, int part, jobopt_T *opt)
9977{
9978 char_u *val = get_tv_string(item);
9979
9980 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9981 if (STRCMP(val, "null") == 0)
9982 opt->jo_io[part] = JIO_NULL;
9983 else if (STRCMP(val, "pipe") == 0)
9984 opt->jo_io[part] = JIO_PIPE;
9985 else if (STRCMP(val, "file") == 0)
9986 opt->jo_io[part] = JIO_FILE;
9987 else if (STRCMP(val, "buffer") == 0)
9988 opt->jo_io[part] = JIO_BUFFER;
9989 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9990 opt->jo_io[part] = JIO_OUT;
9991 else
9992 {
9993 EMSG2(_(e_invarg2), val);
9994 return FAIL;
9995 }
9996 return OK;
9997}
9998
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009999 static void
10000clear_job_options(jobopt_T *opt)
10001{
10002 vim_memset(opt, 0, sizeof(jobopt_T));
10003}
10004
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010005/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010006 * Get the PART_ number from the first character of an option name.
10007 */
10008 static int
10009part_from_char(int c)
10010{
10011 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10012}
10013
10014/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010015 * Get the option entries from the dict in "tv", parse them and put the result
10016 * in "opt".
10017 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010018 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010019 */
10020 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010021get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010022{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010023 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010024 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010025 dict_T *dict;
10026 int todo;
10027 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010028 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010029
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010030 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010031 if (tv->v_type == VAR_UNKNOWN)
10032 return OK;
10033 if (tv->v_type != VAR_DICT)
10034 {
10035 EMSG(_(e_invarg));
10036 return FAIL;
10037 }
10038 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010039 if (dict == NULL)
10040 return OK;
10041
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010042 todo = (int)dict->dv_hashtab.ht_used;
10043 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10044 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010045 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010046 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010047
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010048 if (STRCMP(hi->hi_key, "mode") == 0)
10049 {
10050 if (!(supported & JO_MODE))
10051 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010052 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010053 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010054 }
10055 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10056 {
10057 if (!(supported & JO_IN_MODE))
10058 break;
10059 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10060 == FAIL)
10061 return FAIL;
10062 }
10063 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10064 {
10065 if (!(supported & JO_OUT_MODE))
10066 break;
10067 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10068 == FAIL)
10069 return FAIL;
10070 }
10071 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10072 {
10073 if (!(supported & JO_ERR_MODE))
10074 break;
10075 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10076 == FAIL)
10077 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010078 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010079 else if (STRCMP(hi->hi_key, "in-io") == 0
10080 || STRCMP(hi->hi_key, "out-io") == 0
10081 || STRCMP(hi->hi_key, "err-io") == 0)
10082 {
10083 if (!(supported & JO_OUT_IO))
10084 break;
10085 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10086 return FAIL;
10087 }
10088 else if (STRCMP(hi->hi_key, "in-name") == 0
10089 || STRCMP(hi->hi_key, "out-name") == 0
10090 || STRCMP(hi->hi_key, "err-name") == 0)
10091 {
10092 part = part_from_char(*hi->hi_key);
10093
10094 if (!(supported & JO_OUT_IO))
10095 break;
10096 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10097 opt->jo_io_name[part] =
10098 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10099 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 else if (STRCMP(hi->hi_key, "callback") == 0)
10101 {
10102 if (!(supported & JO_CALLBACK))
10103 break;
10104 opt->jo_set |= JO_CALLBACK;
10105 opt->jo_callback = get_callback(item);
10106 if (opt->jo_callback == NULL)
10107 {
10108 EMSG2(_(e_invarg2), "callback");
10109 return FAIL;
10110 }
10111 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010112 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10113 {
10114 if (!(supported & JO_OUT_CALLBACK))
10115 break;
10116 opt->jo_set |= JO_OUT_CALLBACK;
10117 opt->jo_out_cb = get_callback(item);
10118 if (opt->jo_out_cb == NULL)
10119 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010120 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010121 return FAIL;
10122 }
10123 }
10124 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10125 {
10126 if (!(supported & JO_ERR_CALLBACK))
10127 break;
10128 opt->jo_set |= JO_ERR_CALLBACK;
10129 opt->jo_err_cb = get_callback(item);
10130 if (opt->jo_err_cb == NULL)
10131 {
10132 EMSG2(_(e_invarg2), "err-cb");
10133 return FAIL;
10134 }
10135 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010136 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10137 {
10138 if (!(supported & JO_CLOSE_CALLBACK))
10139 break;
10140 opt->jo_set |= JO_CLOSE_CALLBACK;
10141 opt->jo_close_cb = get_callback(item);
10142 if (opt->jo_close_cb == NULL)
10143 {
10144 EMSG2(_(e_invarg2), "close-cb");
10145 return FAIL;
10146 }
10147 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010148 else if (STRCMP(hi->hi_key, "waittime") == 0)
10149 {
10150 if (!(supported & JO_WAITTIME))
10151 break;
10152 opt->jo_set |= JO_WAITTIME;
10153 opt->jo_waittime = get_tv_number(item);
10154 }
10155 else if (STRCMP(hi->hi_key, "timeout") == 0)
10156 {
10157 if (!(supported & JO_TIMEOUT))
10158 break;
10159 opt->jo_set |= JO_TIMEOUT;
10160 opt->jo_timeout = get_tv_number(item);
10161 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010162 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10163 {
10164 if (!(supported & JO_OUT_TIMEOUT))
10165 break;
10166 opt->jo_set |= JO_OUT_TIMEOUT;
10167 opt->jo_out_timeout = get_tv_number(item);
10168 }
10169 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10170 {
10171 if (!(supported & JO_ERR_TIMEOUT))
10172 break;
10173 opt->jo_set |= JO_ERR_TIMEOUT;
10174 opt->jo_err_timeout = get_tv_number(item);
10175 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010176 else if (STRCMP(hi->hi_key, "part") == 0)
10177 {
10178 if (!(supported & JO_PART))
10179 break;
10180 opt->jo_set |= JO_PART;
10181 val = get_tv_string(item);
10182 if (STRCMP(val, "err") == 0)
10183 opt->jo_part = PART_ERR;
10184 else
10185 {
10186 EMSG2(_(e_invarg2), val);
10187 return FAIL;
10188 }
10189 }
10190 else if (STRCMP(hi->hi_key, "id") == 0)
10191 {
10192 if (!(supported & JO_ID))
10193 break;
10194 opt->jo_set |= JO_ID;
10195 opt->jo_id = get_tv_number(item);
10196 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010197 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10198 {
10199 if (!(supported & JO_STOPONEXIT))
10200 break;
10201 opt->jo_set |= JO_STOPONEXIT;
10202 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10203 opt->jo_soe_buf);
10204 if (opt->jo_stoponexit == NULL)
10205 {
10206 EMSG2(_(e_invarg2), "stoponexit");
10207 return FAIL;
10208 }
10209 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010210 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10211 {
10212 if (!(supported & JO_EXIT_CB))
10213 break;
10214 opt->jo_set |= JO_EXIT_CB;
10215 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010216 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010217 {
10218 EMSG2(_(e_invarg2), "exit-cb");
10219 return FAIL;
10220 }
10221 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010222 else
10223 break;
10224 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010225 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010226 if (todo > 0)
10227 {
10228 EMSG2(_(e_invarg2), hi->hi_key);
10229 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010230 }
10231
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010232 return OK;
10233}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010234#endif
10235
10236#ifdef FEAT_CHANNEL
10237/*
10238 * Get the channel from the argument.
10239 * Returns NULL if the handle is invalid.
10240 */
10241 static channel_T *
10242get_channel_arg(typval_T *tv)
10243{
10244 channel_T *channel;
10245
10246 if (tv->v_type != VAR_CHANNEL)
10247 {
10248 EMSG2(_(e_invarg2), get_tv_string(tv));
10249 return NULL;
10250 }
10251 channel = tv->vval.v_channel;
10252
10253 if (channel == NULL || !channel_is_open(channel))
10254 {
10255 EMSG(_("E906: not an open channel"));
10256 return NULL;
10257 }
10258 return channel;
10259}
10260
10261/*
10262 * "ch_close()" function
10263 */
10264 static void
10265f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10266{
10267 channel_T *channel = get_channel_arg(&argvars[0]);
10268
10269 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010270 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010271 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010272 channel_clear(channel);
10273 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010274}
10275
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010276/*
10277 * "ch_getbufnr()" function
10278 */
10279 static void
10280f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10281{
10282 channel_T *channel = get_channel_arg(&argvars[0]);
10283
10284 rettv->vval.v_number = -1;
10285 if (channel != NULL)
10286 {
10287 char_u *what = get_tv_string(&argvars[1]);
10288 int part;
10289
10290 if (STRCMP(what, "err") == 0)
10291 part = PART_ERR;
10292 else if (STRCMP(what, "out") == 0)
10293 part = PART_OUT;
10294 else if (STRCMP(what, "in") == 0)
10295 part = PART_IN;
10296 else
10297 part = PART_SOCK;
10298 if (channel->ch_part[part].ch_buffer != NULL)
10299 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10300 }
10301}
10302
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010303# ifdef FEAT_JOB
10304/*
10305 * "ch_getjob()" function
10306 */
10307 static void
10308f_ch_getjob(typval_T *argvars, typval_T *rettv)
10309{
10310 channel_T *channel = get_channel_arg(&argvars[0]);
10311
10312 if (channel != NULL)
10313 {
10314 rettv->v_type = VAR_JOB;
10315 rettv->vval.v_job = channel->ch_job;
10316 if (channel->ch_job != NULL)
10317 ++channel->ch_job->jv_refcount;
10318 }
10319}
10320# endif
10321
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010322/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010323 * "ch_log()" function
10324 */
10325 static void
10326f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10327{
10328 char_u *msg = get_tv_string(&argvars[0]);
10329 channel_T *channel = NULL;
10330
10331 if (argvars[1].v_type != VAR_UNKNOWN)
10332 channel = get_channel_arg(&argvars[1]);
10333
10334 ch_log(channel, (char *)msg);
10335}
10336
10337/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010338 * "ch_logfile()" function
10339 */
10340 static void
10341f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10342{
10343 char_u *fname;
10344 char_u *opt = (char_u *)"";
10345 char_u buf[NUMBUFLEN];
10346 FILE *file = NULL;
10347
10348 fname = get_tv_string(&argvars[0]);
10349 if (argvars[1].v_type == VAR_STRING)
10350 opt = get_tv_string_buf(&argvars[1], buf);
10351 if (*fname != NUL)
10352 {
10353 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10354 if (file == NULL)
10355 {
10356 EMSG2(_(e_notopen), fname);
10357 return;
10358 }
10359 }
10360 ch_logfile(file);
10361}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010362
10363/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010364 * "ch_open()" function
10365 */
10366 static void
10367f_ch_open(typval_T *argvars, typval_T *rettv)
10368{
10369 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010370 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010371 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010372 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010373 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010374 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010375
10376 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010377 rettv->v_type = VAR_CHANNEL;
10378 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010379
10380 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010381 if (argvars[1].v_type != VAR_UNKNOWN
10382 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010383 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010384 EMSG(_(e_invarg));
10385 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010386 }
10387
10388 /* parse address */
10389 p = vim_strchr(address, ':');
10390 if (p == NULL)
10391 {
10392 EMSG2(_(e_invarg2), address);
10393 return;
10394 }
10395 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010396 port = strtol((char *)p, &rest, 10);
10397 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010398 {
10399 p[-1] = ':';
10400 EMSG2(_(e_invarg2), address);
10401 return;
10402 }
10403
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010404 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010405 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010406 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010407 opt.jo_timeout = 2000;
10408 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010409 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010410 return;
10411 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010412 {
10413 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010414 return;
10415 }
10416
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010417 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010418 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010419 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010420 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010421 opt.jo_set = JO_ALL;
10422 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010423 }
10424}
10425
10426/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010427 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010428 */
10429 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010430common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010431{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010432 channel_T *channel;
10433 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010434 jobopt_T opt;
10435 int mode;
10436 int timeout;
10437 int id = -1;
10438 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010439
10440 /* return an empty string by default */
10441 rettv->v_type = VAR_STRING;
10442 rettv->vval.v_string = NULL;
10443
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010444 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010445 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10446 == FAIL)
10447 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010448
Bram Moolenaar77073442016-02-13 23:23:53 +010010449 channel = get_channel_arg(&argvars[0]);
10450 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010451 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010452 if (opt.jo_set & JO_PART)
10453 part = opt.jo_part;
10454 else
10455 part = channel_part_read(channel);
10456 mode = channel_get_mode(channel, part);
10457 timeout = channel_get_timeout(channel, part);
10458 if (opt.jo_set & JO_TIMEOUT)
10459 timeout = opt.jo_timeout;
10460
10461 if (raw || mode == MODE_RAW || mode == MODE_NL)
10462 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10463 else
10464 {
10465 if (opt.jo_set & JO_ID)
10466 id = opt.jo_id;
10467 channel_read_json_block(channel, part, timeout, id, &listtv);
10468 if (listtv != NULL)
10469 *rettv = *listtv;
10470 else
10471 {
10472 rettv->v_type = VAR_SPECIAL;
10473 rettv->vval.v_number = VVAL_NONE;
10474 }
10475 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010476 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010477}
10478
10479/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010480 * "ch_read()" function
10481 */
10482 static void
10483f_ch_read(typval_T *argvars, typval_T *rettv)
10484{
10485 common_channel_read(argvars, rettv, FALSE);
10486}
10487
10488/*
10489 * "ch_readraw()" function
10490 */
10491 static void
10492f_ch_readraw(typval_T *argvars, typval_T *rettv)
10493{
10494 common_channel_read(argvars, rettv, TRUE);
10495}
10496
10497/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010498 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010499 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010500 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010501 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010502 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010503 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010504send_common(
10505 typval_T *argvars,
10506 char_u *text,
10507 int id,
10508 int eval,
10509 char *fun,
10510 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010511{
Bram Moolenaar77073442016-02-13 23:23:53 +010010512 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010513 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010514 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010515
Bram Moolenaar77073442016-02-13 23:23:53 +010010516 channel = get_channel_arg(&argvars[0]);
10517 if (channel == NULL)
10518 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010519 part_send = channel_part_send(channel);
10520 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010521
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010522 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010523 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10524 return NULL;
10525
Bram Moolenaara07fec92016-02-05 21:04:08 +010010526 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010527 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10528 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010529 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010530 {
10531 if (eval)
10532 {
10533 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10534 return NULL;
10535 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010536 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010537 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010538
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010539 if (channel_send(channel, part_send, text, fun) == OK
10540 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010541 return channel;
10542 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010543}
10544
10545/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010546 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010547 */
10548 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010549ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010550{
10551 char_u *text;
10552 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010553 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010554 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010555 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010556 int part_send;
10557 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010558 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010559
10560 /* return an empty string by default */
10561 rettv->v_type = VAR_STRING;
10562 rettv->vval.v_string = NULL;
10563
Bram Moolenaar77073442016-02-13 23:23:53 +010010564 channel = get_channel_arg(&argvars[0]);
10565 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010566 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010567 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010568
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010569 ch_mode = channel_get_mode(channel, part_send);
10570 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010571 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010572 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010573 return;
10574 }
10575
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010576 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010577 text = json_encode_nr_expr(id, &argvars[1],
10578 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010579 if (text == NULL)
10580 return;
10581
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010582 channel = send_common(argvars, text, id, eval,
10583 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010584 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010585 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010586 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010587 /* TODO: timeout from options */
10588 timeout = channel_get_timeout(channel, part_read);
10589 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10590 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010591 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010592 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010593
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010594 /* Move the item from the list and then change the type to
10595 * avoid the value being freed. */
10596 *rettv = list->lv_last->li_tv;
10597 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010598 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010599 }
10600 }
10601}
10602
10603/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010604 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010605 */
10606 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010607f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10608{
10609 ch_expr_common(argvars, rettv, TRUE);
10610}
10611
10612/*
10613 * "ch_sendexpr()" function
10614 */
10615 static void
10616f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10617{
10618 ch_expr_common(argvars, rettv, FALSE);
10619}
10620
10621/*
10622 * common for "ch_evalraw()" and "ch_sendraw()"
10623 */
10624 static void
10625ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010626{
10627 char_u buf[NUMBUFLEN];
10628 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010629 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010630 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010631 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010632
10633 /* return an empty string by default */
10634 rettv->v_type = VAR_STRING;
10635 rettv->vval.v_string = NULL;
10636
10637 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010638 channel = send_common(argvars, text, 0, eval,
10639 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10640 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010641 {
10642 /* TODO: timeout from options */
10643 timeout = channel_get_timeout(channel, part_read);
10644 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10645 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010646}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010647
10648/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010649 * "ch_evalraw()" function
10650 */
10651 static void
10652f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10653{
10654 ch_raw_common(argvars, rettv, TRUE);
10655}
10656
10657/*
10658 * "ch_sendraw()" function
10659 */
10660 static void
10661f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10662{
10663 ch_raw_common(argvars, rettv, FALSE);
10664}
10665
10666/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010667 * "ch_setoptions()" function
10668 */
10669 static void
10670f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10671{
10672 channel_T *channel;
10673 jobopt_T opt;
10674
10675 channel = get_channel_arg(&argvars[0]);
10676 if (channel == NULL)
10677 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010678 clear_job_options(&opt);
10679 if (get_job_options(&argvars[1], &opt,
10680 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010681 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010682 channel_set_options(channel, &opt);
10683}
10684
10685/*
10686 * "ch_status()" function
10687 */
10688 static void
10689f_ch_status(typval_T *argvars, typval_T *rettv)
10690{
10691 /* return an empty string by default */
10692 rettv->v_type = VAR_STRING;
10693
10694 if (argvars[0].v_type != VAR_CHANNEL)
10695 {
10696 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10697 rettv->vval.v_string = NULL;
10698 }
10699 else
10700 rettv->vval.v_string = vim_strsave(
10701 (char_u *)channel_status(argvars[0].vval.v_channel));
10702}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010703#endif
10704
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010705/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010706 * "changenr()" function
10707 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010709f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010710{
10711 rettv->vval.v_number = curbuf->b_u_seq_cur;
10712}
10713
10714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 * "char2nr(string)" function
10716 */
10717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719{
10720#ifdef FEAT_MBYTE
10721 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010722 {
10723 int utf8 = 0;
10724
10725 if (argvars[1].v_type != VAR_UNKNOWN)
10726 utf8 = get_tv_number_chk(&argvars[1], NULL);
10727
10728 if (utf8)
10729 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10730 else
10731 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 else
10734#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736}
10737
10738/*
10739 * "cindent(lnum)" function
10740 */
10741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010742f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743{
10744#ifdef FEAT_CINDENT
10745 pos_T pos;
10746 linenr_T lnum;
10747
10748 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10751 {
10752 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 curwin->w_cursor = pos;
10755 }
10756 else
10757#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759}
10760
10761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010762 * "clearmatches()" function
10763 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010765f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010766{
10767#ifdef FEAT_SEARCH_EXTRA
10768 clear_matches(curwin);
10769#endif
10770}
10771
10772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 * "col(string)" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 colnr_T col = 0;
10779 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010780 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010782 fp = var2fpos(&argvars[0], FALSE, &fnum);
10783 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 {
10785 if (fp->col == MAXCOL)
10786 {
10787 /* '> can be MAXCOL, get the length of the line then */
10788 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010789 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 else
10791 col = MAXCOL;
10792 }
10793 else
10794 {
10795 col = fp->col + 1;
10796#ifdef FEAT_VIRTUALEDIT
10797 /* col(".") when the cursor is on the NUL at the end of the line
10798 * because of "coladd" can be seen as an extra column. */
10799 if (virtual_active() && fp == &curwin->w_cursor)
10800 {
10801 char_u *p = ml_get_cursor();
10802
10803 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10804 curwin->w_virtcol - curwin->w_cursor.coladd))
10805 {
10806# ifdef FEAT_MBYTE
10807 int l;
10808
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010809 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 col += l;
10811# else
10812 if (*p != NUL && p[1] == NUL)
10813 ++col;
10814# endif
10815 }
10816 }
10817#endif
10818 }
10819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821}
10822
Bram Moolenaar572cb562005-08-05 21:35:02 +000010823#if defined(FEAT_INS_EXPAND)
10824/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010825 * "complete()" function
10826 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010829{
10830 int startcol;
10831
10832 if ((State & INSERT) == 0)
10833 {
10834 EMSG(_("E785: complete() can only be used in Insert mode"));
10835 return;
10836 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010837
10838 /* Check for undo allowed here, because if something was already inserted
10839 * the line was already saved for undo and this check isn't done. */
10840 if (!undo_allowed())
10841 return;
10842
Bram Moolenaarade00832006-03-10 21:46:58 +000010843 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10844 {
10845 EMSG(_(e_invarg));
10846 return;
10847 }
10848
10849 startcol = get_tv_number_chk(&argvars[0], NULL);
10850 if (startcol <= 0)
10851 return;
10852
10853 set_completion(startcol - 1, argvars[1].vval.v_list);
10854}
10855
10856/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010857 * "complete_add()" function
10858 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010860f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010861{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010862 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010863}
10864
10865/*
10866 * "complete_check()" function
10867 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010869f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010870{
10871 int saved = RedrawingDisabled;
10872
10873 RedrawingDisabled = 0;
10874 ins_compl_check_keys(0);
10875 rettv->vval.v_number = compl_interrupted;
10876 RedrawingDisabled = saved;
10877}
10878#endif
10879
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880/*
10881 * "confirm(message, buttons[, default [, type]])" function
10882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010884f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10887 char_u *message;
10888 char_u *buttons = NULL;
10889 char_u buf[NUMBUFLEN];
10890 char_u buf2[NUMBUFLEN];
10891 int def = 1;
10892 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 char_u *typestr;
10894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 message = get_tv_string_chk(&argvars[0]);
10897 if (message == NULL)
10898 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010899 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10902 if (buttons == NULL)
10903 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010904 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010907 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10910 if (typestr == NULL)
10911 error = TRUE;
10912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 switch (TOUPPER_ASC(*typestr))
10915 {
10916 case 'E': type = VIM_ERROR; break;
10917 case 'Q': type = VIM_QUESTION; break;
10918 case 'I': type = VIM_INFO; break;
10919 case 'W': type = VIM_WARNING; break;
10920 case 'G': type = VIM_GENERIC; break;
10921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 }
10923 }
10924 }
10925 }
10926
10927 if (buttons == NULL || *buttons == NUL)
10928 buttons = (char_u *)_("&Ok");
10929
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010930 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010932 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933#endif
10934}
10935
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010936/*
10937 * "copy()" function
10938 */
10939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010940f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010941{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010942 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010945#ifdef FEAT_FLOAT
10946/*
10947 * "cos()" function
10948 */
10949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010950f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010951{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010952 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010953
10954 rettv->v_type = VAR_FLOAT;
10955 if (get_float_arg(argvars, &f) == OK)
10956 rettv->vval.v_float = cos(f);
10957 else
10958 rettv->vval.v_float = 0.0;
10959}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010960
10961/*
10962 * "cosh()" function
10963 */
10964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010965f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010966{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010967 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010968
10969 rettv->v_type = VAR_FLOAT;
10970 if (get_float_arg(argvars, &f) == OK)
10971 rettv->vval.v_float = cosh(f);
10972 else
10973 rettv->vval.v_float = 0.0;
10974}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010975#endif
10976
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010978 * "count()" function
10979 */
10980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010981f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010982{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010983 long n = 0;
10984 int ic = FALSE;
10985
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 listitem_T *li;
10989 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010990 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010991
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if ((l = argvars[0].vval.v_list) != NULL)
10993 {
10994 li = l->lv_first;
10995 if (argvars[2].v_type != VAR_UNKNOWN)
10996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 int error = FALSE;
10998
10999 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 if (argvars[3].v_type != VAR_UNKNOWN)
11001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 idx = get_tv_number_chk(&argvars[3], &error);
11003 if (!error)
11004 {
11005 li = list_find(l, idx);
11006 if (li == NULL)
11007 EMSGN(_(e_listidx), idx);
11008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011010 if (error)
11011 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 }
11013
11014 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011015 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011016 ++n;
11017 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011018 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 else if (argvars[0].v_type == VAR_DICT)
11020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 int todo;
11022 dict_T *d;
11023 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011025 if ((d = argvars[0].vval.v_dict) != NULL)
11026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011027 int error = FALSE;
11028
Bram Moolenaare9a41262005-01-15 22:18:47 +000011029 if (argvars[2].v_type != VAR_UNKNOWN)
11030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011032 if (argvars[3].v_type != VAR_UNKNOWN)
11033 EMSG(_(e_invarg));
11034 }
11035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011036 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011038 {
11039 if (!HASHITEM_EMPTY(hi))
11040 {
11041 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011042 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011043 ++n;
11044 }
11045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 }
11047 }
11048 else
11049 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011050 rettv->vval.v_number = n;
11051}
11052
11053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11055 *
11056 * Checks the existence of a cscope connection.
11057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011059f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061#ifdef FEAT_CSCOPE
11062 int num = 0;
11063 char_u *dbpath = NULL;
11064 char_u *prepend = NULL;
11065 char_u buf[NUMBUFLEN];
11066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011067 if (argvars[0].v_type != VAR_UNKNOWN
11068 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 num = (int)get_tv_number(&argvars[0]);
11071 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 }
11075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077#endif
11078}
11079
11080/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011081 * "cursor(lnum, col)" function, or
11082 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011084 * Moves the cursor to the specified line and column.
11085 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011088f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011091#ifdef FEAT_VIRTUALEDIT
11092 long coladd = 0;
11093#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011094 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011096 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011097 if (argvars[1].v_type == VAR_UNKNOWN)
11098 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011099 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011100 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011101
Bram Moolenaar493c1782014-05-28 14:34:46 +020011102 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011103 {
11104 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011105 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011106 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011107 line = pos.lnum;
11108 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011109#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011110 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011111#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011112 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011113 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011114 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011115 set_curswant = FALSE;
11116 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011117 }
11118 else
11119 {
11120 line = get_tv_lnum(argvars);
11121 col = get_tv_number_chk(&argvars[1], NULL);
11122#ifdef FEAT_VIRTUALEDIT
11123 if (argvars[2].v_type != VAR_UNKNOWN)
11124 coladd = get_tv_number_chk(&argvars[2], NULL);
11125#endif
11126 }
11127 if (line < 0 || col < 0
11128#ifdef FEAT_VIRTUALEDIT
11129 || coladd < 0
11130#endif
11131 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 if (line > 0)
11134 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 if (col > 0)
11136 curwin->w_cursor.col = col - 1;
11137#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011138 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139#endif
11140
11141 /* Make sure the cursor is in a valid position. */
11142 check_cursor();
11143#ifdef FEAT_MBYTE
11144 /* Correct cursor for multi-byte character. */
11145 if (has_mbyte)
11146 mb_adjust_cursor();
11147#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011148
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011149 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011150 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151}
11152
11153/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 */
11156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011157f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011159 int noref = 0;
11160
11161 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011163 if (noref < 0 || noref > 1)
11164 EMSG(_(e_invarg));
11165 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011166 {
11167 current_copyID += COPYID_INC;
11168 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170}
11171
11172/*
11173 * "delete()" function
11174 */
11175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011176f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011178 char_u nbuf[NUMBUFLEN];
11179 char_u *name;
11180 char_u *flags;
11181
11182 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011184 return;
11185
11186 name = get_tv_string(&argvars[0]);
11187 if (name == NULL || *name == NUL)
11188 {
11189 EMSG(_(e_invarg));
11190 return;
11191 }
11192
11193 if (argvars[1].v_type != VAR_UNKNOWN)
11194 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011196 flags = (char_u *)"";
11197
11198 if (*flags == NUL)
11199 /* delete a file */
11200 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11201 else if (STRCMP(flags, "d") == 0)
11202 /* delete an empty directory */
11203 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11204 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011205 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011206 rettv->vval.v_number = delete_recursive(name);
11207 else
11208 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209}
11210
11211/*
11212 * "did_filetype()" function
11213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011215f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216{
11217#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219#endif
11220}
11221
11222/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011223 * "diff_filler()" function
11224 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011226f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011227{
11228#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011230#endif
11231}
11232
11233/*
11234 * "diff_hlID()" function
11235 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011238{
11239#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011241 static linenr_T prev_lnum = 0;
11242 static int changedtick = 0;
11243 static int fnum = 0;
11244 static int change_start = 0;
11245 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011246 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011247 int filler_lines;
11248 int col;
11249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 if (lnum < 0) /* ignore type error in {lnum} arg */
11251 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011252 if (lnum != prev_lnum
11253 || changedtick != curbuf->b_changedtick
11254 || fnum != curbuf->b_fnum)
11255 {
11256 /* New line, buffer, change: need to get the values. */
11257 filler_lines = diff_check(curwin, lnum);
11258 if (filler_lines < 0)
11259 {
11260 if (filler_lines == -1)
11261 {
11262 change_start = MAXCOL;
11263 change_end = -1;
11264 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11265 hlID = HLF_ADD; /* added line */
11266 else
11267 hlID = HLF_CHD; /* changed line */
11268 }
11269 else
11270 hlID = HLF_ADD; /* added line */
11271 }
11272 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011273 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011274 prev_lnum = lnum;
11275 changedtick = curbuf->b_changedtick;
11276 fnum = curbuf->b_fnum;
11277 }
11278
11279 if (hlID == HLF_CHD || hlID == HLF_TXD)
11280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011281 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011282 if (col >= change_start && col <= change_end)
11283 hlID = HLF_TXD; /* changed text */
11284 else
11285 hlID = HLF_CHD; /* changed line */
11286 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011287 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011288#endif
11289}
11290
11291/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011292 * "disable_char_avail_for_testing({expr})" function
11293 */
11294 static void
11295f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11296{
11297 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11298}
11299
11300/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011301 * "empty({expr})" function
11302 */
11303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011304f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011305{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011306 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011307
11308 switch (argvars[0].v_type)
11309 {
11310 case VAR_STRING:
11311 case VAR_FUNC:
11312 n = argvars[0].vval.v_string == NULL
11313 || *argvars[0].vval.v_string == NUL;
11314 break;
11315 case VAR_NUMBER:
11316 n = argvars[0].vval.v_number == 0;
11317 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011318 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011319#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011320 n = argvars[0].vval.v_float == 0.0;
11321 break;
11322#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011323 case VAR_LIST:
11324 n = argvars[0].vval.v_list == NULL
11325 || argvars[0].vval.v_list->lv_first == NULL;
11326 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327 case VAR_DICT:
11328 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011330 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011331 case VAR_SPECIAL:
11332 n = argvars[0].vval.v_number != VVAL_TRUE;
11333 break;
11334
Bram Moolenaar835dc632016-02-07 14:27:38 +010011335 case VAR_JOB:
11336#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011337 n = argvars[0].vval.v_job == NULL
11338 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11339 break;
11340#endif
11341 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011342#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011343 n = argvars[0].vval.v_channel == NULL
11344 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011345 break;
11346#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011347 case VAR_UNKNOWN:
11348 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11349 n = TRUE;
11350 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011351 }
11352
11353 rettv->vval.v_number = n;
11354}
11355
11356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 * "escape({string}, {chars})" function
11358 */
11359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011360f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
11362 char_u buf[NUMBUFLEN];
11363
Bram Moolenaar758711c2005-02-02 23:11:38 +000011364 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11365 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367}
11368
11369/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370 * "eval()" function
11371 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011373f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011374{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011375 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 s = get_tv_string_chk(&argvars[0]);
11378 if (s != NULL)
11379 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011380
Bram Moolenaar615b9972015-01-14 17:15:05 +010011381 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11383 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011384 if (p != NULL && !aborting())
11385 EMSG2(_(e_invexpr2), p);
11386 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011388 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011390 else if (*s != NUL)
11391 EMSG(_(e_trailing));
11392}
11393
11394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 * "eventhandler()" function
11396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011398f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
11404 * "executable()" function
11405 */
11406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011409 char_u *name = get_tv_string(&argvars[0]);
11410
11411 /* Check in $PATH and also check directly if there is a directory name. */
11412 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11413 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011414}
11415
11416/*
11417 * "exepath()" function
11418 */
11419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011420f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011421{
11422 char_u *p = NULL;
11423
Bram Moolenaarb5971142015-03-21 17:32:19 +010011424 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011425 rettv->v_type = VAR_STRING;
11426 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427}
11428
11429/*
11430 * "exists()" function
11431 */
11432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011433f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
11435 char_u *p;
11436 char_u *name;
11437 int n = FALSE;
11438 int len = 0;
11439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 if (*p == '$') /* environment variable */
11442 {
11443 /* first try "normal" environment variables (fast) */
11444 if (mch_getenv(p + 1) != NULL)
11445 n = TRUE;
11446 else
11447 {
11448 /* try expanding things like $VIM and ${HOME} */
11449 p = expand_env_save(p);
11450 if (p != NULL && *p != '$')
11451 n = TRUE;
11452 vim_free(p);
11453 }
11454 }
11455 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011458 if (*skipwhite(p) != NUL)
11459 n = FALSE; /* trailing garbage */
11460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 else if (*p == '*') /* internal or user defined function */
11462 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011463 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 }
11465 else if (*p == ':')
11466 {
11467 n = cmd_exists(p + 1);
11468 }
11469 else if (*p == '#')
11470 {
11471#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011472 if (p[1] == '#')
11473 n = autocmd_supported(p + 2);
11474 else
11475 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476#endif
11477 }
11478 else /* internal variable */
11479 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011480 char_u *tofree;
11481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011483 /* get_name_len() takes care of expanding curly braces */
11484 name = p;
11485 len = get_name_len(&p, &tofree, TRUE, FALSE);
11486 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011488 if (tofree != NULL)
11489 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011490 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011491 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011493 /* handle d.key, l[idx], f(expr) */
11494 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11495 if (n)
11496 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 }
11498 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011499 if (*p != NUL)
11500 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011502 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 }
11504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506}
11507
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011508#ifdef FEAT_FLOAT
11509/*
11510 * "exp()" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011514{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011515 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011516
11517 rettv->v_type = VAR_FLOAT;
11518 if (get_float_arg(argvars, &f) == OK)
11519 rettv->vval.v_float = exp(f);
11520 else
11521 rettv->vval.v_float = 0.0;
11522}
11523#endif
11524
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525/*
11526 * "expand()" function
11527 */
11528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011529f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 char_u *s;
11532 int len;
11533 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011534 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011537 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011540 if (argvars[1].v_type != VAR_UNKNOWN
11541 && argvars[2].v_type != VAR_UNKNOWN
11542 && get_tv_number_chk(&argvars[2], &error)
11543 && !error)
11544 {
11545 rettv->v_type = VAR_LIST;
11546 rettv->vval.v_list = NULL;
11547 }
11548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 if (*s == '%' || *s == '#' || *s == '<')
11551 {
11552 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011553 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011555 if (rettv->v_type == VAR_LIST)
11556 {
11557 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11558 list_append_string(rettv->vval.v_list, result, -1);
11559 else
11560 vim_free(result);
11561 }
11562 else
11563 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
11565 else
11566 {
11567 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011568 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 if (argvars[1].v_type != VAR_UNKNOWN
11570 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011571 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 if (!error)
11573 {
11574 ExpandInit(&xpc);
11575 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011576 if (p_wic)
11577 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011578 if (rettv->v_type == VAR_STRING)
11579 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11580 options, WILD_ALL);
11581 else if (rettv_list_alloc(rettv) != FAIL)
11582 {
11583 int i;
11584
11585 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11586 for (i = 0; i < xpc.xp_numfiles; i++)
11587 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11588 ExpandCleanup(&xpc);
11589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 }
11591 else
11592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 }
11594}
11595
11596/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011597 * Go over all entries in "d2" and add them to "d1".
11598 * When "action" is "error" then a duplicate key is an error.
11599 * When "action" is "force" then a duplicate key is overwritten.
11600 * Otherwise duplicate keys are ignored ("action" is "keep").
11601 */
11602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011604{
11605 dictitem_T *di1;
11606 hashitem_T *hi2;
11607 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011608 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011609
11610 todo = (int)d2->dv_hashtab.ht_used;
11611 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11612 {
11613 if (!HASHITEM_EMPTY(hi2))
11614 {
11615 --todo;
11616 di1 = dict_find(d1, hi2->hi_key, -1);
11617 if (d1->dv_scope != 0)
11618 {
11619 /* Disallow replacing a builtin function in l: and g:.
11620 * Check the key to be valid when adding to any
11621 * scope. */
11622 if (d1->dv_scope == VAR_DEF_SCOPE
11623 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11624 && var_check_func_name(hi2->hi_key,
11625 di1 == NULL))
11626 break;
11627 if (!valid_varname(hi2->hi_key))
11628 break;
11629 }
11630 if (di1 == NULL)
11631 {
11632 di1 = dictitem_copy(HI2DI(hi2));
11633 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11634 dictitem_free(di1);
11635 }
11636 else if (*action == 'e')
11637 {
11638 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11639 break;
11640 }
11641 else if (*action == 'f' && HI2DI(hi2) != di1)
11642 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011643 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11644 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011645 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011646 clear_tv(&di1->di_tv);
11647 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11648 }
11649 }
11650 }
11651}
11652
11653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011654 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011655 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011656 */
11657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011658f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011659{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011660 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011661
Bram Moolenaare9a41262005-01-15 22:18:47 +000011662 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011663 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011664 list_T *l1, *l2;
11665 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011666 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011668
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 l1 = argvars[0].vval.v_list;
11670 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011671 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011672 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011673 {
11674 if (argvars[2].v_type != VAR_UNKNOWN)
11675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 before = get_tv_number_chk(&argvars[2], &error);
11677 if (error)
11678 return; /* type error; errmsg already given */
11679
Bram Moolenaar758711c2005-02-02 23:11:38 +000011680 if (before == l1->lv_len)
11681 item = NULL;
11682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011684 item = list_find(l1, before);
11685 if (item == NULL)
11686 {
11687 EMSGN(_(e_listidx), before);
11688 return;
11689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011690 }
11691 }
11692 else
11693 item = NULL;
11694 list_extend(l1, l2, item);
11695
Bram Moolenaare9a41262005-01-15 22:18:47 +000011696 copy_tv(&argvars[0], rettv);
11697 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011698 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11700 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011701 dict_T *d1, *d2;
11702 char_u *action;
11703 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011704
11705 d1 = argvars[0].vval.v_dict;
11706 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011707 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011708 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 {
11710 /* Check the third argument. */
11711 if (argvars[2].v_type != VAR_UNKNOWN)
11712 {
11713 static char *(av[]) = {"keep", "force", "error"};
11714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 action = get_tv_string_chk(&argvars[2]);
11716 if (action == NULL)
11717 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 for (i = 0; i < 3; ++i)
11719 if (STRCMP(action, av[i]) == 0)
11720 break;
11721 if (i == 3)
11722 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011723 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011724 return;
11725 }
11726 }
11727 else
11728 action = (char_u *)"force";
11729
Bram Moolenaara9922d62013-05-30 13:01:18 +020011730 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011731
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 copy_tv(&argvars[0], rettv);
11733 }
11734 }
11735 else
11736 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011737}
11738
11739/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011740 * "feedkeys()" function
11741 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011743f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011744{
11745 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011746 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011747 char_u *keys, *flags;
11748 char_u nbuf[NUMBUFLEN];
11749 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011750 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011751 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011752
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011753 /* This is not allowed in the sandbox. If the commands would still be
11754 * executed in the sandbox it would be OK, but it probably happens later,
11755 * when "sandbox" is no longer set. */
11756 if (check_secure())
11757 return;
11758
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011759 keys = get_tv_string(&argvars[0]);
11760 if (*keys != NUL)
11761 {
11762 if (argvars[1].v_type != VAR_UNKNOWN)
11763 {
11764 flags = get_tv_string_buf(&argvars[1], nbuf);
11765 for ( ; *flags != NUL; ++flags)
11766 {
11767 switch (*flags)
11768 {
11769 case 'n': remap = FALSE; break;
11770 case 'm': remap = TRUE; break;
11771 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011772 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011773 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011774 }
11775 }
11776 }
11777
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011778 /* Need to escape K_SPECIAL and CSI before putting the string in the
11779 * typeahead buffer. */
11780 keys_esc = vim_strsave_escape_csi(keys);
11781 if (keys_esc != NULL)
11782 {
11783 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011784 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011785 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011786 if (vgetc_busy)
11787 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011788 if (execute)
11789 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011790 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011791 }
11792}
11793
11794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 * "filereadable()" function
11796 */
11797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011798f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011800 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 char_u *p;
11802 int n;
11803
Bram Moolenaarc236c162008-07-13 17:41:49 +000011804#ifndef O_NONBLOCK
11805# define O_NONBLOCK 0
11806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011808 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11809 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 {
11811 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011812 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 }
11814 else
11815 n = FALSE;
11816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818}
11819
11820/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011821 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 * rights to write into.
11823 */
11824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011825f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011827 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828}
11829
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011830 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011831findfilendir(
11832 typval_T *argvars UNUSED,
11833 typval_T *rettv,
11834 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011835{
11836#ifdef FEAT_SEARCHPATH
11837 char_u *fname;
11838 char_u *fresult = NULL;
11839 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11840 char_u *p;
11841 char_u pathbuf[NUMBUFLEN];
11842 int count = 1;
11843 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011844 int error = FALSE;
11845#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011846
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011847 rettv->vval.v_string = NULL;
11848 rettv->v_type = VAR_STRING;
11849
11850#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11856 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011857 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 else
11859 {
11860 if (*p != NUL)
11861 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011864 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011866 }
11867
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011868 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11869 error = TRUE;
11870
11871 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 do
11874 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011875 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011876 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 fresult = find_file_in_path_option(first ? fname : NULL,
11878 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011879 0, first, path,
11880 find_what,
11881 curbuf->b_ffname,
11882 find_what == FINDFILE_DIR
11883 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011885
11886 if (fresult != NULL && rettv->v_type == VAR_LIST)
11887 list_append_string(rettv->vval.v_list, fresult, -1);
11888
11889 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011891
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011892 if (rettv->v_type == VAR_STRING)
11893 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011894#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011895}
11896
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011897static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11898static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011899
11900/*
11901 * Implementation of map() and filter().
11902 */
11903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011904filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011905{
11906 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011907 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 listitem_T *li, *nli;
11909 list_T *l = NULL;
11910 dictitem_T *di;
11911 hashtab_T *ht;
11912 hashitem_T *hi;
11913 dict_T *d = NULL;
11914 typval_T save_val;
11915 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011916 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011917 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011918 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011919 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011920 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011921 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011922 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923
Bram Moolenaare9a41262005-01-15 22:18:47 +000011924 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011925 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011926 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011927 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011928 return;
11929 }
11930 else if (argvars[0].v_type == VAR_DICT)
11931 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011932 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011933 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011934 return;
11935 }
11936 else
11937 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011938 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011939 return;
11940 }
11941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011942 expr = get_tv_string_buf_chk(&argvars[1], buf);
11943 /* On type errors, the preceding call has already displayed an error
11944 * message. Avoid a misleading error message for an empty string that
11945 * was not passed as argument. */
11946 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 prepare_vimvar(VV_VAL, &save_val);
11949 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011951 /* We reset "did_emsg" to be able to detect whether an error
11952 * occurred during evaluation of the expression. */
11953 save_did_emsg = did_emsg;
11954 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011955
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011956 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 vimvars[VV_KEY].vv_type = VAR_STRING;
11960
11961 ht = &d->dv_hashtab;
11962 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011963 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 if (!HASHITEM_EMPTY(hi))
11967 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011968 int r;
11969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 --todo;
11971 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011972 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011973 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11974 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 break;
11976 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011977 r = filter_map_one(&di->di_tv, expr, map, &rem);
11978 clear_tv(&vimvars[VV_KEY].vv_tv);
11979 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 break;
11981 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011982 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011983 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11984 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011985 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011987 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 }
11989 }
11990 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011991 }
11992 else
11993 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011994 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011996 for (li = l->lv_first; li != NULL; li = nli)
11997 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011998 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011999 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012001 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012002 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012003 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012004 break;
12005 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012006 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012007 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012008 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012010
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012011 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012012 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012013
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012014 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016
12017 copy_tv(&argvars[0], rettv);
12018}
12019
12020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012021filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012022{
Bram Moolenaar33570922005-01-25 22:26:29 +000012023 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012024 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012025 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012026
Bram Moolenaar33570922005-01-25 22:26:29 +000012027 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012028 s = expr;
12029 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012030 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 if (*s != NUL) /* check for trailing chars after expr */
12032 {
12033 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012034 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012035 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036 }
12037 if (map)
12038 {
12039 /* map(): replace the list item value */
12040 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012041 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012042 *tv = rettv;
12043 }
12044 else
12045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 int error = FALSE;
12047
Bram Moolenaare9a41262005-01-15 22:18:47 +000012048 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 /* On type error, nothing has been removed; return FAIL to stop the
12052 * loop. The error message was given by get_tv_number_chk(). */
12053 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012054 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012055 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012056 retval = OK;
12057theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012059 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060}
12061
12062/*
12063 * "filter()" function
12064 */
12065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012066f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012067{
12068 filter_map(argvars, rettv, FALSE);
12069}
12070
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072 * "finddir({fname}[, {path}[, {count}]])" function
12073 */
12074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012075f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012077 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012078}
12079
12080/*
12081 * "findfile({fname}[, {path}[, {count}]])" function
12082 */
12083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012084f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012086 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012087}
12088
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012089#ifdef FEAT_FLOAT
12090/*
12091 * "float2nr({float})" function
12092 */
12093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012094f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012095{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012096 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012097
12098 if (get_float_arg(argvars, &f) == OK)
12099 {
12100 if (f < -0x7fffffff)
12101 rettv->vval.v_number = -0x7fffffff;
12102 else if (f > 0x7fffffff)
12103 rettv->vval.v_number = 0x7fffffff;
12104 else
12105 rettv->vval.v_number = (varnumber_T)f;
12106 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012107}
12108
12109/*
12110 * "floor({float})" function
12111 */
12112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012113f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012114{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012115 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012116
12117 rettv->v_type = VAR_FLOAT;
12118 if (get_float_arg(argvars, &f) == OK)
12119 rettv->vval.v_float = floor(f);
12120 else
12121 rettv->vval.v_float = 0.0;
12122}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012123
12124/*
12125 * "fmod()" function
12126 */
12127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012128f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012129{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012130 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012131
12132 rettv->v_type = VAR_FLOAT;
12133 if (get_float_arg(argvars, &fx) == OK
12134 && get_float_arg(&argvars[1], &fy) == OK)
12135 rettv->vval.v_float = fmod(fx, fy);
12136 else
12137 rettv->vval.v_float = 0.0;
12138}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012139#endif
12140
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012142 * "fnameescape({string})" function
12143 */
12144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012145f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012146{
12147 rettv->vval.v_string = vim_strsave_fnameescape(
12148 get_tv_string(&argvars[0]), FALSE);
12149 rettv->v_type = VAR_STRING;
12150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "fnamemodify({fname}, {mods})" function
12154 */
12155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 char_u *fname;
12159 char_u *mods;
12160 int usedlen = 0;
12161 int len;
12162 char_u *fbuf = NULL;
12163 char_u buf[NUMBUFLEN];
12164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012165 fname = get_tv_string_chk(&argvars[0]);
12166 mods = get_tv_string_buf_chk(&argvars[1], buf);
12167 if (fname == NULL || mods == NULL)
12168 fname = NULL;
12169 else
12170 {
12171 len = (int)STRLEN(fname);
12172 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180 vim_free(fbuf);
12181}
12182
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012183static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184
12185/*
12186 * "foldclosed()" function
12187 */
12188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189foldclosed_both(
12190 typval_T *argvars UNUSED,
12191 typval_T *rettv,
12192 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
12194#ifdef FEAT_FOLDING
12195 linenr_T lnum;
12196 linenr_T first, last;
12197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12200 {
12201 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12202 {
12203 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 return;
12208 }
12209 }
12210#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212}
12213
12214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012215 * "foldclosed()" function
12216 */
12217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012218f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012219{
12220 foldclosed_both(argvars, rettv, FALSE);
12221}
12222
12223/*
12224 * "foldclosedend()" function
12225 */
12226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012227f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228{
12229 foldclosed_both(argvars, rettv, TRUE);
12230}
12231
12232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 * "foldlevel()" function
12234 */
12235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012236f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238#ifdef FEAT_FOLDING
12239 linenr_T lnum;
12240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245}
12246
12247/*
12248 * "foldtext()" function
12249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252{
12253#ifdef FEAT_FOLDING
12254 linenr_T lnum;
12255 char_u *s;
12256 char_u *r;
12257 int len;
12258 char *txt;
12259#endif
12260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->v_type = VAR_STRING;
12262 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012264 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12265 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12266 <= curbuf->b_ml.ml_line_count
12267 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 {
12269 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012270 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12271 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 {
12273 if (!linewhite(lnum))
12274 break;
12275 ++lnum;
12276 }
12277
12278 /* Find interesting text in this line. */
12279 s = skipwhite(ml_get(lnum));
12280 /* skip C comment-start */
12281 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012284 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012286 {
12287 s = skipwhite(ml_get(lnum + 1));
12288 if (*s == '*')
12289 s = skipwhite(s + 1);
12290 }
12291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 txt = _("+-%s%3ld lines: ");
12293 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012294 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 + 20 /* for %3ld */
12296 + STRLEN(s))); /* concatenated */
12297 if (r != NULL)
12298 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012299 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12300 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12301 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302 len = (int)STRLEN(r);
12303 STRCAT(r, s);
12304 /* remove 'foldmarker' and 'commentstring' */
12305 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307 }
12308 }
12309#endif
12310}
12311
12312/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012313 * "foldtextresult(lnum)" function
12314 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012316f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012317{
12318#ifdef FEAT_FOLDING
12319 linenr_T lnum;
12320 char_u *text;
12321 char_u buf[51];
12322 foldinfo_T foldinfo;
12323 int fold_count;
12324#endif
12325
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->v_type = VAR_STRING;
12327 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012328#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012330 /* treat illegal types and illegal string values for {lnum} the same */
12331 if (lnum < 0)
12332 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012333 fold_count = foldedCount(curwin, lnum, &foldinfo);
12334 if (fold_count > 0)
12335 {
12336 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12337 &foldinfo, buf);
12338 if (text == buf)
12339 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012341 }
12342#endif
12343}
12344
12345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 * "foreground()" function
12347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012349f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_GUI
12352 if (gui.in_use)
12353 gui_mch_set_foreground();
12354#else
12355# ifdef WIN32
12356 win32_set_foreground();
12357# endif
12358#endif
12359}
12360
12361/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012362 * "function()" function
12363 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012366{
12367 char_u *s;
12368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012370 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012371 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012372 /* Don't check an autoload name for existence here. */
12373 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012374 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012375 else
12376 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012377 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012378 {
12379 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012380 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012381
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012382 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12383 * also be called from another script. Using trans_function_name()
12384 * would also work, but some plugins depend on the name being
12385 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012386 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012387 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012388 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012389 if (rettv->vval.v_string != NULL)
12390 {
12391 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012392 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012393 }
12394 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012395 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012396 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012398 }
12399}
12400
12401/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012402 * "garbagecollect()" function
12403 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012405f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012406{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012407 /* This is postponed until we are back at the toplevel, because we may be
12408 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12409 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012410
12411 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12412 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012413}
12414
12415/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 * "get()" function
12417 */
12418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012419f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012420{
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 listitem_T *li;
12422 list_T *l;
12423 dictitem_T *di;
12424 dict_T *d;
12425 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012426
Bram Moolenaare9a41262005-01-15 22:18:47 +000012427 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012428 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012429 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 int error = FALSE;
12432
12433 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12434 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012435 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012436 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012437 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012438 else if (argvars[0].v_type == VAR_DICT)
12439 {
12440 if ((d = argvars[0].vval.v_dict) != NULL)
12441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012442 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012443 if (di != NULL)
12444 tv = &di->di_tv;
12445 }
12446 }
12447 else
12448 EMSG2(_(e_listdictarg), "get()");
12449
12450 if (tv == NULL)
12451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012453 copy_tv(&argvars[2], rettv);
12454 }
12455 else
12456 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457}
12458
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012459static 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 +000012460
12461/*
12462 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012463 * Return a range (from start to end) of lines in rettv from the specified
12464 * buffer.
12465 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012466 */
12467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012468get_buffer_lines(
12469 buf_T *buf,
12470 linenr_T start,
12471 linenr_T end,
12472 int retlist,
12473 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012474{
12475 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012476
Bram Moolenaar959a1432013-12-14 12:17:38 +010012477 rettv->v_type = VAR_STRING;
12478 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012479 if (retlist && rettv_list_alloc(rettv) == FAIL)
12480 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012481
12482 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12483 return;
12484
12485 if (!retlist)
12486 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012487 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12488 p = ml_get_buf(buf, start, FALSE);
12489 else
12490 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491 rettv->vval.v_string = vim_strsave(p);
12492 }
12493 else
12494 {
12495 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012496 return;
12497
12498 if (start < 1)
12499 start = 1;
12500 if (end > buf->b_ml.ml_line_count)
12501 end = buf->b_ml.ml_line_count;
12502 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012503 if (list_append_string(rettv->vval.v_list,
12504 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012505 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012506 }
12507}
12508
12509/*
12510 * "getbufline()" function
12511 */
12512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012513f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012514{
12515 linenr_T lnum;
12516 linenr_T end;
12517 buf_T *buf;
12518
12519 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12520 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012521 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012522 --emsg_off;
12523
Bram Moolenaar661b1822005-07-28 22:36:45 +000012524 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012525 if (argvars[2].v_type == VAR_UNKNOWN)
12526 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012527 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012528 end = get_tv_lnum_buf(&argvars[2], buf);
12529
Bram Moolenaar342337a2005-07-21 21:11:17 +000012530 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012531}
12532
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533/*
12534 * "getbufvar()" function
12535 */
12536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012537f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538{
12539 buf_T *buf;
12540 buf_T *save_curbuf;
12541 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012543 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12546 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012548 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012550 rettv->v_type = VAR_STRING;
12551 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552
12553 if (buf != NULL && varname != NULL)
12554 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012555 /* set curbuf to be our buf, temporarily */
12556 save_curbuf = curbuf;
12557 curbuf = buf;
12558
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012560 {
12561 if (get_option_tv(&varname, rettv, TRUE) == OK)
12562 done = TRUE;
12563 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012564 else if (STRCMP(varname, "changedtick") == 0)
12565 {
12566 rettv->v_type = VAR_NUMBER;
12567 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012568 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 else
12571 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012572 /* Look up the variable. */
12573 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12574 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12575 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012578 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012579 done = TRUE;
12580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012582
12583 /* restore previous notion of curbuf */
12584 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 }
12586
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012587 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12588 /* use the default value */
12589 copy_tv(&argvars[2], rettv);
12590
Bram Moolenaar0d660222005-01-07 21:51:51 +000012591 --emsg_off;
12592}
12593
12594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 * "getchar()" function
12596 */
12597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012598f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599{
12600 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012603 /* Position the cursor. Needed after a message that ends in a space. */
12604 windgoto(msg_row, msg_col);
12605
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 ++no_mapping;
12607 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012608 for (;;)
12609 {
12610 if (argvars[0].v_type == VAR_UNKNOWN)
12611 /* getchar(): blocking wait. */
12612 n = safe_vgetc();
12613 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12614 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012615 n = vpeekc_any();
12616 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012617 /* illegal argument or getchar(0) and no char avail: return zero */
12618 n = 0;
12619 else
12620 /* getchar(0) and char avail: return char */
12621 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012622
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012623 if (n == K_IGNORE)
12624 continue;
12625 break;
12626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 --no_mapping;
12628 --allow_keys;
12629
Bram Moolenaar219b8702006-11-01 14:32:36 +000012630 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12631 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12632 vimvars[VV_MOUSE_COL].vv_nr = 0;
12633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 if (IS_SPECIAL(n) || mod_mask != 0)
12636 {
12637 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12638 int i = 0;
12639
12640 /* Turn a special key into three bytes, plus modifier. */
12641 if (mod_mask != 0)
12642 {
12643 temp[i++] = K_SPECIAL;
12644 temp[i++] = KS_MODIFIER;
12645 temp[i++] = mod_mask;
12646 }
12647 if (IS_SPECIAL(n))
12648 {
12649 temp[i++] = K_SPECIAL;
12650 temp[i++] = K_SECOND(n);
12651 temp[i++] = K_THIRD(n);
12652 }
12653#ifdef FEAT_MBYTE
12654 else if (has_mbyte)
12655 i += (*mb_char2bytes)(n, temp + i);
12656#endif
12657 else
12658 temp[i++] = n;
12659 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->v_type = VAR_STRING;
12661 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012662
12663#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012664 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012665 {
12666 int row = mouse_row;
12667 int col = mouse_col;
12668 win_T *win;
12669 linenr_T lnum;
12670# ifdef FEAT_WINDOWS
12671 win_T *wp;
12672# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012673 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012674
12675 if (row >= 0 && col >= 0)
12676 {
12677 /* Find the window at the mouse coordinates and compute the
12678 * text position. */
12679 win = mouse_find_win(&row, &col);
12680 (void)mouse_comp_pos(win, &row, &col, &lnum);
12681# ifdef FEAT_WINDOWS
12682 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012683 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012684# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012685 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012686 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12687 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12688 }
12689 }
12690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 }
12692}
12693
12694/*
12695 * "getcharmod()" function
12696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012698f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701}
12702
12703/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012704 * "getcharsearch()" function
12705 */
12706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012707f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012708{
12709 if (rettv_dict_alloc(rettv) != FAIL)
12710 {
12711 dict_T *dict = rettv->vval.v_dict;
12712
12713 dict_add_nr_str(dict, "char", 0L, last_csearch());
12714 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12715 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12716 }
12717}
12718
12719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 * "getcmdline()" function
12721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012723f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->v_type = VAR_STRING;
12726 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727}
12728
12729/*
12730 * "getcmdpos()" function
12731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012733f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736}
12737
12738/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012739 * "getcmdtype()" function
12740 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012742f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012743{
12744 rettv->v_type = VAR_STRING;
12745 rettv->vval.v_string = alloc(2);
12746 if (rettv->vval.v_string != NULL)
12747 {
12748 rettv->vval.v_string[0] = get_cmdline_type();
12749 rettv->vval.v_string[1] = NUL;
12750 }
12751}
12752
12753/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012754 * "getcmdwintype()" function
12755 */
12756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012757f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012758{
12759 rettv->v_type = VAR_STRING;
12760 rettv->vval.v_string = NULL;
12761#ifdef FEAT_CMDWIN
12762 rettv->vval.v_string = alloc(2);
12763 if (rettv->vval.v_string != NULL)
12764 {
12765 rettv->vval.v_string[0] = cmdwin_type;
12766 rettv->vval.v_string[1] = NUL;
12767 }
12768#endif
12769}
12770
12771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 * "getcwd()" function
12773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012777 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012778 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012781 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012782
12783 wp = find_tabwin(&argvars[0], &argvars[1]);
12784 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012786 if (wp->w_localdir != NULL)
12787 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12788 else if(globaldir != NULL)
12789 rettv->vval.v_string = vim_strsave(globaldir);
12790 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012791 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012792 cwd = alloc(MAXPATHL);
12793 if (cwd != NULL)
12794 {
12795 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12796 rettv->vval.v_string = vim_strsave(cwd);
12797 vim_free(cwd);
12798 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012799 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012800#ifdef BACKSLASH_IN_FILENAME
12801 if (rettv->vval.v_string != NULL)
12802 slash_adjust(rettv->vval.v_string);
12803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 }
12805}
12806
12807/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012808 * "getfontname()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->v_type = VAR_STRING;
12814 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012815#ifdef FEAT_GUI
12816 if (gui.in_use)
12817 {
12818 GuiFont font;
12819 char_u *name = NULL;
12820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012821 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012822 {
12823 /* Get the "Normal" font. Either the name saved by
12824 * hl_set_font_name() or from the font ID. */
12825 font = gui.norm_font;
12826 name = hl_get_font_name();
12827 }
12828 else
12829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012831 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12832 return;
12833 font = gui_mch_get_font(name, FALSE);
12834 if (font == NOFONT)
12835 return; /* Invalid font name, return empty string. */
12836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012839 gui_mch_free_font(font);
12840 }
12841#endif
12842}
12843
12844/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012845 * "getfperm({fname})" function
12846 */
12847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012848f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012849{
12850 char_u *fname;
12851 struct stat st;
12852 char_u *perm = NULL;
12853 char_u flags[] = "rwx";
12854 int i;
12855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012859 if (mch_stat((char *)fname, &st) >= 0)
12860 {
12861 perm = vim_strsave((char_u *)"---------");
12862 if (perm != NULL)
12863 {
12864 for (i = 0; i < 9; i++)
12865 {
12866 if (st.st_mode & (1 << (8 - i)))
12867 perm[i] = flags[i % 3];
12868 }
12869 }
12870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012872}
12873
12874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 * "getfsize({fname})" function
12876 */
12877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012878f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880 char_u *fname;
12881 struct stat st;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
12887 if (mch_stat((char *)fname, &st) >= 0)
12888 {
12889 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012894
12895 /* non-perfect check for overflow */
12896 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12897 rettv->vval.v_number = -2;
12898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 }
12900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902}
12903
12904/*
12905 * "getftime({fname})" function
12906 */
12907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012908f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
12910 char_u *fname;
12911 struct stat st;
12912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
12915 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919}
12920
12921/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012922 * "getftype({fname})" function
12923 */
12924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012925f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012926{
12927 char_u *fname;
12928 struct stat st;
12929 char_u *type = NULL;
12930 char *t;
12931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012935 if (mch_lstat((char *)fname, &st) >= 0)
12936 {
12937#ifdef S_ISREG
12938 if (S_ISREG(st.st_mode))
12939 t = "file";
12940 else if (S_ISDIR(st.st_mode))
12941 t = "dir";
12942# ifdef S_ISLNK
12943 else if (S_ISLNK(st.st_mode))
12944 t = "link";
12945# endif
12946# ifdef S_ISBLK
12947 else if (S_ISBLK(st.st_mode))
12948 t = "bdev";
12949# endif
12950# ifdef S_ISCHR
12951 else if (S_ISCHR(st.st_mode))
12952 t = "cdev";
12953# endif
12954# ifdef S_ISFIFO
12955 else if (S_ISFIFO(st.st_mode))
12956 t = "fifo";
12957# endif
12958# ifdef S_ISSOCK
12959 else if (S_ISSOCK(st.st_mode))
12960 t = "fifo";
12961# endif
12962 else
12963 t = "other";
12964#else
12965# ifdef S_IFMT
12966 switch (st.st_mode & S_IFMT)
12967 {
12968 case S_IFREG: t = "file"; break;
12969 case S_IFDIR: t = "dir"; break;
12970# ifdef S_IFLNK
12971 case S_IFLNK: t = "link"; break;
12972# endif
12973# ifdef S_IFBLK
12974 case S_IFBLK: t = "bdev"; break;
12975# endif
12976# ifdef S_IFCHR
12977 case S_IFCHR: t = "cdev"; break;
12978# endif
12979# ifdef S_IFIFO
12980 case S_IFIFO: t = "fifo"; break;
12981# endif
12982# ifdef S_IFSOCK
12983 case S_IFSOCK: t = "socket"; break;
12984# endif
12985 default: t = "other";
12986 }
12987# else
12988 if (mch_isdir(fname))
12989 t = "dir";
12990 else
12991 t = "file";
12992# endif
12993#endif
12994 type = vim_strsave((char_u *)t);
12995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012997}
12998
12999/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013000 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001 */
13002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013003f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013004{
13005 linenr_T lnum;
13006 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013007 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008
13009 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013010 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013011 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013012 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013013 retlist = FALSE;
13014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013015 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013016 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013017 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013018 retlist = TRUE;
13019 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013020
Bram Moolenaar342337a2005-07-21 21:11:17 +000013021 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022}
13023
13024/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013025 * "getmatches()" function
13026 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013028f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013029{
13030#ifdef FEAT_SEARCH_EXTRA
13031 dict_T *dict;
13032 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013033 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013034
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013035 if (rettv_list_alloc(rettv) == OK)
13036 {
13037 while (cur != NULL)
13038 {
13039 dict = dict_alloc();
13040 if (dict == NULL)
13041 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013042 if (cur->match.regprog == NULL)
13043 {
13044 /* match added with matchaddpos() */
13045 for (i = 0; i < MAXPOSMATCH; ++i)
13046 {
13047 llpos_T *llpos;
13048 char buf[6];
13049 list_T *l;
13050
13051 llpos = &cur->pos.pos[i];
13052 if (llpos->lnum == 0)
13053 break;
13054 l = list_alloc();
13055 if (l == NULL)
13056 break;
13057 list_append_number(l, (varnumber_T)llpos->lnum);
13058 if (llpos->col > 0)
13059 {
13060 list_append_number(l, (varnumber_T)llpos->col);
13061 list_append_number(l, (varnumber_T)llpos->len);
13062 }
13063 sprintf(buf, "pos%d", i + 1);
13064 dict_add_list(dict, buf, l);
13065 }
13066 }
13067 else
13068 {
13069 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13070 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013071 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013072 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13073 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013074# ifdef FEAT_CONCEAL
13075 if (cur->conceal_char)
13076 {
13077 char_u buf[MB_MAXBYTES + 1];
13078
13079 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13080 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13081 }
13082# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013083 list_append_dict(rettv->vval.v_list, dict);
13084 cur = cur->next;
13085 }
13086 }
13087#endif
13088}
13089
13090/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013091 * "getpid()" function
13092 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013094f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013095{
13096 rettv->vval.v_number = mch_get_pid();
13097}
13098
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013099static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013100
13101/*
13102 * "getcurpos()" function
13103 */
13104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013105f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013106{
13107 getpos_both(argvars, rettv, TRUE);
13108}
13109
Bram Moolenaar18081e32008-02-20 19:11:07 +000013110/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013111 * "getpos(string)" function
13112 */
13113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013114f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013115{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013116 getpos_both(argvars, rettv, FALSE);
13117}
13118
13119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013120getpos_both(
13121 typval_T *argvars,
13122 typval_T *rettv,
13123 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013124{
Bram Moolenaara5525202006-03-02 22:52:09 +000013125 pos_T *fp;
13126 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013127 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013128
13129 if (rettv_list_alloc(rettv) == OK)
13130 {
13131 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013132 if (getcurpos)
13133 fp = &curwin->w_cursor;
13134 else
13135 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013136 if (fnum != -1)
13137 list_append_number(l, (varnumber_T)fnum);
13138 else
13139 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013140 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13141 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013142 list_append_number(l, (fp != NULL)
13143 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013144 : (varnumber_T)0);
13145 list_append_number(l,
13146#ifdef FEAT_VIRTUALEDIT
13147 (fp != NULL) ? (varnumber_T)fp->coladd :
13148#endif
13149 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013150 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013151 {
13152 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013153 list_append_number(l, curwin->w_curswant == MAXCOL ?
13154 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013155 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013156 }
13157 else
13158 rettv->vval.v_number = FALSE;
13159}
13160
13161/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013162 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013163 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013165f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013166{
13167#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013168 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013169#endif
13170
Bram Moolenaar2641f772005-03-25 21:58:17 +000013171#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013172 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013173 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013174 wp = NULL;
13175 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13176 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013177 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013178 if (wp == NULL)
13179 return;
13180 }
13181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013182 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013183 }
13184#endif
13185}
13186
13187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 * "getreg()" function
13189 */
13190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013191f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
13193 char_u *strregname;
13194 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013195 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013196 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 strregname = get_tv_string_chk(&argvars[0]);
13202 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013203 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013205 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013206 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13207 return_list = get_tv_number_chk(&argvars[2], &error);
13208 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013211 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013212
13213 if (error)
13214 return;
13215
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 regname = (strregname == NULL ? '"' : *strregname);
13217 if (regname == 0)
13218 regname = '"';
13219
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013220 if (return_list)
13221 {
13222 rettv->v_type = VAR_LIST;
13223 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13224 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013225 if (rettv->vval.v_list != NULL)
13226 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013227 }
13228 else
13229 {
13230 rettv->v_type = VAR_STRING;
13231 rettv->vval.v_string = get_reg_contents(regname,
13232 arg2 ? GREG_EXPR_SRC : 0);
13233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234}
13235
13236/*
13237 * "getregtype()" function
13238 */
13239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013240f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241{
13242 char_u *strregname;
13243 int regname;
13244 char_u buf[NUMBUFLEN + 2];
13245 long reglen = 0;
13246
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013247 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013248 {
13249 strregname = get_tv_string_chk(&argvars[0]);
13250 if (strregname == NULL) /* type error; errmsg already given */
13251 {
13252 rettv->v_type = VAR_STRING;
13253 rettv->vval.v_string = NULL;
13254 return;
13255 }
13256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 else
13258 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013259 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260
13261 regname = (strregname == NULL ? '"' : *strregname);
13262 if (regname == 0)
13263 regname = '"';
13264
13265 buf[0] = NUL;
13266 buf[1] = NUL;
13267 switch (get_reg_type(regname, &reglen))
13268 {
13269 case MLINE: buf[0] = 'V'; break;
13270 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 case MBLOCK:
13272 buf[0] = Ctrl_V;
13273 sprintf((char *)buf + 1, "%ld", reglen + 1);
13274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->v_type = VAR_STRING;
13277 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
13280/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013281 * "gettabvar()" function
13282 */
13283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013284f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013285{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013286 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013287 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013288 dictitem_T *v;
13289 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013290 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013291
13292 rettv->v_type = VAR_STRING;
13293 rettv->vval.v_string = NULL;
13294
13295 varname = get_tv_string_chk(&argvars[1]);
13296 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13297 if (tp != NULL && varname != NULL)
13298 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013299 /* Set tp to be our tabpage, temporarily. Also set the window to the
13300 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013301 if (switch_win(&oldcurwin, &oldtabpage,
13302 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013303 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013304 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013305 /* look up the variable */
13306 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13307 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13308 if (v != NULL)
13309 {
13310 copy_tv(&v->di_tv, rettv);
13311 done = TRUE;
13312 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013313 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013314
13315 /* restore previous notion of curwin */
13316 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013317 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013318
13319 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013320 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013321}
13322
13323/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013324 * "gettabwinvar()" function
13325 */
13326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013327f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013328{
13329 getwinvar(argvars, rettv, 1);
13330}
13331
13332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 * "getwinposx()" function
13334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013336f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339#ifdef FEAT_GUI
13340 if (gui.in_use)
13341 {
13342 int x, y;
13343
13344 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 }
13347#endif
13348}
13349
13350/*
13351 * "getwinposy()" function
13352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013354f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357#ifdef FEAT_GUI
13358 if (gui.in_use)
13359 {
13360 int x, y;
13361
13362 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 }
13365#endif
13366}
13367
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013368/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013369 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013370 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013371 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013372find_win_by_nr(
13373 typval_T *vp,
13374 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013375{
13376#ifdef FEAT_WINDOWS
13377 win_T *wp;
13378#endif
13379 int nr;
13380
13381 nr = get_tv_number_chk(vp, NULL);
13382
13383#ifdef FEAT_WINDOWS
13384 if (nr < 0)
13385 return NULL;
13386 if (nr == 0)
13387 return curwin;
13388
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013389 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13390 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013391 if (--nr <= 0)
13392 break;
13393 return wp;
13394#else
13395 if (nr == 0 || nr == 1)
13396 return curwin;
13397 return NULL;
13398#endif
13399}
13400
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013402 * Find window specified by "wvp" in tabpage "tvp".
13403 */
13404 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013405find_tabwin(
13406 typval_T *wvp, /* VAR_UNKNOWN for current window */
13407 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013408{
13409 win_T *wp = NULL;
13410 tabpage_T *tp = NULL;
13411 long n;
13412
13413 if (wvp->v_type != VAR_UNKNOWN)
13414 {
13415 if (tvp->v_type != VAR_UNKNOWN)
13416 {
13417 n = get_tv_number(tvp);
13418 if (n >= 0)
13419 tp = find_tabpage(n);
13420 }
13421 else
13422 tp = curtab;
13423
13424 if (tp != NULL)
13425 wp = find_win_by_nr(wvp, tp);
13426 }
13427 else
13428 wp = curwin;
13429
13430 return wp;
13431}
13432
13433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 * "getwinvar()" function
13435 */
13436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013437f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013439 getwinvar(argvars, rettv, 0);
13440}
13441
13442/*
13443 * getwinvar() and gettabwinvar()
13444 */
13445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013446getwinvar(
13447 typval_T *argvars,
13448 typval_T *rettv,
13449 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013450{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013451 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013453 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013454 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013455 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013456#ifdef FEAT_WINDOWS
13457 win_T *oldcurwin;
13458 tabpage_T *oldtabpage;
13459 int need_switch_win;
13460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013462#ifdef FEAT_WINDOWS
13463 if (off == 1)
13464 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13465 else
13466 tp = curtab;
13467#endif
13468 win = find_win_by_nr(&argvars[off], tp);
13469 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013472 rettv->v_type = VAR_STRING;
13473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474
13475 if (win != NULL && varname != NULL)
13476 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013477#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013478 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013479 * otherwise the window is not valid. Only do this when needed,
13480 * autocommands get blocked. */
13481 need_switch_win = !(tp == curtab && win == curwin);
13482 if (!need_switch_win
13483 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13484#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013485 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013486 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013487 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013488 if (get_option_tv(&varname, rettv, 1) == OK)
13489 done = TRUE;
13490 }
13491 else
13492 {
13493 /* Look up the variable. */
13494 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13495 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13496 varname, FALSE);
13497 if (v != NULL)
13498 {
13499 copy_tv(&v->di_tv, rettv);
13500 done = TRUE;
13501 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013504
Bram Moolenaarba117c22015-09-29 16:53:22 +020013505#ifdef FEAT_WINDOWS
13506 if (need_switch_win)
13507 /* restore previous notion of curwin */
13508 restore_win(oldcurwin, oldtabpage, TRUE);
13509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 }
13511
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013512 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13513 /* use the default return value */
13514 copy_tv(&argvars[off + 2], rettv);
13515
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 --emsg_off;
13517}
13518
13519/*
13520 * "glob()" function
13521 */
13522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013523f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013525 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013527 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013529 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013530 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013532 if (argvars[1].v_type != VAR_UNKNOWN)
13533 {
13534 if (get_tv_number_chk(&argvars[1], &error))
13535 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013536 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013537 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013538 if (get_tv_number_chk(&argvars[2], &error))
13539 {
13540 rettv->v_type = VAR_LIST;
13541 rettv->vval.v_list = NULL;
13542 }
13543 if (argvars[3].v_type != VAR_UNKNOWN
13544 && get_tv_number_chk(&argvars[3], &error))
13545 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013546 }
13547 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013548 if (!error)
13549 {
13550 ExpandInit(&xpc);
13551 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013552 if (p_wic)
13553 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013554 if (rettv->v_type == VAR_STRING)
13555 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013556 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013557 else if (rettv_list_alloc(rettv) != FAIL)
13558 {
13559 int i;
13560
13561 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13562 NULL, options, WILD_ALL_KEEP);
13563 for (i = 0; i < xpc.xp_numfiles; i++)
13564 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13565
13566 ExpandCleanup(&xpc);
13567 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013568 }
13569 else
13570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571}
13572
13573/*
13574 * "globpath()" function
13575 */
13576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013577f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013579 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013581 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013582 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013583 garray_T ga;
13584 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013586 /* When the optional second argument is non-zero, don't remove matches
13587 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013588 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013589 if (argvars[2].v_type != VAR_UNKNOWN)
13590 {
13591 if (get_tv_number_chk(&argvars[2], &error))
13592 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013593 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013594 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013595 if (get_tv_number_chk(&argvars[3], &error))
13596 {
13597 rettv->v_type = VAR_LIST;
13598 rettv->vval.v_list = NULL;
13599 }
13600 if (argvars[4].v_type != VAR_UNKNOWN
13601 && get_tv_number_chk(&argvars[4], &error))
13602 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013603 }
13604 }
13605 if (file != NULL && !error)
13606 {
13607 ga_init2(&ga, (int)sizeof(char_u *), 10);
13608 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13609 if (rettv->v_type == VAR_STRING)
13610 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13611 else if (rettv_list_alloc(rettv) != FAIL)
13612 for (i = 0; i < ga.ga_len; ++i)
13613 list_append_string(rettv->vval.v_list,
13614 ((char_u **)(ga.ga_data))[i], -1);
13615 ga_clear_strings(&ga);
13616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619}
13620
13621/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013622 * "glob2regpat()" function
13623 */
13624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013625f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013626{
13627 char_u *pat = get_tv_string_chk(&argvars[0]);
13628
13629 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013630 rettv->vval.v_string = (pat == NULL)
13631 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013632}
13633
13634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635 * "has()" function
13636 */
13637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013638f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639{
13640 int i;
13641 char_u *name;
13642 int n = FALSE;
13643 static char *(has_list[]) =
13644 {
13645#ifdef AMIGA
13646 "amiga",
13647# ifdef FEAT_ARP
13648 "arp",
13649# endif
13650#endif
13651#ifdef __BEOS__
13652 "beos",
13653#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013654#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 "mac",
13656#endif
13657#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013658 "macunix", /* built with 'darwin' enabled */
13659#endif
13660#if defined(__APPLE__) && __APPLE__ == 1
13661 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663#ifdef __QNX__
13664 "qnx",
13665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666#ifdef UNIX
13667 "unix",
13668#endif
13669#ifdef VMS
13670 "vms",
13671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#ifdef WIN32
13673 "win32",
13674#endif
13675#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13676 "win32unix",
13677#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013678#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 "win64",
13680#endif
13681#ifdef EBCDIC
13682 "ebcdic",
13683#endif
13684#ifndef CASE_INSENSITIVE_FILENAME
13685 "fname_case",
13686#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013687#ifdef HAVE_ACL
13688 "acl",
13689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690#ifdef FEAT_ARABIC
13691 "arabic",
13692#endif
13693#ifdef FEAT_AUTOCMD
13694 "autocmd",
13695#endif
13696#ifdef FEAT_BEVAL
13697 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013698# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13699 "balloon_multiline",
13700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701#endif
13702#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13703 "builtin_terms",
13704# ifdef ALL_BUILTIN_TCAPS
13705 "all_builtin_terms",
13706# endif
13707#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013708#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13709 || defined(FEAT_GUI_W32) \
13710 || defined(FEAT_GUI_MOTIF))
13711 "browsefilter",
13712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713#ifdef FEAT_BYTEOFF
13714 "byte_offset",
13715#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013716#ifdef FEAT_CHANNEL
13717 "channel",
13718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719#ifdef FEAT_CINDENT
13720 "cindent",
13721#endif
13722#ifdef FEAT_CLIENTSERVER
13723 "clientserver",
13724#endif
13725#ifdef FEAT_CLIPBOARD
13726 "clipboard",
13727#endif
13728#ifdef FEAT_CMDL_COMPL
13729 "cmdline_compl",
13730#endif
13731#ifdef FEAT_CMDHIST
13732 "cmdline_hist",
13733#endif
13734#ifdef FEAT_COMMENTS
13735 "comments",
13736#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013737#ifdef FEAT_CONCEAL
13738 "conceal",
13739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#ifdef FEAT_CRYPT
13741 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013742 "crypt-blowfish",
13743 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#endif
13745#ifdef FEAT_CSCOPE
13746 "cscope",
13747#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013748#ifdef FEAT_CURSORBIND
13749 "cursorbind",
13750#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013751#ifdef CURSOR_SHAPE
13752 "cursorshape",
13753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#ifdef DEBUG
13755 "debug",
13756#endif
13757#ifdef FEAT_CON_DIALOG
13758 "dialog_con",
13759#endif
13760#ifdef FEAT_GUI_DIALOG
13761 "dialog_gui",
13762#endif
13763#ifdef FEAT_DIFF
13764 "diff",
13765#endif
13766#ifdef FEAT_DIGRAPHS
13767 "digraphs",
13768#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013769#ifdef FEAT_DIRECTX
13770 "directx",
13771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772#ifdef FEAT_DND
13773 "dnd",
13774#endif
13775#ifdef FEAT_EMACS_TAGS
13776 "emacs_tags",
13777#endif
13778 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013779 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_SEARCH_EXTRA
13781 "extra_search",
13782#endif
13783#ifdef FEAT_FKMAP
13784 "farsi",
13785#endif
13786#ifdef FEAT_SEARCHPATH
13787 "file_in_path",
13788#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013789#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013790 "filterpipe",
13791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_FIND_ID
13793 "find_in_path",
13794#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013795#ifdef FEAT_FLOAT
13796 "float",
13797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef FEAT_FOLDING
13799 "folding",
13800#endif
13801#ifdef FEAT_FOOTER
13802 "footer",
13803#endif
13804#if !defined(USE_SYSTEM) && defined(UNIX)
13805 "fork",
13806#endif
13807#ifdef FEAT_GETTEXT
13808 "gettext",
13809#endif
13810#ifdef FEAT_GUI
13811 "gui",
13812#endif
13813#ifdef FEAT_GUI_ATHENA
13814# ifdef FEAT_GUI_NEXTAW
13815 "gui_neXtaw",
13816# else
13817 "gui_athena",
13818# endif
13819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820#ifdef FEAT_GUI_GTK
13821 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013822# ifdef USE_GTK3
13823 "gui_gtk3",
13824# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013828#ifdef FEAT_GUI_GNOME
13829 "gui_gnome",
13830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#ifdef FEAT_GUI_MAC
13832 "gui_mac",
13833#endif
13834#ifdef FEAT_GUI_MOTIF
13835 "gui_motif",
13836#endif
13837#ifdef FEAT_GUI_PHOTON
13838 "gui_photon",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef FEAT_GUI_W32
13841 "gui_win32",
13842#endif
13843#ifdef FEAT_HANGULIN
13844 "hangul_input",
13845#endif
13846#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13847 "iconv",
13848#endif
13849#ifdef FEAT_INS_EXPAND
13850 "insert_expand",
13851#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013852#ifdef FEAT_JOB
13853 "job",
13854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855#ifdef FEAT_JUMPLIST
13856 "jumplist",
13857#endif
13858#ifdef FEAT_KEYMAP
13859 "keymap",
13860#endif
13861#ifdef FEAT_LANGMAP
13862 "langmap",
13863#endif
13864#ifdef FEAT_LIBCALL
13865 "libcall",
13866#endif
13867#ifdef FEAT_LINEBREAK
13868 "linebreak",
13869#endif
13870#ifdef FEAT_LISP
13871 "lispindent",
13872#endif
13873#ifdef FEAT_LISTCMDS
13874 "listcmds",
13875#endif
13876#ifdef FEAT_LOCALMAP
13877 "localmap",
13878#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013879#ifdef FEAT_LUA
13880# ifndef DYNAMIC_LUA
13881 "lua",
13882# endif
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_MENU
13885 "menu",
13886#endif
13887#ifdef FEAT_SESSION
13888 "mksession",
13889#endif
13890#ifdef FEAT_MODIFY_FNAME
13891 "modify_fname",
13892#endif
13893#ifdef FEAT_MOUSE
13894 "mouse",
13895#endif
13896#ifdef FEAT_MOUSESHAPE
13897 "mouseshape",
13898#endif
13899#if defined(UNIX) || defined(VMS)
13900# ifdef FEAT_MOUSE_DEC
13901 "mouse_dec",
13902# endif
13903# ifdef FEAT_MOUSE_GPM
13904 "mouse_gpm",
13905# endif
13906# ifdef FEAT_MOUSE_JSB
13907 "mouse_jsbterm",
13908# endif
13909# ifdef FEAT_MOUSE_NET
13910 "mouse_netterm",
13911# endif
13912# ifdef FEAT_MOUSE_PTERM
13913 "mouse_pterm",
13914# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013915# ifdef FEAT_MOUSE_SGR
13916 "mouse_sgr",
13917# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013918# ifdef FEAT_SYSMOUSE
13919 "mouse_sysmouse",
13920# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013921# ifdef FEAT_MOUSE_URXVT
13922 "mouse_urxvt",
13923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924# ifdef FEAT_MOUSE_XTERM
13925 "mouse_xterm",
13926# endif
13927#endif
13928#ifdef FEAT_MBYTE
13929 "multi_byte",
13930#endif
13931#ifdef FEAT_MBYTE_IME
13932 "multi_byte_ime",
13933#endif
13934#ifdef FEAT_MULTI_LANG
13935 "multi_lang",
13936#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013937#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013938#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013939 "mzscheme",
13940#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#ifdef FEAT_OLE
13943 "ole",
13944#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013945 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946#ifdef FEAT_PATH_EXTRA
13947 "path_extra",
13948#endif
13949#ifdef FEAT_PERL
13950#ifndef DYNAMIC_PERL
13951 "perl",
13952#endif
13953#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013954#ifdef FEAT_PERSISTENT_UNDO
13955 "persistent_undo",
13956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#ifdef FEAT_PYTHON
13958#ifndef DYNAMIC_PYTHON
13959 "python",
13960#endif
13961#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013962#ifdef FEAT_PYTHON3
13963#ifndef DYNAMIC_PYTHON3
13964 "python3",
13965#endif
13966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef FEAT_POSTSCRIPT
13968 "postscript",
13969#endif
13970#ifdef FEAT_PRINTER
13971 "printer",
13972#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013973#ifdef FEAT_PROFILE
13974 "profile",
13975#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013976#ifdef FEAT_RELTIME
13977 "reltime",
13978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979#ifdef FEAT_QUICKFIX
13980 "quickfix",
13981#endif
13982#ifdef FEAT_RIGHTLEFT
13983 "rightleft",
13984#endif
13985#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13986 "ruby",
13987#endif
13988#ifdef FEAT_SCROLLBIND
13989 "scrollbind",
13990#endif
13991#ifdef FEAT_CMDL_INFO
13992 "showcmd",
13993 "cmdline_info",
13994#endif
13995#ifdef FEAT_SIGNS
13996 "signs",
13997#endif
13998#ifdef FEAT_SMARTINDENT
13999 "smartindent",
14000#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014001#ifdef STARTUPTIME
14002 "startuptime",
14003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004#ifdef FEAT_STL_OPT
14005 "statusline",
14006#endif
14007#ifdef FEAT_SUN_WORKSHOP
14008 "sun_workshop",
14009#endif
14010#ifdef FEAT_NETBEANS_INTG
14011 "netbeans_intg",
14012#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014013#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014014 "spell",
14015#endif
14016#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 "syntax",
14018#endif
14019#if defined(USE_SYSTEM) || !defined(UNIX)
14020 "system",
14021#endif
14022#ifdef FEAT_TAG_BINS
14023 "tag_binary",
14024#endif
14025#ifdef FEAT_TAG_OLDSTATIC
14026 "tag_old_static",
14027#endif
14028#ifdef FEAT_TAG_ANYWHITE
14029 "tag_any_white",
14030#endif
14031#ifdef FEAT_TCL
14032# ifndef DYNAMIC_TCL
14033 "tcl",
14034# endif
14035#endif
14036#ifdef TERMINFO
14037 "terminfo",
14038#endif
14039#ifdef FEAT_TERMRESPONSE
14040 "termresponse",
14041#endif
14042#ifdef FEAT_TEXTOBJ
14043 "textobjects",
14044#endif
14045#ifdef HAVE_TGETENT
14046 "tgetent",
14047#endif
14048#ifdef FEAT_TITLE
14049 "title",
14050#endif
14051#ifdef FEAT_TOOLBAR
14052 "toolbar",
14053#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014054#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14055 "unnamedplus",
14056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057#ifdef FEAT_USR_CMDS
14058 "user-commands", /* was accidentally included in 5.4 */
14059 "user_commands",
14060#endif
14061#ifdef FEAT_VIMINFO
14062 "viminfo",
14063#endif
14064#ifdef FEAT_VERTSPLIT
14065 "vertsplit",
14066#endif
14067#ifdef FEAT_VIRTUALEDIT
14068 "virtualedit",
14069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#ifdef FEAT_VISUALEXTRA
14072 "visualextra",
14073#endif
14074#ifdef FEAT_VREPLACE
14075 "vreplace",
14076#endif
14077#ifdef FEAT_WILDIGN
14078 "wildignore",
14079#endif
14080#ifdef FEAT_WILDMENU
14081 "wildmenu",
14082#endif
14083#ifdef FEAT_WINDOWS
14084 "windows",
14085#endif
14086#ifdef FEAT_WAK
14087 "winaltkeys",
14088#endif
14089#ifdef FEAT_WRITEBACKUP
14090 "writebackup",
14091#endif
14092#ifdef FEAT_XIM
14093 "xim",
14094#endif
14095#ifdef FEAT_XFONTSET
14096 "xfontset",
14097#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014098#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014099 "xpm",
14100 "xpm_w32", /* for backward compatibility */
14101#else
14102# if defined(HAVE_XPM)
14103 "xpm",
14104# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106#ifdef USE_XSMP
14107 "xsmp",
14108#endif
14109#ifdef USE_XSMP_INTERACT
14110 "xsmp_interact",
14111#endif
14112#ifdef FEAT_XCLIPBOARD
14113 "xterm_clipboard",
14114#endif
14115#ifdef FEAT_XTERM_SAVE
14116 "xterm_save",
14117#endif
14118#if defined(UNIX) && defined(FEAT_X11)
14119 "X11",
14120#endif
14121 NULL
14122 };
14123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 for (i = 0; has_list[i] != NULL; ++i)
14126 if (STRICMP(name, has_list[i]) == 0)
14127 {
14128 n = TRUE;
14129 break;
14130 }
14131
14132 if (n == FALSE)
14133 {
14134 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014135 {
14136 if (name[5] == '-'
14137 && STRLEN(name) > 11
14138 && vim_isdigit(name[6])
14139 && vim_isdigit(name[8])
14140 && vim_isdigit(name[10]))
14141 {
14142 int major = atoi((char *)name + 6);
14143 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014144
14145 /* Expect "patch-9.9.01234". */
14146 n = (major < VIM_VERSION_MAJOR
14147 || (major == VIM_VERSION_MAJOR
14148 && (minor < VIM_VERSION_MINOR
14149 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014150 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014151 }
14152 else
14153 n = has_patch(atoi((char *)name + 5));
14154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 else if (STRICMP(name, "vim_starting") == 0)
14156 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014157#ifdef FEAT_MBYTE
14158 else if (STRICMP(name, "multi_byte_encoding") == 0)
14159 n = has_mbyte;
14160#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014161#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14162 else if (STRICMP(name, "balloon_multiline") == 0)
14163 n = multiline_balloon_available();
14164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165#ifdef DYNAMIC_TCL
14166 else if (STRICMP(name, "tcl") == 0)
14167 n = tcl_enabled(FALSE);
14168#endif
14169#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14170 else if (STRICMP(name, "iconv") == 0)
14171 n = iconv_enabled(FALSE);
14172#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014173#ifdef DYNAMIC_LUA
14174 else if (STRICMP(name, "lua") == 0)
14175 n = lua_enabled(FALSE);
14176#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014177#ifdef DYNAMIC_MZSCHEME
14178 else if (STRICMP(name, "mzscheme") == 0)
14179 n = mzscheme_enabled(FALSE);
14180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181#ifdef DYNAMIC_RUBY
14182 else if (STRICMP(name, "ruby") == 0)
14183 n = ruby_enabled(FALSE);
14184#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014185#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186#ifdef DYNAMIC_PYTHON
14187 else if (STRICMP(name, "python") == 0)
14188 n = python_enabled(FALSE);
14189#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014190#endif
14191#ifdef FEAT_PYTHON3
14192#ifdef DYNAMIC_PYTHON3
14193 else if (STRICMP(name, "python3") == 0)
14194 n = python3_enabled(FALSE);
14195#endif
14196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197#ifdef DYNAMIC_PERL
14198 else if (STRICMP(name, "perl") == 0)
14199 n = perl_enabled(FALSE);
14200#endif
14201#ifdef FEAT_GUI
14202 else if (STRICMP(name, "gui_running") == 0)
14203 n = (gui.in_use || gui.starting);
14204# ifdef FEAT_GUI_W32
14205 else if (STRICMP(name, "gui_win32s") == 0)
14206 n = gui_is_win32s();
14207# endif
14208# ifdef FEAT_BROWSE
14209 else if (STRICMP(name, "browse") == 0)
14210 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14211# endif
14212#endif
14213#ifdef FEAT_SYN_HL
14214 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014215 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216#endif
14217#if defined(WIN3264)
14218 else if (STRICMP(name, "win95") == 0)
14219 n = mch_windows95();
14220#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014221#ifdef FEAT_NETBEANS_INTG
14222 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014223 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 }
14226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014227 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228}
14229
14230/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014231 * "has_key()" function
14232 */
14233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014234f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014235{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014236 if (argvars[0].v_type != VAR_DICT)
14237 {
14238 EMSG(_(e_dictreq));
14239 return;
14240 }
14241 if (argvars[0].vval.v_dict == NULL)
14242 return;
14243
14244 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014245 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014246}
14247
14248/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014249 * "haslocaldir()" function
14250 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014252f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014253{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014254 win_T *wp = NULL;
14255
14256 wp = find_tabwin(&argvars[0], &argvars[1]);
14257 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014258}
14259
14260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 * "hasmapto()" function
14262 */
14263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014264f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265{
14266 char_u *name;
14267 char_u *mode;
14268 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014269 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014272 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 mode = (char_u *)"nvo";
14274 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014277 if (argvars[2].v_type != VAR_UNKNOWN)
14278 abbr = get_tv_number(&argvars[2]);
14279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280
Bram Moolenaar2c932302006-03-18 21:42:09 +000014281 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285}
14286
14287/*
14288 * "histadd()" function
14289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014291f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292{
14293#ifdef FEAT_CMDHIST
14294 int histype;
14295 char_u *str;
14296 char_u buf[NUMBUFLEN];
14297#endif
14298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 if (check_restricted() || check_secure())
14301 return;
14302#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14304 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 if (histype >= 0)
14306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 if (*str != NUL)
14309 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014310 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 return;
14314 }
14315 }
14316#endif
14317}
14318
14319/*
14320 * "histdel()" function
14321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014323f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324{
14325#ifdef FEAT_CMDHIST
14326 int n;
14327 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14331 if (str == NULL)
14332 n = 0;
14333 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014336 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 else
14341 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 get_tv_string_buf(&argvars[1], buf));
14344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345#endif
14346}
14347
14348/*
14349 * "histget()" function
14350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014352f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354#ifdef FEAT_CMDHIST
14355 int type;
14356 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14360 if (str == NULL)
14361 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 {
14364 type = get_histtype(str);
14365 if (argvars[1].v_type == VAR_UNKNOWN)
14366 idx = get_history_idx(type);
14367 else
14368 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14369 /* -1 on type error */
14370 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014375 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376}
14377
14378/*
14379 * "histnr()" function
14380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014382f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
14384 int i;
14385
14386#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 char_u *history = get_tv_string_chk(&argvars[0]);
14388
14389 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 if (i >= HIST_CMD && i < HIST_COUNT)
14391 i = get_history_idx(i);
14392 else
14393#endif
14394 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396}
14397
14398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 * "highlightID(name)" function
14400 */
14401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014402f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405}
14406
14407/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408 * "highlight_exists()" function
14409 */
14410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014411f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014412{
14413 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14414}
14415
14416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 * "hostname()" function
14418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014420f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421{
14422 char_u hostname[256];
14423
14424 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->v_type = VAR_STRING;
14426 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427}
14428
14429/*
14430 * iconv() function
14431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014433f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434{
14435#ifdef FEAT_MBYTE
14436 char_u buf1[NUMBUFLEN];
14437 char_u buf2[NUMBUFLEN];
14438 char_u *from, *to, *str;
14439 vimconv_T vimconv;
14440#endif
14441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442 rettv->v_type = VAR_STRING;
14443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
14445#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446 str = get_tv_string(&argvars[0]);
14447 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14448 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 vimconv.vc_type = CONV_NONE;
14450 convert_setup(&vimconv, from, to);
14451
14452 /* If the encodings are equal, no conversion needed. */
14453 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
14458 convert_setup(&vimconv, NULL, NULL);
14459 vim_free(from);
14460 vim_free(to);
14461#endif
14462}
14463
14464/*
14465 * "indent()" function
14466 */
14467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014468f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469{
14470 linenr_T lnum;
14471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014479/*
14480 * "index()" function
14481 */
14482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014484{
Bram Moolenaar33570922005-01-25 22:26:29 +000014485 list_T *l;
14486 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014487 long idx = 0;
14488 int ic = FALSE;
14489
14490 rettv->vval.v_number = -1;
14491 if (argvars[0].v_type != VAR_LIST)
14492 {
14493 EMSG(_(e_listreq));
14494 return;
14495 }
14496 l = argvars[0].vval.v_list;
14497 if (l != NULL)
14498 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014499 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014502 int error = FALSE;
14503
Bram Moolenaar758711c2005-02-02 23:11:38 +000014504 /* Start at specified item. Use the cached index that list_find()
14505 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014507 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014508 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 ic = get_tv_number_chk(&argvars[3], &error);
14510 if (error)
14511 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014512 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014513
Bram Moolenaar758711c2005-02-02 23:11:38 +000014514 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014515 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014516 {
14517 rettv->vval.v_number = idx;
14518 break;
14519 }
14520 }
14521}
14522
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523static int inputsecret_flag = 0;
14524
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014525static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014526
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014528 * This function is used by f_input() and f_inputdialog() functions. The third
14529 * argument to f_input() specifies the type of completion to use at the
14530 * prompt. The third argument to f_inputdialog() specifies the value to return
14531 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 */
14533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014534get_user_input(
14535 typval_T *argvars,
14536 typval_T *rettv,
14537 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 char_u *p = NULL;
14541 int c;
14542 char_u buf[NUMBUFLEN];
14543 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014545 int xp_type = EXPAND_NOTHING;
14546 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550
14551#ifdef NO_CONSOLE_INPUT
14552 /* While starting up, there is no place to enter text. */
14553 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555#endif
14556
14557 cmd_silent = FALSE; /* Want to see the prompt. */
14558 if (prompt != NULL)
14559 {
14560 /* Only the part of the message after the last NL is considered as
14561 * prompt for the command line */
14562 p = vim_strrchr(prompt, '\n');
14563 if (p == NULL)
14564 p = prompt;
14565 else
14566 {
14567 ++p;
14568 c = *p;
14569 *p = NUL;
14570 msg_start();
14571 msg_clr_eos();
14572 msg_puts_attr(prompt, echo_attr);
14573 msg_didout = FALSE;
14574 msg_starthere();
14575 *p = c;
14576 }
14577 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 if (argvars[1].v_type != VAR_UNKNOWN)
14580 {
14581 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14582 if (defstr != NULL)
14583 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014585 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014586 {
14587 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014588 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014589 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014590
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014591 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014592 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014593
Bram Moolenaar4463f292005-09-25 22:20:24 +000014594 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14595 if (xp_name == NULL)
14596 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014598 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014599
Bram Moolenaar4463f292005-09-25 22:20:24 +000014600 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14601 &xp_arg) == FAIL)
14602 return;
14603 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014604 }
14605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014607 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014608 int save_ex_normal_busy = ex_normal_busy;
14609 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014610 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014611 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14612 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014613 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014614 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014615 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014616 && argvars[1].v_type != VAR_UNKNOWN
14617 && argvars[2].v_type != VAR_UNKNOWN)
14618 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14619 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014620
14621 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 /* since the user typed this, no need to wait for return */
14624 need_wait_return = FALSE;
14625 msg_didout = FALSE;
14626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 cmd_silent = cmd_silent_save;
14628}
14629
14630/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014631 * "input()" function
14632 * Also handles inputsecret() when inputsecret is set.
14633 */
14634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014635f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014636{
14637 get_user_input(argvars, rettv, FALSE);
14638}
14639
14640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 * "inputdialog()" function
14642 */
14643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014644f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645{
14646#if defined(FEAT_GUI_TEXTDIALOG)
14647 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14648 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14649 {
14650 char_u *message;
14651 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 message = get_tv_string_chk(&argvars[0]);
14655 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014656 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014657 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658 else
14659 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 if (message != NULL && defstr != NULL
14661 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014662 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 else
14665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 if (message != NULL && defstr != NULL
14667 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014668 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669 rettv->vval.v_string = vim_strsave(
14670 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 }
14676 else
14677#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014678 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679}
14680
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014681/*
14682 * "inputlist()" function
14683 */
14684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014685f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014686{
14687 listitem_T *li;
14688 int selected;
14689 int mouse_used;
14690
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014691#ifdef NO_CONSOLE_INPUT
14692 /* While starting up, there is no place to enter text. */
14693 if (no_console_input())
14694 return;
14695#endif
14696 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14697 {
14698 EMSG2(_(e_listarg), "inputlist()");
14699 return;
14700 }
14701
14702 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014703 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014704 lines_left = Rows; /* avoid more prompt */
14705 msg_scroll = TRUE;
14706 msg_clr_eos();
14707
14708 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14709 {
14710 msg_puts(get_tv_string(&li->li_tv));
14711 msg_putchar('\n');
14712 }
14713
14714 /* Ask for choice. */
14715 selected = prompt_for_number(&mouse_used);
14716 if (mouse_used)
14717 selected -= lines_left;
14718
14719 rettv->vval.v_number = selected;
14720}
14721
14722
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14724
14725/*
14726 * "inputrestore()" function
14727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014729f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730{
14731 if (ga_userinput.ga_len > 0)
14732 {
14733 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14735 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014736 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 }
14738 else if (p_verbose > 1)
14739 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014740 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 }
14743}
14744
14745/*
14746 * "inputsave()" function
14747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014749f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014751 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 if (ga_grow(&ga_userinput, 1) == OK)
14753 {
14754 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14755 + ga_userinput.ga_len);
14756 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014757 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 }
14759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761}
14762
14763/*
14764 * "inputsecret()" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768{
14769 ++cmdline_star;
14770 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014771 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 --cmdline_star;
14773 --inputsecret_flag;
14774}
14775
14776/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014777 * "insert()" function
14778 */
14779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014780f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014781{
14782 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 listitem_T *item;
14784 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014786
14787 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014788 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014789 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014790 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014791 {
14792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 before = get_tv_number_chk(&argvars[2], &error);
14794 if (error)
14795 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014796
Bram Moolenaar758711c2005-02-02 23:11:38 +000014797 if (before == l->lv_len)
14798 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799 else
14800 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014801 item = list_find(l, before);
14802 if (item == NULL)
14803 {
14804 EMSGN(_(e_listidx), before);
14805 l = NULL;
14806 }
14807 }
14808 if (l != NULL)
14809 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014810 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014811 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 }
14813 }
14814}
14815
14816/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014817 * "invert(expr)" function
14818 */
14819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014820f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014821{
14822 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14823}
14824
14825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 * "isdirectory()" function
14827 */
14828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014829f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832}
14833
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014834/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014835 * "islocked()" function
14836 */
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014839{
14840 lval_T lv;
14841 char_u *end;
14842 dictitem_T *di;
14843
14844 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014845 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14846 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014847 if (end != NULL && lv.ll_name != NULL)
14848 {
14849 if (*end != NUL)
14850 EMSG(_(e_trailing));
14851 else
14852 {
14853 if (lv.ll_tv == NULL)
14854 {
14855 if (check_changedtick(lv.ll_name))
14856 rettv->vval.v_number = 1; /* always locked */
14857 else
14858 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014859 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014860 if (di != NULL)
14861 {
14862 /* Consider a variable locked when:
14863 * 1. the variable itself is locked
14864 * 2. the value of the variable is locked.
14865 * 3. the List or Dict value is locked.
14866 */
14867 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14868 || tv_islocked(&di->di_tv));
14869 }
14870 }
14871 }
14872 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014873 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014874 else if (lv.ll_newkey != NULL)
14875 EMSG2(_(e_dictkey), lv.ll_newkey);
14876 else if (lv.ll_list != NULL)
14877 /* List item. */
14878 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14879 else
14880 /* Dictionary item. */
14881 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14882 }
14883 }
14884
14885 clear_lval(&lv);
14886}
14887
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014888#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14889/*
14890 * "isnan()" function
14891 */
14892 static void
14893f_isnan(typval_T *argvars, typval_T *rettv)
14894{
14895 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14896 && isnan(argvars[0].vval.v_float);
14897}
14898#endif
14899
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014900static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014901
14902/*
14903 * Turn a dict into a list:
14904 * "what" == 0: list of keys
14905 * "what" == 1: list of values
14906 * "what" == 2: list of items
14907 */
14908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014909dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014910{
Bram Moolenaar33570922005-01-25 22:26:29 +000014911 list_T *l2;
14912 dictitem_T *di;
14913 hashitem_T *hi;
14914 listitem_T *li;
14915 listitem_T *li2;
14916 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014917 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014918
Bram Moolenaar8c711452005-01-14 21:53:12 +000014919 if (argvars[0].v_type != VAR_DICT)
14920 {
14921 EMSG(_(e_dictreq));
14922 return;
14923 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014924 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925 return;
14926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014928 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014929
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014930 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014931 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014933 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014935 --todo;
14936 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014937
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014938 li = listitem_alloc();
14939 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014940 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014941 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014943 if (what == 0)
14944 {
14945 /* keys() */
14946 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014947 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014948 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14949 }
14950 else if (what == 1)
14951 {
14952 /* values() */
14953 copy_tv(&di->di_tv, &li->li_tv);
14954 }
14955 else
14956 {
14957 /* items() */
14958 l2 = list_alloc();
14959 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014960 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014961 li->li_tv.vval.v_list = l2;
14962 if (l2 == NULL)
14963 break;
14964 ++l2->lv_refcount;
14965
14966 li2 = listitem_alloc();
14967 if (li2 == NULL)
14968 break;
14969 list_append(l2, li2);
14970 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014971 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014972 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14973
14974 li2 = listitem_alloc();
14975 if (li2 == NULL)
14976 break;
14977 list_append(l2, li2);
14978 copy_tv(&di->di_tv, &li2->li_tv);
14979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014980 }
14981 }
14982}
14983
14984/*
14985 * "items(dict)" function
14986 */
14987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014988f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014989{
14990 dict_list(argvars, rettv, 2);
14991}
14992
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014993#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014994/*
14995 * Get the job from the argument.
14996 * Returns NULL if the job is invalid.
14997 */
14998 static job_T *
14999get_job_arg(typval_T *tv)
15000{
15001 job_T *job;
15002
15003 if (tv->v_type != VAR_JOB)
15004 {
15005 EMSG2(_(e_invarg2), get_tv_string(tv));
15006 return NULL;
15007 }
15008 job = tv->vval.v_job;
15009
15010 if (job == NULL)
15011 EMSG(_("E916: not a valid job"));
15012 return job;
15013}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015014
15015# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015016/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015017 * "job_getchannel()" function
15018 */
15019 static void
15020f_job_getchannel(typval_T *argvars, typval_T *rettv)
15021{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015022 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015023
Bram Moolenaar65edff82016-02-21 16:40:11 +010015024 if (job != NULL)
15025 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015026 rettv->v_type = VAR_CHANNEL;
15027 rettv->vval.v_channel = job->jv_channel;
15028 if (job->jv_channel != NULL)
15029 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015030 }
15031}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015032# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015033
15034/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015035 * "job_setoptions()" function
15036 */
15037 static void
15038f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15039{
15040 job_T *job = get_job_arg(&argvars[0]);
15041 jobopt_T opt;
15042
15043 if (job == NULL)
15044 return;
15045 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015046 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015047 return;
15048 job_set_options(job, &opt);
15049}
15050
15051/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015052 * "job_start()" function
15053 */
15054 static void
15055f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15056{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015057 job_T *job;
15058 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015059#if defined(UNIX)
15060# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015061 char **argv = NULL;
15062 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015063#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015064 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015065#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015066 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015067
15068 rettv->v_type = VAR_JOB;
15069 job = job_alloc();
15070 rettv->vval.v_job = job;
15071 if (job == NULL)
15072 return;
15073
15074 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015075
15076 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015077 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015078 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015079 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015080 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015081 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015082 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015083 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015084
Bram Moolenaar835dc632016-02-07 14:27:38 +010015085#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015086 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015087#endif
15088
15089 if (argvars[0].v_type == VAR_STRING)
15090 {
15091 /* Command is a string. */
15092 cmd = argvars[0].vval.v_string;
15093#ifdef USE_ARGV
15094 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15095 return;
15096 argv[argc] = NULL;
15097#endif
15098 }
15099 else if (argvars[0].v_type != VAR_LIST
15100 || argvars[0].vval.v_list == NULL
15101 || argvars[0].vval.v_list->lv_len < 1)
15102 {
15103 EMSG(_(e_invarg));
15104 return;
15105 }
15106 else
15107 {
15108 list_T *l = argvars[0].vval.v_list;
15109 listitem_T *li;
15110 char_u *s;
15111
15112#ifdef USE_ARGV
15113 /* Pass argv[] to mch_call_shell(). */
15114 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15115 if (argv == NULL)
15116 return;
15117#endif
15118 for (li = l->lv_first; li != NULL; li = li->li_next)
15119 {
15120 s = get_tv_string_chk(&li->li_tv);
15121 if (s == NULL)
15122 goto theend;
15123#ifdef USE_ARGV
15124 argv[argc++] = (char *)s;
15125#else
15126 if (li != l->lv_first)
15127 {
15128 s = vim_strsave_shellescape(s, FALSE, TRUE);
15129 if (s == NULL)
15130 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015131 ga_concat(&ga, s);
15132 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015133 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015134 else
15135 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015136 if (li->li_next != NULL)
15137 ga_append(&ga, ' ');
15138#endif
15139 }
15140#ifdef USE_ARGV
15141 argv[argc] = NULL;
15142#else
15143 cmd = ga.ga_data;
15144#endif
15145 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015146
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015148# ifdef FEAT_CHANNEL
15149 if (ch_log_active())
15150 {
15151 garray_T ga;
15152 int i;
15153
15154 ga_init2(&ga, (int)sizeof(char), 200);
15155 for (i = 0; i < argc; ++i)
15156 {
15157 if (i > 0)
15158 ga_concat(&ga, (char_u *)" ");
15159 ga_concat(&ga, (char_u *)argv[i]);
15160 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015161 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015162 ga_clear(&ga);
15163 }
15164# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015165 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015166#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015167# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015168 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015169# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015170 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015171#endif
15172
15173theend:
15174#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015175 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015176#else
15177 vim_free(ga.ga_data);
15178#endif
15179}
15180
15181/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015182 * Get the status of "job" and invoke the exit callback when needed.
15183 * The returned string is not allocated.
15184 */
15185 static char *
15186job_status(job_T *job)
15187{
15188 char *result;
15189
15190 if (job->jv_status == JOB_ENDED)
15191 /* No need to check, dead is dead. */
15192 result = "dead";
15193 else if (job->jv_status == JOB_FAILED)
15194 result = "fail";
15195 else
15196 {
15197 result = mch_job_status(job);
15198# ifdef FEAT_CHANNEL
15199 if (job->jv_status == JOB_ENDED)
15200 ch_log(job->jv_channel, "Job ended");
15201# endif
15202 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15203 {
15204 typval_T argv[3];
15205 typval_T rettv;
15206 int dummy;
15207
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015208 /* invoke the exit callback; make sure the refcount is > 0 */
15209 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015210 argv[0].v_type = VAR_JOB;
15211 argv[0].vval.v_job = job;
15212 argv[1].v_type = VAR_NUMBER;
15213 argv[1].vval.v_number = job->jv_exitval;
15214 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15215 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15216 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015217 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015218 }
15219 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15220 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015221 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015222 * freed. Careful: caller must not use "job" after this! */
15223 job_free(job);
15224 }
15225 }
15226 return result;
15227}
15228
15229/*
15230 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15231 */
15232 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015233job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015234{
15235 static time_t last_check = 0;
15236 time_t now;
15237 job_T *job;
15238 job_T *next;
15239
15240 /* Only do this once in 10 seconds. */
15241 now = time(NULL);
15242 if (last_check + 10 < now)
15243 {
15244 last_check = now;
15245 for (job = first_job; job != NULL; job = next)
15246 {
15247 next = job->jv_next;
15248 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15249 job_status(job); /* may free "job" */
15250 }
15251 }
15252}
15253
15254/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015255 * "job_status()" function
15256 */
15257 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015258f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015259{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015260 job_T *job = get_job_arg(&argvars[0]);
15261 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015262
Bram Moolenaar65edff82016-02-21 16:40:11 +010015263 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015264 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015265 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015266 rettv->v_type = VAR_STRING;
15267 rettv->vval.v_string = vim_strsave((char_u *)result);
15268 }
15269}
15270
15271/*
15272 * "job_stop()" function
15273 */
15274 static void
15275f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15276{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015277 job_T *job = get_job_arg(&argvars[0]);
15278
15279 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015280 {
15281 char_u *arg;
15282
15283 if (argvars[1].v_type == VAR_UNKNOWN)
15284 arg = (char_u *)"";
15285 else
15286 {
15287 arg = get_tv_string_chk(&argvars[1]);
15288 if (arg == NULL)
15289 {
15290 EMSG(_(e_invarg));
15291 return;
15292 }
15293 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015294 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015295 rettv->vval.v_number = 0;
15296 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015297 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015298 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015299 /* Assume that "hup" does not kill the job. */
15300 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15301 job->jv_channel->ch_job_killed = TRUE;
15302 }
15303 /* We don't try freeing the job, obviously the caller still has a
15304 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015305 }
15306}
15307#endif
15308
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015310 * "join()" function
15311 */
15312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015313f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015314{
15315 garray_T ga;
15316 char_u *sep;
15317
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015318 if (argvars[0].v_type != VAR_LIST)
15319 {
15320 EMSG(_(e_listreq));
15321 return;
15322 }
15323 if (argvars[0].vval.v_list == NULL)
15324 return;
15325 if (argvars[1].v_type == VAR_UNKNOWN)
15326 sep = (char_u *)" ";
15327 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015329
15330 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015331
15332 if (sep != NULL)
15333 {
15334 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015335 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015336 ga_append(&ga, NUL);
15337 rettv->vval.v_string = (char_u *)ga.ga_data;
15338 }
15339 else
15340 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015341}
15342
15343/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015344 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015345 */
15346 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015347f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015348{
15349 js_read_T reader;
15350
15351 reader.js_buf = get_tv_string(&argvars[0]);
15352 reader.js_fill = NULL;
15353 reader.js_used = 0;
15354 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15355 EMSG(_(e_invarg));
15356}
15357
15358/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015359 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015360 */
15361 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015362f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015363{
15364 rettv->v_type = VAR_STRING;
15365 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15366}
15367
15368/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015369 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015370 */
15371 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015372f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015373{
15374 js_read_T reader;
15375
15376 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015377 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015378 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015379 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015380 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015381}
15382
15383/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015384 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015385 */
15386 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015387f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015388{
15389 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015390 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015391}
15392
15393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015394 * "keys()" function
15395 */
15396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015397f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015398{
15399 dict_list(argvars, rettv, 0);
15400}
15401
15402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 * "last_buffer_nr()" function.
15404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015406f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407{
15408 int n = 0;
15409 buf_T *buf;
15410
15411 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15412 if (n < buf->b_fnum)
15413 n = buf->b_fnum;
15414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015416}
15417
15418/*
15419 * "len()" function
15420 */
15421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015422f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015423{
15424 switch (argvars[0].v_type)
15425 {
15426 case VAR_STRING:
15427 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015428 rettv->vval.v_number = (varnumber_T)STRLEN(
15429 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015430 break;
15431 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015434 case VAR_DICT:
15435 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15436 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015437 case VAR_UNKNOWN:
15438 case VAR_SPECIAL:
15439 case VAR_FLOAT:
15440 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015441 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015442 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015443 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015444 break;
15445 }
15446}
15447
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015448static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015449
15450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015451libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015452{
15453#ifdef FEAT_LIBCALL
15454 char_u *string_in;
15455 char_u **string_result;
15456 int nr_result;
15457#endif
15458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015460 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462
15463 if (check_restricted() || check_secure())
15464 return;
15465
15466#ifdef FEAT_LIBCALL
15467 /* The first two args must be strings, otherwise its meaningless */
15468 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15469 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015470 string_in = NULL;
15471 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015472 string_in = argvars[2].vval.v_string;
15473 if (type == VAR_NUMBER)
15474 string_result = NULL;
15475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015477 if (mch_libcall(argvars[0].vval.v_string,
15478 argvars[1].vval.v_string,
15479 string_in,
15480 argvars[2].vval.v_number,
15481 string_result,
15482 &nr_result) == OK
15483 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 }
15486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487}
15488
15489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015490 * "libcall()" function
15491 */
15492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015493f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015494{
15495 libcall_common(argvars, rettv, VAR_STRING);
15496}
15497
15498/*
15499 * "libcallnr()" function
15500 */
15501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015502f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015503{
15504 libcall_common(argvars, rettv, VAR_NUMBER);
15505}
15506
15507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 * "line(string)" function
15509 */
15510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512{
15513 linenr_T lnum = 0;
15514 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015515 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015517 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 if (fp != NULL)
15519 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521}
15522
15523/*
15524 * "line2byte(lnum)" function
15525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
15529#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531#else
15532 linenr_T lnum;
15533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15539 if (rettv->vval.v_number >= 0)
15540 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541#endif
15542}
15543
15544/*
15545 * "lispindent(lnum)" function
15546 */
15547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549{
15550#ifdef FEAT_LISP
15551 pos_T pos;
15552 linenr_T lnum;
15553
15554 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15557 {
15558 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 curwin->w_cursor = pos;
15561 }
15562 else
15563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565}
15566
15567/*
15568 * "localtime()" function
15569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015571f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015576static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580{
15581 char_u *keys;
15582 char_u *which;
15583 char_u buf[NUMBUFLEN];
15584 char_u *keys_buf = NULL;
15585 char_u *rhs;
15586 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015587 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015588 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015589 mapblock_T *mp;
15590 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591
15592 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015593 rettv->v_type = VAR_STRING;
15594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 if (*keys == NUL)
15598 return;
15599
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015600 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015603 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015604 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015605 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015606 if (argvars[3].v_type != VAR_UNKNOWN)
15607 get_dict = get_tv_number(&argvars[3]);
15608 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 else
15611 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 if (which == NULL)
15613 return;
15614
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 mode = get_map_mode(&which, 0);
15616
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015617 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015618 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015620
15621 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015623 /* Return a string. */
15624 if (rhs != NULL)
15625 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626
Bram Moolenaarbd743252010-10-20 21:23:33 +020015627 }
15628 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15629 {
15630 /* Return a dictionary. */
15631 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15632 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15633 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634
Bram Moolenaarbd743252010-10-20 21:23:33 +020015635 dict_add_nr_str(dict, "lhs", 0L, lhs);
15636 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15637 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15638 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15639 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15640 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15641 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015642 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015643 dict_add_nr_str(dict, "mode", 0L, mapmode);
15644
15645 vim_free(lhs);
15646 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
15648}
15649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015650#ifdef FEAT_FLOAT
15651/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015652 * "log()" function
15653 */
15654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015655f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015656{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015657 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015658
15659 rettv->v_type = VAR_FLOAT;
15660 if (get_float_arg(argvars, &f) == OK)
15661 rettv->vval.v_float = log(f);
15662 else
15663 rettv->vval.v_float = 0.0;
15664}
15665
15666/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015667 * "log10()" function
15668 */
15669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015670f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015671{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015672 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015673
15674 rettv->v_type = VAR_FLOAT;
15675 if (get_float_arg(argvars, &f) == OK)
15676 rettv->vval.v_float = log10(f);
15677 else
15678 rettv->vval.v_float = 0.0;
15679}
15680#endif
15681
Bram Moolenaar1dced572012-04-05 16:54:08 +020015682#ifdef FEAT_LUA
15683/*
15684 * "luaeval()" function
15685 */
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015688{
15689 char_u *str;
15690 char_u buf[NUMBUFLEN];
15691
15692 str = get_tv_string_buf(&argvars[0], buf);
15693 do_luaeval(str, argvars + 1, rettv);
15694}
15695#endif
15696
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 * "map()" function
15699 */
15700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015701f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015702{
15703 filter_map(argvars, rettv, TRUE);
15704}
15705
15706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 */
15709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015710f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015712 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713}
15714
15715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 */
15718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015719f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015721 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722}
15723
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015724static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
15726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015727find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015729 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015730 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015731 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 char_u *pat;
15733 regmatch_T regmatch;
15734 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015735 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 char_u *save_cpo;
15737 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015738 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015739 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015740 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015741 list_T *l = NULL;
15742 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015743 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
15746 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15747 save_cpo = p_cpo;
15748 p_cpo = (char_u *)"";
15749
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015750 rettv->vval.v_number = -1;
15751 if (type == 3)
15752 {
15753 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015754 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015756 }
15757 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 rettv->v_type = VAR_STRING;
15760 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015763 if (argvars[0].v_type == VAR_LIST)
15764 {
15765 if ((l = argvars[0].vval.v_list) == NULL)
15766 goto theend;
15767 li = l->lv_first;
15768 }
15769 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015770 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015771 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015772 len = (long)STRLEN(str);
15773 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015775 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15776 if (pat == NULL)
15777 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015778
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015781 int error = FALSE;
15782
15783 start = get_tv_number_chk(&argvars[2], &error);
15784 if (error)
15785 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015786 if (l != NULL)
15787 {
15788 li = list_find(l, start);
15789 if (li == NULL)
15790 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015791 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015792 }
15793 else
15794 {
15795 if (start < 0)
15796 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015797 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015798 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015799 /* When "count" argument is there ignore matches before "start",
15800 * otherwise skip part of the string. Differs when pattern is "^"
15801 * or "\<". */
15802 if (argvars[3].v_type != VAR_UNKNOWN)
15803 startcol = start;
15804 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015805 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015806 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015807 len -= start;
15808 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015809 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015811 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015812 nth = get_tv_number_chk(&argvars[3], &error);
15813 if (error)
15814 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 }
15816
15817 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15818 if (regmatch.regprog != NULL)
15819 {
15820 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015821
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015822 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015823 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015824 if (l != NULL)
15825 {
15826 if (li == NULL)
15827 {
15828 match = FALSE;
15829 break;
15830 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015831 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015832 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015833 if (str == NULL)
15834 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015835 }
15836
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015837 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015838
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015839 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015840 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015841 if (l == NULL && !match)
15842 break;
15843
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015844 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015845 if (l != NULL)
15846 {
15847 li = li->li_next;
15848 ++idx;
15849 }
15850 else
15851 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015852#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015853 startcol = (colnr_T)(regmatch.startp[0]
15854 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015855#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015856 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015857#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015858 if (startcol > (colnr_T)len
15859 || str + startcol <= regmatch.startp[0])
15860 {
15861 match = FALSE;
15862 break;
15863 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015864 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015865 }
15866
15867 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015869 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015870 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015871 int i;
15872
15873 /* return list with matched string and submatches */
15874 for (i = 0; i < NSUBEXP; ++i)
15875 {
15876 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015877 {
15878 if (list_append_string(rettv->vval.v_list,
15879 (char_u *)"", 0) == FAIL)
15880 break;
15881 }
15882 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015883 regmatch.startp[i],
15884 (int)(regmatch.endp[i] - regmatch.startp[i]))
15885 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015886 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 }
15888 }
15889 else if (type == 2)
15890 {
15891 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015892 if (l != NULL)
15893 copy_tv(&li->li_tv, rettv);
15894 else
15895 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015897 }
15898 else if (l != NULL)
15899 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 else
15901 {
15902 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015903 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 (varnumber_T)(regmatch.startp[0] - str);
15905 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015908 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 }
15910 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015911 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 }
15913
15914theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015915 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 p_cpo = save_cpo;
15917}
15918
15919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 * "match()" function
15921 */
15922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015923f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015924{
15925 find_some_match(argvars, rettv, 1);
15926}
15927
15928/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015929 * "matchadd()" function
15930 */
15931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015932f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015933{
15934#ifdef FEAT_SEARCH_EXTRA
15935 char_u buf[NUMBUFLEN];
15936 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15937 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15938 int prio = 10; /* default priority */
15939 int id = -1;
15940 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015941 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015942
15943 rettv->vval.v_number = -1;
15944
15945 if (grp == NULL || pat == NULL)
15946 return;
15947 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015948 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015949 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015950 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015951 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015952 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015953 if (argvars[4].v_type != VAR_UNKNOWN)
15954 {
15955 if (argvars[4].v_type != VAR_DICT)
15956 {
15957 EMSG(_(e_dictreq));
15958 return;
15959 }
15960 if (dict_find(argvars[4].vval.v_dict,
15961 (char_u *)"conceal", -1) != NULL)
15962 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15963 (char_u *)"conceal", FALSE);
15964 }
15965 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015966 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015967 if (error == TRUE)
15968 return;
15969 if (id >= 1 && id <= 3)
15970 {
15971 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15972 return;
15973 }
15974
Bram Moolenaar6561d522015-07-21 15:48:27 +020015975 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15976 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015977#endif
15978}
15979
15980/*
15981 * "matchaddpos()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015985{
15986#ifdef FEAT_SEARCH_EXTRA
15987 char_u buf[NUMBUFLEN];
15988 char_u *group;
15989 int prio = 10;
15990 int id = -1;
15991 int error = FALSE;
15992 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015993 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015994
15995 rettv->vval.v_number = -1;
15996
15997 group = get_tv_string_buf_chk(&argvars[0], buf);
15998 if (group == NULL)
15999 return;
16000
16001 if (argvars[1].v_type != VAR_LIST)
16002 {
16003 EMSG2(_(e_listarg), "matchaddpos()");
16004 return;
16005 }
16006 l = argvars[1].vval.v_list;
16007 if (l == NULL)
16008 return;
16009
16010 if (argvars[2].v_type != VAR_UNKNOWN)
16011 {
16012 prio = get_tv_number_chk(&argvars[2], &error);
16013 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016014 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016015 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016016 if (argvars[4].v_type != VAR_UNKNOWN)
16017 {
16018 if (argvars[4].v_type != VAR_DICT)
16019 {
16020 EMSG(_(e_dictreq));
16021 return;
16022 }
16023 if (dict_find(argvars[4].vval.v_dict,
16024 (char_u *)"conceal", -1) != NULL)
16025 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16026 (char_u *)"conceal", FALSE);
16027 }
16028 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016029 }
16030 if (error == TRUE)
16031 return;
16032
16033 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16034 if (id == 1 || id == 2)
16035 {
16036 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16037 return;
16038 }
16039
Bram Moolenaar6561d522015-07-21 15:48:27 +020016040 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16041 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016042#endif
16043}
16044
16045/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016046 * "matcharg()" function
16047 */
16048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016049f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016050{
16051 if (rettv_list_alloc(rettv) == OK)
16052 {
16053#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016054 int id = get_tv_number(&argvars[0]);
16055 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016056
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016057 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016058 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016059 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16060 {
16061 list_append_string(rettv->vval.v_list,
16062 syn_id2name(m->hlg_id), -1);
16063 list_append_string(rettv->vval.v_list, m->pattern, -1);
16064 }
16065 else
16066 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016067 list_append_string(rettv->vval.v_list, NULL, -1);
16068 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016069 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016070 }
16071#endif
16072 }
16073}
16074
16075/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076 * "matchdelete()" function
16077 */
16078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016079f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016080{
16081#ifdef FEAT_SEARCH_EXTRA
16082 rettv->vval.v_number = match_delete(curwin,
16083 (int)get_tv_number(&argvars[0]), TRUE);
16084#endif
16085}
16086
16087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088 * "matchend()" function
16089 */
16090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016091f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092{
16093 find_some_match(argvars, rettv, 0);
16094}
16095
16096/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016097 * "matchlist()" function
16098 */
16099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016100f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016101{
16102 find_some_match(argvars, rettv, 3);
16103}
16104
16105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106 * "matchstr()" function
16107 */
16108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016109f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016110{
16111 find_some_match(argvars, rettv, 2);
16112}
16113
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016114static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016115
16116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016117max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016118{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016119 long n = 0;
16120 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016121 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016122
16123 if (argvars[0].v_type == VAR_LIST)
16124 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016125 list_T *l;
16126 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016127
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016128 l = argvars[0].vval.v_list;
16129 if (l != NULL)
16130 {
16131 li = l->lv_first;
16132 if (li != NULL)
16133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016135 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016136 {
16137 li = li->li_next;
16138 if (li == NULL)
16139 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016141 if (domax ? i > n : i < n)
16142 n = i;
16143 }
16144 }
16145 }
16146 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016147 else if (argvars[0].v_type == VAR_DICT)
16148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016150 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016152 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016153
16154 d = argvars[0].vval.v_dict;
16155 if (d != NULL)
16156 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016157 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016158 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016160 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016161 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016162 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016163 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016164 if (first)
16165 {
16166 n = i;
16167 first = FALSE;
16168 }
16169 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016170 n = i;
16171 }
16172 }
16173 }
16174 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016175 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016176 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016177 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016178}
16179
16180/*
16181 * "max()" function
16182 */
16183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016184f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016185{
16186 max_min(argvars, rettv, TRUE);
16187}
16188
16189/*
16190 * "min()" function
16191 */
16192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016193f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016194{
16195 max_min(argvars, rettv, FALSE);
16196}
16197
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016198static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016199
16200/*
16201 * Create the directory in which "dir" is located, and higher levels when
16202 * needed.
16203 */
16204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016205mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016206{
16207 char_u *p;
16208 char_u *updir;
16209 int r = FAIL;
16210
16211 /* Get end of directory name in "dir".
16212 * We're done when it's "/" or "c:/". */
16213 p = gettail_sep(dir);
16214 if (p <= get_past_head(dir))
16215 return OK;
16216
16217 /* If the directory exists we're done. Otherwise: create it.*/
16218 updir = vim_strnsave(dir, (int)(p - dir));
16219 if (updir == NULL)
16220 return FAIL;
16221 if (mch_isdir(updir))
16222 r = OK;
16223 else if (mkdir_recurse(updir, prot) == OK)
16224 r = vim_mkdir_emsg(updir, prot);
16225 vim_free(updir);
16226 return r;
16227}
16228
16229#ifdef vim_mkdir
16230/*
16231 * "mkdir()" function
16232 */
16233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016234f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016235{
16236 char_u *dir;
16237 char_u buf[NUMBUFLEN];
16238 int prot = 0755;
16239
16240 rettv->vval.v_number = FAIL;
16241 if (check_restricted() || check_secure())
16242 return;
16243
16244 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016245 if (*dir == NUL)
16246 rettv->vval.v_number = FAIL;
16247 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016248 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016249 if (*gettail(dir) == NUL)
16250 /* remove trailing slashes */
16251 *gettail_sep(dir) = NUL;
16252
16253 if (argvars[1].v_type != VAR_UNKNOWN)
16254 {
16255 if (argvars[2].v_type != VAR_UNKNOWN)
16256 prot = get_tv_number_chk(&argvars[2], NULL);
16257 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16258 mkdir_recurse(dir, prot);
16259 }
16260 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016261 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016262}
16263#endif
16264
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 * "mode()" function
16267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016269f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016271 char_u buf[3];
16272
16273 buf[1] = NUL;
16274 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 if (VIsual_active)
16277 {
16278 if (VIsual_select)
16279 buf[0] = VIsual_mode + 's' - 'v';
16280 else
16281 buf[0] = VIsual_mode;
16282 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016283 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016284 || State == CONFIRM)
16285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016287 if (State == ASKMORE)
16288 buf[1] = 'm';
16289 else if (State == CONFIRM)
16290 buf[1] = '?';
16291 }
16292 else if (State == EXTERNCMD)
16293 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 else if (State & INSERT)
16295 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016296#ifdef FEAT_VREPLACE
16297 if (State & VREPLACE_FLAG)
16298 {
16299 buf[0] = 'R';
16300 buf[1] = 'v';
16301 }
16302 else
16303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 if (State & REPLACE_FLAG)
16305 buf[0] = 'R';
16306 else
16307 buf[0] = 'i';
16308 }
16309 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016312 if (exmode_active)
16313 buf[1] = 'v';
16314 }
16315 else if (exmode_active)
16316 {
16317 buf[0] = 'c';
16318 buf[1] = 'e';
16319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323 if (finish_op)
16324 buf[1] = 'o';
16325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016327 /* Clear out the minor mode when the argument is not a non-zero number or
16328 * non-empty string. */
16329 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016330 buf[1] = NUL;
16331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016332 rettv->vval.v_string = vim_strsave(buf);
16333 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334}
16335
Bram Moolenaar429fa852013-04-15 12:27:36 +020016336#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016337/*
16338 * "mzeval()" function
16339 */
16340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016341f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016342{
16343 char_u *str;
16344 char_u buf[NUMBUFLEN];
16345
16346 str = get_tv_string_buf(&argvars[0], buf);
16347 do_mzeval(str, rettv);
16348}
Bram Moolenaar75676462013-01-30 14:55:42 +010016349
16350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016351mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016352{
16353 typval_T argvars[3];
16354
16355 argvars[0].v_type = VAR_STRING;
16356 argvars[0].vval.v_string = name;
16357 copy_tv(args, &argvars[1]);
16358 argvars[2].v_type = VAR_UNKNOWN;
16359 f_call(argvars, rettv);
16360 clear_tv(&argvars[1]);
16361}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016362#endif
16363
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 * "nextnonblank()" function
16366 */
16367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369{
16370 linenr_T lnum;
16371
16372 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016374 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 {
16376 lnum = 0;
16377 break;
16378 }
16379 if (*skipwhite(ml_get(lnum)) != NUL)
16380 break;
16381 }
16382 rettv->vval.v_number = lnum;
16383}
16384
16385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 * "nr2char()" function
16387 */
16388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016389f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
16391 char_u buf[NUMBUFLEN];
16392
16393#ifdef FEAT_MBYTE
16394 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016395 {
16396 int utf8 = 0;
16397
16398 if (argvars[1].v_type != VAR_UNKNOWN)
16399 utf8 = get_tv_number_chk(&argvars[1], NULL);
16400 if (utf8)
16401 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16402 else
16403 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
16406#endif
16407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016408 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 buf[1] = NUL;
16410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016411 rettv->v_type = VAR_STRING;
16412 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016413}
16414
16415/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016416 * "or(expr, expr)" function
16417 */
16418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016419f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016420{
16421 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16422 | get_tv_number_chk(&argvars[1], NULL);
16423}
16424
16425/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016426 * "pathshorten()" function
16427 */
16428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016429f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016430{
16431 char_u *p;
16432
16433 rettv->v_type = VAR_STRING;
16434 p = get_tv_string_chk(&argvars[0]);
16435 if (p == NULL)
16436 rettv->vval.v_string = NULL;
16437 else
16438 {
16439 p = vim_strsave(p);
16440 rettv->vval.v_string = p;
16441 if (p != NULL)
16442 shorten_dir(p);
16443 }
16444}
16445
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016446#ifdef FEAT_PERL
16447/*
16448 * "perleval()" function
16449 */
16450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016451f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016452{
16453 char_u *str;
16454 char_u buf[NUMBUFLEN];
16455
16456 str = get_tv_string_buf(&argvars[0], buf);
16457 do_perleval(str, rettv);
16458}
16459#endif
16460
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016461#ifdef FEAT_FLOAT
16462/*
16463 * "pow()" function
16464 */
16465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016466f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016468 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016469
16470 rettv->v_type = VAR_FLOAT;
16471 if (get_float_arg(argvars, &fx) == OK
16472 && get_float_arg(&argvars[1], &fy) == OK)
16473 rettv->vval.v_float = pow(fx, fy);
16474 else
16475 rettv->vval.v_float = 0.0;
16476}
16477#endif
16478
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 * "prevnonblank()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484{
16485 linenr_T lnum;
16486
16487 lnum = get_tv_lnum(argvars);
16488 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16489 lnum = 0;
16490 else
16491 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16492 --lnum;
16493 rettv->vval.v_number = lnum;
16494}
16495
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016496/* This dummy va_list is here because:
16497 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16498 * - locally in the function results in a "used before set" warning
16499 * - using va_start() to initialize it gives "function with fixed args" error */
16500static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016501
Bram Moolenaar8c711452005-01-14 21:53:12 +000016502/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016503 * "printf()" function
16504 */
16505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016506f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016507{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016508 char_u buf[NUMBUFLEN];
16509 int len;
16510 char_u *s;
16511 int saved_did_emsg = did_emsg;
16512 char *fmt;
16513
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016514 rettv->v_type = VAR_STRING;
16515 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016516
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016517 /* Get the required length, allocate the buffer and do it for real. */
16518 did_emsg = FALSE;
16519 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16520 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16521 if (!did_emsg)
16522 {
16523 s = alloc(len + 1);
16524 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016525 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016526 rettv->vval.v_string = s;
16527 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016528 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016529 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016530 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016531}
16532
16533/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016534 * "pumvisible()" function
16535 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016537f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016538{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016539#ifdef FEAT_INS_EXPAND
16540 if (pum_visible())
16541 rettv->vval.v_number = 1;
16542#endif
16543}
16544
Bram Moolenaardb913952012-06-29 12:54:53 +020016545#ifdef FEAT_PYTHON3
16546/*
16547 * "py3eval()" function
16548 */
16549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016550f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016551{
16552 char_u *str;
16553 char_u buf[NUMBUFLEN];
16554
16555 str = get_tv_string_buf(&argvars[0], buf);
16556 do_py3eval(str, rettv);
16557}
16558#endif
16559
16560#ifdef FEAT_PYTHON
16561/*
16562 * "pyeval()" function
16563 */
16564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016565f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016566{
16567 char_u *str;
16568 char_u buf[NUMBUFLEN];
16569
16570 str = get_tv_string_buf(&argvars[0], buf);
16571 do_pyeval(str, rettv);
16572}
16573#endif
16574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016576 * "range()" function
16577 */
16578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016579f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016580{
16581 long start;
16582 long end;
16583 long stride = 1;
16584 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016587 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016588 if (argvars[1].v_type == VAR_UNKNOWN)
16589 {
16590 end = start - 1;
16591 start = 0;
16592 }
16593 else
16594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016596 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016598 }
16599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016600 if (error)
16601 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016602 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016603 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016604 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016605 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606 else
16607 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016608 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016609 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016610 if (list_append_number(rettv->vval.v_list,
16611 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016612 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 }
16614}
16615
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016616/*
16617 * "readfile()" function
16618 */
16619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016620f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016621{
16622 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016623 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624 char_u *fname;
16625 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16627 int io_size = sizeof(buf);
16628 int readlen; /* size of last fread() */
16629 char_u *prev = NULL; /* previously read bytes, if any */
16630 long prevlen = 0; /* length of data in prev */
16631 long prevsize = 0; /* size of prev buffer */
16632 long maxline = MAXLNUM;
16633 long cnt = 0;
16634 char_u *p; /* position in buf */
16635 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016636
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016637 if (argvars[1].v_type != VAR_UNKNOWN)
16638 {
16639 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16640 binary = TRUE;
16641 if (argvars[2].v_type != VAR_UNKNOWN)
16642 maxline = get_tv_number(&argvars[2]);
16643 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016645 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647
16648 /* Always open the file in binary mode, library functions have a mind of
16649 * their own about CR-LF conversion. */
16650 fname = get_tv_string(&argvars[0]);
16651 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16652 {
16653 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16654 return;
16655 }
16656
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016657 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016658 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016659 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661 /* This for loop processes what was read, but is also entered at end
16662 * of file so that either:
16663 * - an incomplete line gets written
16664 * - a "binary" file gets an empty line at the end if it ends in a
16665 * newline. */
16666 for (p = buf, start = buf;
16667 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16668 ++p)
16669 {
16670 if (*p == '\n' || readlen <= 0)
16671 {
16672 listitem_T *li;
16673 char_u *s = NULL;
16674 long_u len = p - start;
16675
16676 /* Finished a line. Remove CRs before NL. */
16677 if (readlen > 0 && !binary)
16678 {
16679 while (len > 0 && start[len - 1] == '\r')
16680 --len;
16681 /* removal may cross back to the "prev" string */
16682 if (len == 0)
16683 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16684 --prevlen;
16685 }
16686 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016687 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016688 else
16689 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016690 /* Change "prev" buffer to be the right size. This way
16691 * the bytes are only copied once, and very long lines are
16692 * allocated only once. */
16693 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016694 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016695 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016697 prev = NULL; /* the list will own the string */
16698 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016699 }
16700 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016701 if (s == NULL)
16702 {
16703 do_outofmem_msg((long_u) prevlen + len + 1);
16704 failed = TRUE;
16705 break;
16706 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016707
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709 {
16710 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016711 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016712 break;
16713 }
16714 li->li_tv.v_type = VAR_STRING;
16715 li->li_tv.v_lock = 0;
16716 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016717 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016718
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016719 start = p + 1; /* step over newline */
16720 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016721 break;
16722 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016723 else if (*p == NUL)
16724 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016725#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016726 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16727 * when finding the BF and check the previous two bytes. */
16728 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016729 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016730 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16731 * + 1, these may be in the "prev" string. */
16732 char_u back1 = p >= buf + 1 ? p[-1]
16733 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16734 char_u back2 = p >= buf + 2 ? p[-2]
16735 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16736 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016737
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016738 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016739 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 char_u *dest = p - 2;
16741
16742 /* Usually a BOM is at the beginning of a file, and so at
16743 * the beginning of a line; then we can just step over it.
16744 */
16745 if (start == dest)
16746 start = p + 1;
16747 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016748 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016749 /* have to shuffle buf to close gap */
16750 int adjust_prevlen = 0;
16751
16752 if (dest < buf)
16753 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016754 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016755 dest = buf;
16756 }
16757 if (readlen > p - buf + 1)
16758 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16759 readlen -= 3 - adjust_prevlen;
16760 prevlen -= adjust_prevlen;
16761 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016762 }
16763 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016764 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016765#endif
16766 } /* for */
16767
16768 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16769 break;
16770 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 /* There's part of a line in buf, store it in "prev". */
16773 if (p - start + prevlen >= prevsize)
16774 {
16775 /* need bigger "prev" buffer */
16776 char_u *newprev;
16777
16778 /* A common use case is ordinary text files and "prev" gets a
16779 * fragment of a line, so the first allocation is made
16780 * small, to avoid repeatedly 'allocing' large and
16781 * 'reallocing' small. */
16782 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016783 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016784 else
16785 {
16786 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016787 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016788 prevsize = grow50pc > growmin ? grow50pc : growmin;
16789 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016790 newprev = prev == NULL ? alloc(prevsize)
16791 : vim_realloc(prev, prevsize);
16792 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016793 {
16794 do_outofmem_msg((long_u)prevsize);
16795 failed = TRUE;
16796 break;
16797 }
16798 prev = newprev;
16799 }
16800 /* Add the line part to end of "prev". */
16801 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016802 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016804 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016805
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016806 /*
16807 * For a negative line count use only the lines at the end of the file,
16808 * free the rest.
16809 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016810 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016811 while (cnt > -maxline)
16812 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016813 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016814 --cnt;
16815 }
16816
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016817 if (failed)
16818 {
16819 list_free(rettv->vval.v_list, TRUE);
16820 /* readfile doc says an empty list is returned on error */
16821 rettv->vval.v_list = list_alloc();
16822 }
16823
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016824 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016825 fclose(fd);
16826}
16827
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016828#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016829static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016830
16831/*
16832 * Convert a List to proftime_T.
16833 * Return FAIL when there is something wrong.
16834 */
16835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016836list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016837{
16838 long n1, n2;
16839 int error = FALSE;
16840
16841 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16842 || arg->vval.v_list->lv_len != 2)
16843 return FAIL;
16844 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16845 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16846# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016847 tm->HighPart = n1;
16848 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016849# else
16850 tm->tv_sec = n1;
16851 tm->tv_usec = n2;
16852# endif
16853 return error ? FAIL : OK;
16854}
16855#endif /* FEAT_RELTIME */
16856
16857/*
16858 * "reltime()" function
16859 */
16860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016861f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016862{
16863#ifdef FEAT_RELTIME
16864 proftime_T res;
16865 proftime_T start;
16866
16867 if (argvars[0].v_type == VAR_UNKNOWN)
16868 {
16869 /* No arguments: get current time. */
16870 profile_start(&res);
16871 }
16872 else if (argvars[1].v_type == VAR_UNKNOWN)
16873 {
16874 if (list2proftime(&argvars[0], &res) == FAIL)
16875 return;
16876 profile_end(&res);
16877 }
16878 else
16879 {
16880 /* Two arguments: compute the difference. */
16881 if (list2proftime(&argvars[0], &start) == FAIL
16882 || list2proftime(&argvars[1], &res) == FAIL)
16883 return;
16884 profile_sub(&res, &start);
16885 }
16886
16887 if (rettv_list_alloc(rettv) == OK)
16888 {
16889 long n1, n2;
16890
16891# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016892 n1 = res.HighPart;
16893 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016894# else
16895 n1 = res.tv_sec;
16896 n2 = res.tv_usec;
16897# endif
16898 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16899 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16900 }
16901#endif
16902}
16903
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016904#ifdef FEAT_FLOAT
16905/*
16906 * "reltimefloat()" function
16907 */
16908 static void
16909f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16910{
16911# ifdef FEAT_RELTIME
16912 proftime_T tm;
16913# endif
16914
16915 rettv->v_type = VAR_FLOAT;
16916 rettv->vval.v_float = 0;
16917# ifdef FEAT_RELTIME
16918 if (list2proftime(&argvars[0], &tm) == OK)
16919 rettv->vval.v_float = profile_float(&tm);
16920# endif
16921}
16922#endif
16923
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016924/*
16925 * "reltimestr()" function
16926 */
16927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016928f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016929{
16930#ifdef FEAT_RELTIME
16931 proftime_T tm;
16932#endif
16933
16934 rettv->v_type = VAR_STRING;
16935 rettv->vval.v_string = NULL;
16936#ifdef FEAT_RELTIME
16937 if (list2proftime(&argvars[0], &tm) == OK)
16938 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16939#endif
16940}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016941
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016943static void make_connection(void);
16944static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945
16946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948{
16949 if (X_DISPLAY == NULL
16950# ifdef FEAT_GUI
16951 && !gui.in_use
16952# endif
16953 )
16954 {
16955 x_force_connect = TRUE;
16956 setup_term_clip();
16957 x_force_connect = FALSE;
16958 }
16959}
16960
16961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016962check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963{
16964 make_connection();
16965 if (X_DISPLAY == NULL)
16966 {
16967 EMSG(_("E240: No connection to Vim server"));
16968 return FAIL;
16969 }
16970 return OK;
16971}
16972#endif
16973
16974#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016975static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976
16977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016978remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979{
16980 char_u *server_name;
16981 char_u *keys;
16982 char_u *r = NULL;
16983 char_u buf[NUMBUFLEN];
16984# ifdef WIN32
16985 HWND w;
16986# else
16987 Window w;
16988# endif
16989
16990 if (check_restricted() || check_secure())
16991 return;
16992
16993# ifdef FEAT_X11
16994 if (check_connection() == FAIL)
16995 return;
16996# endif
16997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 server_name = get_tv_string_chk(&argvars[0]);
16999 if (server_name == NULL)
17000 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 keys = get_tv_string_buf(&argvars[1], buf);
17002# ifdef WIN32
17003 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17004# else
17005 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17006 < 0)
17007# endif
17008 {
17009 if (r != NULL)
17010 EMSG(r); /* sending worked but evaluation failed */
17011 else
17012 EMSG2(_("E241: Unable to send to %s"), server_name);
17013 return;
17014 }
17015
17016 rettv->vval.v_string = r;
17017
17018 if (argvars[2].v_type != VAR_UNKNOWN)
17019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017021 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017022 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017024 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 v.di_tv.v_type = VAR_STRING;
17026 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 idvar = get_tv_string_chk(&argvars[2]);
17028 if (idvar != NULL)
17029 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 }
17032}
17033#endif
17034
17035/*
17036 * "remote_expr()" function
17037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017039f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040{
17041 rettv->v_type = VAR_STRING;
17042 rettv->vval.v_string = NULL;
17043#ifdef FEAT_CLIENTSERVER
17044 remote_common(argvars, rettv, TRUE);
17045#endif
17046}
17047
17048/*
17049 * "remote_foreground()" function
17050 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017052f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054#ifdef FEAT_CLIENTSERVER
17055# ifdef WIN32
17056 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 {
17058 char_u *server_name = get_tv_string_chk(&argvars[0]);
17059
17060 if (server_name != NULL)
17061 serverForeground(server_name);
17062 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063# else
17064 /* Send a foreground() expression to the server. */
17065 argvars[1].v_type = VAR_STRING;
17066 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17067 argvars[2].v_type = VAR_UNKNOWN;
17068 remote_common(argvars, rettv, TRUE);
17069 vim_free(argvars[1].vval.v_string);
17070# endif
17071#endif
17072}
17073
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017075f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076{
17077#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017078 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 char_u *s = NULL;
17080# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017081 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084
17085 if (check_restricted() || check_secure())
17086 {
17087 rettv->vval.v_number = -1;
17088 return;
17089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017090 serverid = get_tv_string_chk(&argvars[0]);
17091 if (serverid == NULL)
17092 {
17093 rettv->vval.v_number = -1;
17094 return; /* type error; errmsg already given */
17095 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017097 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 if (n == 0)
17099 rettv->vval.v_number = -1;
17100 else
17101 {
17102 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17103 rettv->vval.v_number = (s != NULL);
17104 }
17105# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 if (check_connection() == FAIL)
17107 return;
17108
17109 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111# endif
17112
17113 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017115 char_u *retvar;
17116
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 v.di_tv.v_type = VAR_STRING;
17118 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119 retvar = get_tv_string_chk(&argvars[1]);
17120 if (retvar != NULL)
17121 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 }
17124#else
17125 rettv->vval.v_number = -1;
17126#endif
17127}
17128
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017130f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131{
17132 char_u *r = NULL;
17133
17134#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 char_u *serverid = get_tv_string_chk(&argvars[0]);
17136
17137 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 {
17139# ifdef WIN32
17140 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017141 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017143 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 if (n != 0)
17145 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17146 if (r == NULL)
17147# else
17148 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150# endif
17151 EMSG(_("E277: Unable to read a server reply"));
17152 }
17153#endif
17154 rettv->v_type = VAR_STRING;
17155 rettv->vval.v_string = r;
17156}
17157
17158/*
17159 * "remote_send()" function
17160 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017162f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163{
17164 rettv->v_type = VAR_STRING;
17165 rettv->vval.v_string = NULL;
17166#ifdef FEAT_CLIENTSERVER
17167 remote_common(argvars, rettv, FALSE);
17168#endif
17169}
17170
17171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017172 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173 */
17174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017175f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176{
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 list_T *l;
17178 listitem_T *item, *item2;
17179 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017180 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017181 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017182 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017183 dict_T *d;
17184 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017185 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017186
Bram Moolenaar8c711452005-01-14 21:53:12 +000017187 if (argvars[0].v_type == VAR_DICT)
17188 {
17189 if (argvars[2].v_type != VAR_UNKNOWN)
17190 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017191 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017192 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017194 key = get_tv_string_chk(&argvars[1]);
17195 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017197 di = dict_find(d, key, -1);
17198 if (di == NULL)
17199 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017200 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17201 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017202 {
17203 *rettv = di->di_tv;
17204 init_tv(&di->di_tv);
17205 dictitem_remove(d, di);
17206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017208 }
17209 }
17210 else if (argvars[0].v_type != VAR_LIST)
17211 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017212 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017213 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 int error = FALSE;
17216
17217 idx = get_tv_number_chk(&argvars[1], &error);
17218 if (error)
17219 ; /* type error: do nothing, errmsg already given */
17220 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 EMSGN(_(e_listidx), idx);
17222 else
17223 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017224 if (argvars[2].v_type == VAR_UNKNOWN)
17225 {
17226 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017227 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017228 *rettv = item->li_tv;
17229 vim_free(item);
17230 }
17231 else
17232 {
17233 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 end = get_tv_number_chk(&argvars[2], &error);
17235 if (error)
17236 ; /* type error: do nothing */
17237 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017238 EMSGN(_(e_listidx), end);
17239 else
17240 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017241 int cnt = 0;
17242
17243 for (li = item; li != NULL; li = li->li_next)
17244 {
17245 ++cnt;
17246 if (li == item2)
17247 break;
17248 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017249 if (li == NULL) /* didn't find "item2" after "item" */
17250 EMSG(_(e_invrange));
17251 else
17252 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017253 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017254 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017255 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017256 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017257 l->lv_first = item;
17258 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017259 item->li_prev = NULL;
17260 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017261 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017262 }
17263 }
17264 }
17265 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017266 }
17267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
17271 * "rename({from}, {to})" function
17272 */
17273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017274f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275{
17276 char_u buf[NUMBUFLEN];
17277
17278 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17282 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283}
17284
17285/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017286 * "repeat()" function
17287 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017289f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017290{
17291 char_u *p;
17292 int n;
17293 int slen;
17294 int len;
17295 char_u *r;
17296 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017297
17298 n = get_tv_number(&argvars[1]);
17299 if (argvars[0].v_type == VAR_LIST)
17300 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017301 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017302 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017303 if (list_extend(rettv->vval.v_list,
17304 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017305 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 }
17307 else
17308 {
17309 p = get_tv_string(&argvars[0]);
17310 rettv->v_type = VAR_STRING;
17311 rettv->vval.v_string = NULL;
17312
17313 slen = (int)STRLEN(p);
17314 len = slen * n;
17315 if (len <= 0)
17316 return;
17317
17318 r = alloc(len + 1);
17319 if (r != NULL)
17320 {
17321 for (i = 0; i < n; i++)
17322 mch_memmove(r + i * slen, p, (size_t)slen);
17323 r[len] = NUL;
17324 }
17325
17326 rettv->vval.v_string = r;
17327 }
17328}
17329
17330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 * "resolve()" function
17332 */
17333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017334f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017337#ifdef HAVE_READLINK
17338 char_u *buf = NULL;
17339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342#ifdef FEAT_SHORTCUT
17343 {
17344 char_u *v = NULL;
17345
17346 v = mch_resolve_shortcut(p);
17347 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 }
17352#else
17353# ifdef HAVE_READLINK
17354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 char_u *cpy;
17356 int len;
17357 char_u *remain = NULL;
17358 char_u *q;
17359 int is_relative_to_current = FALSE;
17360 int has_trailing_pathsep = FALSE;
17361 int limit = 100;
17362
17363 p = vim_strsave(p);
17364
17365 if (p[0] == '.' && (vim_ispathsep(p[1])
17366 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17367 is_relative_to_current = TRUE;
17368
17369 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017370 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017373 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
17376 q = getnextcomp(p);
17377 if (*q != NUL)
17378 {
17379 /* Separate the first path component in "p", and keep the
17380 * remainder (beginning with the path separator). */
17381 remain = vim_strsave(q - 1);
17382 q[-1] = NUL;
17383 }
17384
Bram Moolenaard9462e32011-04-11 21:35:11 +020017385 buf = alloc(MAXPATHL + 1);
17386 if (buf == NULL)
17387 goto fail;
17388
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 for (;;)
17390 {
17391 for (;;)
17392 {
17393 len = readlink((char *)p, (char *)buf, MAXPATHL);
17394 if (len <= 0)
17395 break;
17396 buf[len] = NUL;
17397
17398 if (limit-- == 0)
17399 {
17400 vim_free(p);
17401 vim_free(remain);
17402 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 goto fail;
17405 }
17406
17407 /* Ensure that the result will have a trailing path separator
17408 * if the argument has one. */
17409 if (remain == NULL && has_trailing_pathsep)
17410 add_pathsep(buf);
17411
17412 /* Separate the first path component in the link value and
17413 * concatenate the remainders. */
17414 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17415 if (*q != NUL)
17416 {
17417 if (remain == NULL)
17418 remain = vim_strsave(q - 1);
17419 else
17420 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017421 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 if (cpy != NULL)
17423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 vim_free(remain);
17425 remain = cpy;
17426 }
17427 }
17428 q[-1] = NUL;
17429 }
17430
17431 q = gettail(p);
17432 if (q > p && *q == NUL)
17433 {
17434 /* Ignore trailing path separator. */
17435 q[-1] = NUL;
17436 q = gettail(p);
17437 }
17438 if (q > p && !mch_isFullName(buf))
17439 {
17440 /* symlink is relative to directory of argument */
17441 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17442 if (cpy != NULL)
17443 {
17444 STRCPY(cpy, p);
17445 STRCPY(gettail(cpy), buf);
17446 vim_free(p);
17447 p = cpy;
17448 }
17449 }
17450 else
17451 {
17452 vim_free(p);
17453 p = vim_strsave(buf);
17454 }
17455 }
17456
17457 if (remain == NULL)
17458 break;
17459
17460 /* Append the first path component of "remain" to "p". */
17461 q = getnextcomp(remain + 1);
17462 len = q - remain - (*q != NUL);
17463 cpy = vim_strnsave(p, STRLEN(p) + len);
17464 if (cpy != NULL)
17465 {
17466 STRNCAT(cpy, remain, len);
17467 vim_free(p);
17468 p = cpy;
17469 }
17470 /* Shorten "remain". */
17471 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017472 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 else
17474 {
17475 vim_free(remain);
17476 remain = NULL;
17477 }
17478 }
17479
17480 /* If the result is a relative path name, make it explicitly relative to
17481 * the current directory if and only if the argument had this form. */
17482 if (!vim_ispathsep(*p))
17483 {
17484 if (is_relative_to_current
17485 && *p != NUL
17486 && !(p[0] == '.'
17487 && (p[1] == NUL
17488 || vim_ispathsep(p[1])
17489 || (p[1] == '.'
17490 && (p[2] == NUL
17491 || vim_ispathsep(p[2]))))))
17492 {
17493 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017494 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 if (cpy != NULL)
17496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 vim_free(p);
17498 p = cpy;
17499 }
17500 }
17501 else if (!is_relative_to_current)
17502 {
17503 /* Strip leading "./". */
17504 q = p;
17505 while (q[0] == '.' && vim_ispathsep(q[1]))
17506 q += 2;
17507 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017508 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510 }
17511
17512 /* Ensure that the result will have no trailing path separator
17513 * if the argument had none. But keep "/" or "//". */
17514 if (!has_trailing_pathsep)
17515 {
17516 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017517 if (after_pathsep(p, q))
17518 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 }
17520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525# endif
17526#endif
17527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529
17530#ifdef HAVE_READLINK
17531fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017532 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
17537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 */
17540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017541f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542{
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 list_T *l;
17544 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 if (argvars[0].v_type != VAR_LIST)
17547 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017548 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017549 && !tv_check_lock(l->lv_lock,
17550 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 {
17552 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017553 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017554 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 while (li != NULL)
17556 {
17557 ni = li->li_prev;
17558 list_append(l, li);
17559 li = ni;
17560 }
17561 rettv->vval.v_list = l;
17562 rettv->v_type = VAR_LIST;
17563 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017564 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566}
17567
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017568#define SP_NOMOVE 0x01 /* don't move cursor */
17569#define SP_REPEAT 0x02 /* repeat to find outer pair */
17570#define SP_RETCOUNT 0x04 /* return matchcount */
17571#define SP_SETPCMARK 0x08 /* set previous context mark */
17572#define SP_START 0x10 /* accept match at start position */
17573#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17574#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017575#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017576
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017577static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578
17579/*
17580 * Get flags for a search function.
17581 * Possibly sets "p_ws".
17582 * Returns BACKWARD, FORWARD or zero (for an error).
17583 */
17584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017585get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586{
17587 int dir = FORWARD;
17588 char_u *flags;
17589 char_u nbuf[NUMBUFLEN];
17590 int mask;
17591
17592 if (varp->v_type != VAR_UNKNOWN)
17593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017594 flags = get_tv_string_buf_chk(varp, nbuf);
17595 if (flags == NULL)
17596 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 while (*flags != NUL)
17598 {
17599 switch (*flags)
17600 {
17601 case 'b': dir = BACKWARD; break;
17602 case 'w': p_ws = TRUE; break;
17603 case 'W': p_ws = FALSE; break;
17604 default: mask = 0;
17605 if (flagsp != NULL)
17606 switch (*flags)
17607 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017608 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017609 case 'e': mask = SP_END; break;
17610 case 'm': mask = SP_RETCOUNT; break;
17611 case 'n': mask = SP_NOMOVE; break;
17612 case 'p': mask = SP_SUBPAT; break;
17613 case 'r': mask = SP_REPEAT; break;
17614 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017615 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616 }
17617 if (mask == 0)
17618 {
17619 EMSG2(_(e_invarg2), flags);
17620 dir = 0;
17621 }
17622 else
17623 *flagsp |= mask;
17624 }
17625 if (dir == 0)
17626 break;
17627 ++flags;
17628 }
17629 }
17630 return dir;
17631}
17632
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017634 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017637search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017639 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 char_u *pat;
17641 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017642 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 int save_p_ws = p_ws;
17644 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017645 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017646 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017647 proftime_T tm;
17648#ifdef FEAT_RELTIME
17649 long time_limit = 0;
17650#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017651 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017652 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017655 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017656 if (dir == 0)
17657 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017658 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017659 if (flags & SP_START)
17660 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017661 if (flags & SP_END)
17662 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017663 if (flags & SP_COLUMN)
17664 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017665
Bram Moolenaar76929292008-01-06 19:07:36 +000017666 /* Optional arguments: line number to stop searching and timeout. */
17667 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017668 {
17669 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17670 if (lnum_stop < 0)
17671 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017672#ifdef FEAT_RELTIME
17673 if (argvars[3].v_type != VAR_UNKNOWN)
17674 {
17675 time_limit = get_tv_number_chk(&argvars[3], NULL);
17676 if (time_limit < 0)
17677 goto theend;
17678 }
17679#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017680 }
17681
Bram Moolenaar76929292008-01-06 19:07:36 +000017682#ifdef FEAT_RELTIME
17683 /* Set the time limit, if there is one. */
17684 profile_setlimit(time_limit, &tm);
17685#endif
17686
Bram Moolenaar231334e2005-07-25 20:46:57 +000017687 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017688 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017689 * Check to make sure only those flags are set.
17690 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17691 * flags cannot be set. Check for that condition also.
17692 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017693 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017694 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017697 goto theend;
17698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017700 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017701 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017702 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017703 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017705 if (flags & SP_SUBPAT)
17706 retval = subpatnum;
17707 else
17708 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017709 if (flags & SP_SETPCMARK)
17710 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017712 if (match_pos != NULL)
17713 {
17714 /* Store the match cursor position */
17715 match_pos->lnum = pos.lnum;
17716 match_pos->col = pos.col + 1;
17717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 /* "/$" will put the cursor after the end of the line, may need to
17719 * correct that here */
17720 check_cursor();
17721 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017722
17723 /* If 'n' flag is used: restore cursor position. */
17724 if (flags & SP_NOMOVE)
17725 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017726 else
17727 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017728theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017730
17731 return retval;
17732}
17733
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017734#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017735
17736/*
17737 * round() is not in C90, use ceil() or floor() instead.
17738 */
17739 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017740vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017741{
17742 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17743}
17744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017745/*
17746 * "round({float})" function
17747 */
17748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017749f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017750{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017751 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752
17753 rettv->v_type = VAR_FLOAT;
17754 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017755 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017756 else
17757 rettv->vval.v_float = 0.0;
17758}
17759#endif
17760
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017761/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017762 * "screenattr()" function
17763 */
17764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017765f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017766{
17767 int row;
17768 int col;
17769 int c;
17770
17771 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17772 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17773 if (row < 0 || row >= screen_Rows
17774 || col < 0 || col >= screen_Columns)
17775 c = -1;
17776 else
17777 c = ScreenAttrs[LineOffset[row] + col];
17778 rettv->vval.v_number = c;
17779}
17780
17781/*
17782 * "screenchar()" function
17783 */
17784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017785f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017786{
17787 int row;
17788 int col;
17789 int off;
17790 int c;
17791
17792 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17793 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17794 if (row < 0 || row >= screen_Rows
17795 || col < 0 || col >= screen_Columns)
17796 c = -1;
17797 else
17798 {
17799 off = LineOffset[row] + col;
17800#ifdef FEAT_MBYTE
17801 if (enc_utf8 && ScreenLinesUC[off] != 0)
17802 c = ScreenLinesUC[off];
17803 else
17804#endif
17805 c = ScreenLines[off];
17806 }
17807 rettv->vval.v_number = c;
17808}
17809
17810/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017811 * "screencol()" function
17812 *
17813 * First column is 1 to be consistent with virtcol().
17814 */
17815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017816f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017817{
17818 rettv->vval.v_number = screen_screencol() + 1;
17819}
17820
17821/*
17822 * "screenrow()" function
17823 */
17824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017825f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017826{
17827 rettv->vval.v_number = screen_screenrow() + 1;
17828}
17829
17830/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017831 * "search()" function
17832 */
17833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017835{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017836 int flags = 0;
17837
17838 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017842 * "searchdecl()" function
17843 */
17844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017845f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017846{
17847 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017848 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017849 int error = FALSE;
17850 char_u *name;
17851
17852 rettv->vval.v_number = 1; /* default: FAIL */
17853
17854 name = get_tv_string_chk(&argvars[0]);
17855 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017856 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017857 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017858 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17859 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17860 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017861 if (!error && name != NULL)
17862 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017863 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017864}
17865
17866/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017870searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871{
17872 char_u *spat, *mpat, *epat;
17873 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 int dir;
17876 int flags = 0;
17877 char_u nbuf1[NUMBUFLEN];
17878 char_u nbuf2[NUMBUFLEN];
17879 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017880 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017881 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017882 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 spat = get_tv_string_chk(&argvars[0]);
17886 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17887 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17888 if (spat == NULL || mpat == NULL || epat == NULL)
17889 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 /* Handle the optional fourth argument: flags */
17892 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017893 if (dir == 0)
17894 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017895
17896 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017897 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17898 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017899 if ((flags & (SP_END | SP_SUBPAT)) != 0
17900 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017901 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017902 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017903 goto theend;
17904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017906 /* Using 'r' implies 'W', otherwise it doesn't work. */
17907 if (flags & SP_REPEAT)
17908 p_ws = FALSE;
17909
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017910 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017911 if (argvars[3].v_type == VAR_UNKNOWN
17912 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 skip = (char_u *)"";
17914 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017917 if (argvars[5].v_type != VAR_UNKNOWN)
17918 {
17919 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17920 if (lnum_stop < 0)
17921 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017922#ifdef FEAT_RELTIME
17923 if (argvars[6].v_type != VAR_UNKNOWN)
17924 {
17925 time_limit = get_tv_number_chk(&argvars[6], NULL);
17926 if (time_limit < 0)
17927 goto theend;
17928 }
17929#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017930 }
17931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017932 if (skip == NULL)
17933 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017935 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017936 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017937
17938theend:
17939 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017940
17941 return retval;
17942}
17943
17944/*
17945 * "searchpair()" function
17946 */
17947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017948f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017949{
17950 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17951}
17952
17953/*
17954 * "searchpairpos()" function
17955 */
17956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017957f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017959 pos_T match_pos;
17960 int lnum = 0;
17961 int col = 0;
17962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017964 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965
17966 if (searchpair_cmn(argvars, &match_pos) > 0)
17967 {
17968 lnum = match_pos.lnum;
17969 col = match_pos.col;
17970 }
17971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017972 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17973 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017974}
17975
17976/*
17977 * Search for a start/middle/end thing.
17978 * Used by searchpair(), see its documentation for the details.
17979 * Returns 0 or -1 for no match,
17980 */
17981 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017982do_searchpair(
17983 char_u *spat, /* start pattern */
17984 char_u *mpat, /* middle pattern */
17985 char_u *epat, /* end pattern */
17986 int dir, /* BACKWARD or FORWARD */
17987 char_u *skip, /* skip expression */
17988 int flags, /* SP_SETPCMARK and other SP_ values */
17989 pos_T *match_pos,
17990 linenr_T lnum_stop, /* stop at this line if not zero */
17991 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017992{
17993 char_u *save_cpo;
17994 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17995 long retval = 0;
17996 pos_T pos;
17997 pos_T firstpos;
17998 pos_T foundpos;
17999 pos_T save_cursor;
18000 pos_T save_pos;
18001 int n;
18002 int r;
18003 int nest = 1;
18004 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018005 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018006 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018007
18008 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18009 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018010 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018011
Bram Moolenaar76929292008-01-06 19:07:36 +000018012#ifdef FEAT_RELTIME
18013 /* Set the time limit, if there is one. */
18014 profile_setlimit(time_limit, &tm);
18015#endif
18016
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018017 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18018 * start/middle/end (pat3, for the top pair). */
18019 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18020 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18021 if (pat2 == NULL || pat3 == NULL)
18022 goto theend;
18023 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18024 if (*mpat == NUL)
18025 STRCPY(pat3, pat2);
18026 else
18027 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18028 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 if (flags & SP_START)
18030 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018031
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 save_cursor = curwin->w_cursor;
18033 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018034 clearpos(&firstpos);
18035 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 pat = pat3;
18037 for (;;)
18038 {
18039 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018040 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18042 /* didn't find it or found the first match again: FAIL */
18043 break;
18044
18045 if (firstpos.lnum == 0)
18046 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018047 if (equalpos(pos, foundpos))
18048 {
18049 /* Found the same position again. Can happen with a pattern that
18050 * has "\zs" at the end and searching backwards. Advance one
18051 * character and try again. */
18052 if (dir == BACKWARD)
18053 decl(&pos);
18054 else
18055 incl(&pos);
18056 }
18057 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018059 /* clear the start flag to avoid getting stuck here */
18060 options &= ~SEARCH_START;
18061
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 /* If the skip pattern matches, ignore this match. */
18063 if (*skip != NUL)
18064 {
18065 save_pos = curwin->w_cursor;
18066 curwin->w_cursor = pos;
18067 r = eval_to_bool(skip, &err, NULL, FALSE);
18068 curwin->w_cursor = save_pos;
18069 if (err)
18070 {
18071 /* Evaluating {skip} caused an error, break here. */
18072 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018073 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 break;
18075 }
18076 if (r)
18077 continue;
18078 }
18079
18080 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18081 {
18082 /* Found end when searching backwards or start when searching
18083 * forward: nested pair. */
18084 ++nest;
18085 pat = pat2; /* nested, don't search for middle */
18086 }
18087 else
18088 {
18089 /* Found end when searching forward or start when searching
18090 * backward: end of (nested) pair; or found middle in outer pair. */
18091 if (--nest == 1)
18092 pat = pat3; /* outer level, search for middle */
18093 }
18094
18095 if (nest == 0)
18096 {
18097 /* Found the match: return matchcount or line number. */
18098 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018099 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018101 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018102 if (flags & SP_SETPCMARK)
18103 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 curwin->w_cursor = pos;
18105 if (!(flags & SP_REPEAT))
18106 break;
18107 nest = 1; /* search for next unmatched */
18108 }
18109 }
18110
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018111 if (match_pos != NULL)
18112 {
18113 /* Store the match cursor position */
18114 match_pos->lnum = curwin->w_cursor.lnum;
18115 match_pos->col = curwin->w_cursor.col + 1;
18116 }
18117
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018119 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 curwin->w_cursor = save_cursor;
18121
18122theend:
18123 vim_free(pat2);
18124 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018125 if (p_cpo == empty_option)
18126 p_cpo = save_cpo;
18127 else
18128 /* Darn, evaluating the {skip} expression changed the value. */
18129 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018130
18131 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018134/*
18135 * "searchpos()" function
18136 */
18137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018138f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018139{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018140 pos_T match_pos;
18141 int lnum = 0;
18142 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018143 int n;
18144 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018145
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018146 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018147 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018148
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018149 n = search_cmn(argvars, &match_pos, &flags);
18150 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018151 {
18152 lnum = match_pos.lnum;
18153 col = match_pos.col;
18154 }
18155
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018156 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18157 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018158 if (flags & SP_SUBPAT)
18159 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018160}
18161
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018163f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165#ifdef FEAT_CLIENTSERVER
18166 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 char_u *server = get_tv_string_chk(&argvars[0]);
18168 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 if (server == NULL || reply == NULL)
18172 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 if (check_restricted() || check_secure())
18174 return;
18175# ifdef FEAT_X11
18176 if (check_connection() == FAIL)
18177 return;
18178# endif
18179
18180 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182 EMSG(_("E258: Unable to send to client"));
18183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018185 rettv->vval.v_number = 0;
18186#else
18187 rettv->vval.v_number = -1;
18188#endif
18189}
18190
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018192f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018193{
18194 char_u *r = NULL;
18195
18196#ifdef FEAT_CLIENTSERVER
18197# ifdef WIN32
18198 r = serverGetVimNames();
18199# else
18200 make_connection();
18201 if (X_DISPLAY != NULL)
18202 r = serverGetVimNames(X_DISPLAY);
18203# endif
18204#endif
18205 rettv->v_type = VAR_STRING;
18206 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207}
18208
18209/*
18210 * "setbufvar()" function
18211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018213f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214{
18215 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018218 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 char_u nbuf[NUMBUFLEN];
18220
18221 if (check_restricted() || check_secure())
18222 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18224 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 varp = &argvars[2];
18227
18228 if (buf != NULL && varname != NULL && varp != NULL)
18229 {
18230 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
18233 if (*varname == '&')
18234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018235 long numval;
18236 char_u *strval;
18237 int error = FALSE;
18238
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 numval = get_tv_number_chk(varp, &error);
18241 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018242 if (!error && strval != NULL)
18243 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 }
18245 else
18246 {
18247 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18248 if (bufvarname != NULL)
18249 {
18250 STRCPY(bufvarname, "b:");
18251 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018252 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 vim_free(bufvarname);
18254 }
18255 }
18256
18257 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260}
18261
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018263f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018264{
18265 dict_T *d;
18266 dictitem_T *di;
18267 char_u *csearch;
18268
18269 if (argvars[0].v_type != VAR_DICT)
18270 {
18271 EMSG(_(e_dictreq));
18272 return;
18273 }
18274
18275 if ((d = argvars[0].vval.v_dict) != NULL)
18276 {
18277 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18278 if (csearch != NULL)
18279 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018280#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018281 if (enc_utf8)
18282 {
18283 int pcc[MAX_MCO];
18284 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018285
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018286 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18287 }
18288 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018289#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018290 set_last_csearch(PTR2CHAR(csearch),
18291 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018292 }
18293
18294 di = dict_find(d, (char_u *)"forward", -1);
18295 if (di != NULL)
18296 set_csearch_direction(get_tv_number(&di->di_tv)
18297 ? FORWARD : BACKWARD);
18298
18299 di = dict_find(d, (char_u *)"until", -1);
18300 if (di != NULL)
18301 set_csearch_until(!!get_tv_number(&di->di_tv));
18302 }
18303}
18304
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305/*
18306 * "setcmdpos()" function
18307 */
18308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018309f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 int pos = (int)get_tv_number(&argvars[0]) - 1;
18312
18313 if (pos >= 0)
18314 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
18317/*
18318 * "setline()" function
18319 */
18320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018321f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
18323 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018324 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018325 list_T *l = NULL;
18326 listitem_T *li = NULL;
18327 long added = 0;
18328 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018330 lnum = get_tv_lnum(&argvars[0]);
18331 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018333 l = argvars[1].vval.v_list;
18334 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018336 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018337 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018338
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018339 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018340 for (;;)
18341 {
18342 if (l != NULL)
18343 {
18344 /* list argument, get next string */
18345 if (li == NULL)
18346 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018347 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018348 li = li->li_next;
18349 }
18350
18351 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018352 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018353 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018354
18355 /* When coming here from Insert mode, sync undo, so that this can be
18356 * undone separately from what was previously inserted. */
18357 if (u_sync_once == 2)
18358 {
18359 u_sync_once = 1; /* notify that u_sync() was called */
18360 u_sync(TRUE);
18361 }
18362
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018363 if (lnum <= curbuf->b_ml.ml_line_count)
18364 {
18365 /* existing line, replace it */
18366 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18367 {
18368 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018369 if (lnum == curwin->w_cursor.lnum)
18370 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018371 rettv->vval.v_number = 0; /* OK */
18372 }
18373 }
18374 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18375 {
18376 /* lnum is one past the last line, append the line */
18377 ++added;
18378 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18379 rettv->vval.v_number = 0; /* OK */
18380 }
18381
18382 if (l == NULL) /* only one string argument */
18383 break;
18384 ++lnum;
18385 }
18386
18387 if (added > 0)
18388 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389}
18390
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018391static 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 +000018392
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018394 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018395 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018397set_qf_ll_list(
18398 win_T *wp UNUSED,
18399 typval_T *list_arg UNUSED,
18400 typval_T *action_arg UNUSED,
18401 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018402{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018403#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018404 char_u *act;
18405 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018406#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018407
Bram Moolenaar2641f772005-03-25 21:58:17 +000018408 rettv->vval.v_number = -1;
18409
18410#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018411 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018412 EMSG(_(e_listreq));
18413 else
18414 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018415 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018416
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018417 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018418 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018419 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018420 if (act == NULL)
18421 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018422 if (*act == 'a' || *act == 'r')
18423 action = *act;
18424 }
18425
Bram Moolenaar81484f42012-12-05 15:16:47 +010018426 if (l != NULL && set_errorlist(wp, l, action,
18427 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018428 rettv->vval.v_number = 0;
18429 }
18430#endif
18431}
18432
18433/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018434 * "setloclist()" function
18435 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018437f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018438{
18439 win_T *win;
18440
18441 rettv->vval.v_number = -1;
18442
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018443 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018444 if (win != NULL)
18445 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18446}
18447
18448/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018449 * "setmatches()" function
18450 */
18451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018452f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018453{
18454#ifdef FEAT_SEARCH_EXTRA
18455 list_T *l;
18456 listitem_T *li;
18457 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018458 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018459
18460 rettv->vval.v_number = -1;
18461 if (argvars[0].v_type != VAR_LIST)
18462 {
18463 EMSG(_(e_listreq));
18464 return;
18465 }
18466 if ((l = argvars[0].vval.v_list) != NULL)
18467 {
18468
18469 /* To some extent make sure that we are dealing with a list from
18470 * "getmatches()". */
18471 li = l->lv_first;
18472 while (li != NULL)
18473 {
18474 if (li->li_tv.v_type != VAR_DICT
18475 || (d = li->li_tv.vval.v_dict) == NULL)
18476 {
18477 EMSG(_(e_invarg));
18478 return;
18479 }
18480 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018481 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18482 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018483 && dict_find(d, (char_u *)"priority", -1) != NULL
18484 && dict_find(d, (char_u *)"id", -1) != NULL))
18485 {
18486 EMSG(_(e_invarg));
18487 return;
18488 }
18489 li = li->li_next;
18490 }
18491
18492 clear_matches(curwin);
18493 li = l->lv_first;
18494 while (li != NULL)
18495 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018496 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018497 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018498 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018499 char_u *group;
18500 int priority;
18501 int id;
18502 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018503
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018504 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018505 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18506 {
18507 if (s == NULL)
18508 {
18509 s = list_alloc();
18510 if (s == NULL)
18511 return;
18512 }
18513
18514 /* match from matchaddpos() */
18515 for (i = 1; i < 9; i++)
18516 {
18517 sprintf((char *)buf, (char *)"pos%d", i);
18518 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18519 {
18520 if (di->di_tv.v_type != VAR_LIST)
18521 return;
18522
18523 list_append_tv(s, &di->di_tv);
18524 s->lv_refcount++;
18525 }
18526 else
18527 break;
18528 }
18529 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018530
18531 group = get_dict_string(d, (char_u *)"group", FALSE);
18532 priority = (int)get_dict_number(d, (char_u *)"priority");
18533 id = (int)get_dict_number(d, (char_u *)"id");
18534 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18535 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18536 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018537 if (i == 0)
18538 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018539 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018540 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018541 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018542 }
18543 else
18544 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018545 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018546 list_unref(s);
18547 s = NULL;
18548 }
18549
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018550 li = li->li_next;
18551 }
18552 rettv->vval.v_number = 0;
18553 }
18554#endif
18555}
18556
18557/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018558 * "setpos()" function
18559 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018561f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018562{
18563 pos_T pos;
18564 int fnum;
18565 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018566 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018567
Bram Moolenaar08250432008-02-13 11:42:46 +000018568 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018569 name = get_tv_string_chk(argvars);
18570 if (name != NULL)
18571 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018572 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018573 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018574 if (--pos.col < 0)
18575 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018576 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018577 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018578 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018579 if (fnum == curbuf->b_fnum)
18580 {
18581 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018582 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018583 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018584 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018585 curwin->w_set_curswant = FALSE;
18586 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018587 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018588 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018589 }
18590 else
18591 EMSG(_(e_invarg));
18592 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018593 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18594 {
18595 /* set mark */
18596 if (setmark_pos(name[1], &pos, fnum) == OK)
18597 rettv->vval.v_number = 0;
18598 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018599 else
18600 EMSG(_(e_invarg));
18601 }
18602 }
18603}
18604
18605/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018606 * "setqflist()" function
18607 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018610{
18611 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18612}
18613
18614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 * "setreg()" function
18616 */
18617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018618f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
18620 int regname;
18621 char_u *strregname;
18622 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 int append;
18625 char_u yank_type;
18626 long block_len;
18627
18628 block_len = -1;
18629 yank_type = MAUTO;
18630 append = FALSE;
18631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018632 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018635 if (strregname == NULL)
18636 return; /* type error; errmsg already given */
18637 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 if (regname == 0 || regname == '@')
18639 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018641 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018643 stropt = get_tv_string_chk(&argvars[2]);
18644 if (stropt == NULL)
18645 return; /* type error */
18646 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 switch (*stropt)
18648 {
18649 case 'a': case 'A': /* append */
18650 append = TRUE;
18651 break;
18652 case 'v': case 'c': /* character-wise selection */
18653 yank_type = MCHAR;
18654 break;
18655 case 'V': case 'l': /* line-wise selection */
18656 yank_type = MLINE;
18657 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 case 'b': case Ctrl_V: /* block-wise selection */
18659 yank_type = MBLOCK;
18660 if (VIM_ISDIGIT(stropt[1]))
18661 {
18662 ++stropt;
18663 block_len = getdigits(&stropt) - 1;
18664 --stropt;
18665 }
18666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 }
18668 }
18669
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018670 if (argvars[1].v_type == VAR_LIST)
18671 {
18672 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018673 char_u **allocval;
18674 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018675 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018676 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018677 int len = argvars[1].vval.v_list->lv_len;
18678 listitem_T *li;
18679
Bram Moolenaar7d647822014-04-05 21:28:56 +020018680 /* First half: use for pointers to result lines; second half: use for
18681 * pointers to allocated copies. */
18682 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018683 if (lstval == NULL)
18684 return;
18685 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018686 allocval = lstval + len + 2;
18687 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018688
18689 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18690 li = li->li_next)
18691 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018692 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018693 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018694 goto free_lstval;
18695 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018696 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018697 /* Need to make a copy, next get_tv_string_buf_chk() will
18698 * overwrite the string. */
18699 strval = vim_strsave(buf);
18700 if (strval == NULL)
18701 goto free_lstval;
18702 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018703 }
18704 *curval++ = strval;
18705 }
18706 *curval++ = NULL;
18707
18708 write_reg_contents_lst(regname, lstval, -1,
18709 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018710free_lstval:
18711 while (curallocval > allocval)
18712 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018713 vim_free(lstval);
18714 }
18715 else
18716 {
18717 strval = get_tv_string_chk(&argvars[1]);
18718 if (strval == NULL)
18719 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018720 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724}
18725
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018726/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018727 * "settabvar()" function
18728 */
18729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018730f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018731{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018732#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018733 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018734 tabpage_T *tp;
18735#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018736 char_u *varname, *tabvarname;
18737 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018738
18739 rettv->vval.v_number = 0;
18740
18741 if (check_restricted() || check_secure())
18742 return;
18743
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018744#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018745 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018746#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018747 varname = get_tv_string_chk(&argvars[1]);
18748 varp = &argvars[2];
18749
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018750 if (varname != NULL && varp != NULL
18751#ifdef FEAT_WINDOWS
18752 && tp != NULL
18753#endif
18754 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018755 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018756#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018757 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018758 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018759#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018760
18761 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18762 if (tabvarname != NULL)
18763 {
18764 STRCPY(tabvarname, "t:");
18765 STRCPY(tabvarname + 2, varname);
18766 set_var(tabvarname, varp, TRUE);
18767 vim_free(tabvarname);
18768 }
18769
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018770#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018771 /* Restore current tabpage */
18772 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018773 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018774#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018775 }
18776}
18777
18778/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018779 * "settabwinvar()" function
18780 */
18781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018782f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018783{
18784 setwinvar(argvars, rettv, 1);
18785}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786
18787/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018788 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018791f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018793 setwinvar(argvars, rettv, 0);
18794}
18795
18796/*
18797 * "setwinvar()" and "settabwinvar()" functions
18798 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018799
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018801setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018802{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 win_T *win;
18804#ifdef FEAT_WINDOWS
18805 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018806 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018807 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808#endif
18809 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018812 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813
18814 if (check_restricted() || check_secure())
18815 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018816
18817#ifdef FEAT_WINDOWS
18818 if (off == 1)
18819 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18820 else
18821 tp = curtab;
18822#endif
18823 win = find_win_by_nr(&argvars[off], tp);
18824 varname = get_tv_string_chk(&argvars[off + 1]);
18825 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
18827 if (win != NULL && varname != NULL && varp != NULL)
18828 {
18829#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018830 need_switch_win = !(tp == curtab && win == curwin);
18831 if (!need_switch_win
18832 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018835 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018837 long numval;
18838 char_u *strval;
18839 int error = FALSE;
18840
18841 ++varname;
18842 numval = get_tv_number_chk(varp, &error);
18843 strval = get_tv_string_buf_chk(varp, nbuf);
18844 if (!error && strval != NULL)
18845 set_option_value(varname, numval, strval, OPT_LOCAL);
18846 }
18847 else
18848 {
18849 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18850 if (winvarname != NULL)
18851 {
18852 STRCPY(winvarname, "w:");
18853 STRCPY(winvarname + 2, varname);
18854 set_var(winvarname, varp, TRUE);
18855 vim_free(winvarname);
18856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 }
18858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018860 if (need_switch_win)
18861 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862#endif
18863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864}
18865
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018866#ifdef FEAT_CRYPT
18867/*
18868 * "sha256({string})" function
18869 */
18870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018871f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018872{
18873 char_u *p;
18874
18875 p = get_tv_string(&argvars[0]);
18876 rettv->vval.v_string = vim_strsave(
18877 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18878 rettv->v_type = VAR_STRING;
18879}
18880#endif /* FEAT_CRYPT */
18881
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018883 * "shellescape({string})" function
18884 */
18885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018887{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018888 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018889 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018890 rettv->v_type = VAR_STRING;
18891}
18892
18893/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018894 * shiftwidth() function
18895 */
18896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018897f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018898{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018899 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018900}
18901
18902/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 */
18905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018906f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018908 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910 p = get_tv_string(&argvars[0]);
18911 rettv->vval.v_string = vim_strsave(p);
18912 simplify_filename(rettv->vval.v_string); /* simplify in place */
18913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914}
18915
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018916#ifdef FEAT_FLOAT
18917/*
18918 * "sin()" function
18919 */
18920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018921f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018922{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018923 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018924
18925 rettv->v_type = VAR_FLOAT;
18926 if (get_float_arg(argvars, &f) == OK)
18927 rettv->vval.v_float = sin(f);
18928 else
18929 rettv->vval.v_float = 0.0;
18930}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018931
18932/*
18933 * "sinh()" function
18934 */
18935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018936f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018937{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018938 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018939
18940 rettv->v_type = VAR_FLOAT;
18941 if (get_float_arg(argvars, &f) == OK)
18942 rettv->vval.v_float = sinh(f);
18943 else
18944 rettv->vval.v_float = 0.0;
18945}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018946#endif
18947
Bram Moolenaar0d660222005-01-07 21:51:51 +000018948static int
18949#ifdef __BORLANDC__
18950 _RTLENTRYF
18951#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018952 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953static int
18954#ifdef __BORLANDC__
18955 _RTLENTRYF
18956#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018957 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018959/* struct used in the array that's given to qsort() */
18960typedef struct
18961{
18962 listitem_T *item;
18963 int idx;
18964} sortItem_T;
18965
Bram Moolenaar0b962472016-02-22 22:51:33 +010018966/* struct storing information about current sort */
18967typedef struct
18968{
18969 int item_compare_ic;
18970 int item_compare_numeric;
18971 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018972#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018973 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018974#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018975 char_u *item_compare_func;
18976 dict_T *item_compare_selfdict;
18977 int item_compare_func_err;
18978 int item_compare_keep_zero;
18979} sortinfo_T;
18980static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018981static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982#define ITEM_COMPARE_FAIL 999
18983
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018985 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 static int
18988#ifdef __BORLANDC__
18989_RTLENTRYF
18990#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018991item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018993 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018994 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018996 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997 int res;
18998 char_u numbuf1[NUMBUFLEN];
18999 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019001 si1 = (sortItem_T *)s1;
19002 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019003 tv1 = &si1->item->li_tv;
19004 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019005
Bram Moolenaar0b962472016-02-22 22:51:33 +010019006 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019007 {
19008 long v1 = get_tv_number(tv1);
19009 long v2 = get_tv_number(tv2);
19010
19011 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19012 }
19013
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019014#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019016 {
19017 float_T v1 = get_tv_float(tv1);
19018 float_T v2 = get_tv_float(tv2);
19019
19020 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19021 }
19022#endif
19023
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019024 /* tv2string() puts quotes around a string and allocates memory. Don't do
19025 * that for string variables. Use a single quote when comparing with a
19026 * non-string to do what the docs promise. */
19027 if (tv1->v_type == VAR_STRING)
19028 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019029 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019030 p1 = (char_u *)"'";
19031 else
19032 p1 = tv1->vval.v_string;
19033 }
19034 else
19035 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19036 if (tv2->v_type == VAR_STRING)
19037 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019038 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019039 p2 = (char_u *)"'";
19040 else
19041 p2 = tv2->vval.v_string;
19042 }
19043 else
19044 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019045 if (p1 == NULL)
19046 p1 = (char_u *)"";
19047 if (p2 == NULL)
19048 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019049 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019050 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019052 res = STRICMP(p1, p2);
19053 else
19054 res = STRCMP(p1, p2);
19055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019057 {
19058 double n1, n2;
19059 n1 = strtod((char *)p1, (char **)&p1);
19060 n2 = strtod((char *)p2, (char **)&p2);
19061 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19062 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019063
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019064 /* When the result would be zero, compare the item indexes. Makes the
19065 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019067 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019068
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 vim_free(tofree1);
19070 vim_free(tofree2);
19071 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072}
19073
19074 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075#ifdef __BORLANDC__
19076_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019078item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019079{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019080 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019083 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019084 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019086 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019087 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019088 return 0;
19089
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019090 si1 = (sortItem_T *)s1;
19091 si2 = (sortItem_T *)s2;
19092
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019093 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019094 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019095 copy_tv(&si1->item->li_tv, &argv[0]);
19096 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019097
19098 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019100 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019101 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019103 clear_tv(&argv[0]);
19104 clear_tv(&argv[1]);
19105
19106 if (res == FAIL)
19107 res = ITEM_COMPARE_FAIL;
19108 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19110 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019111 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019113
19114 /* When the result would be zero, compare the pointers themselves. Makes
19115 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019117 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019118
Bram Moolenaar0d660222005-01-07 21:51:51 +000019119 return res;
19120}
19121
19122/*
19123 * "sort({list})" function
19124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019126do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127{
Bram Moolenaar33570922005-01-25 22:26:29 +000019128 list_T *l;
19129 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019130 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 sortinfo_T *old_sortinfo;
19132 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133 long len;
19134 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 /* Pointer to current info struct used in compare function. Save and
19137 * restore the current one for nested calls. */
19138 old_sortinfo = sortinfo;
19139 sortinfo = &info;
19140
Bram Moolenaar0d660222005-01-07 21:51:51 +000019141 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019142 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 else
19144 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019145 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019146 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019147 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19148 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 rettv->vval.v_list = l;
19151 rettv->v_type = VAR_LIST;
19152 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153
Bram Moolenaar0d660222005-01-07 21:51:51 +000019154 len = list_len(l);
19155 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019156 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 info.item_compare_ic = FALSE;
19159 info.item_compare_numeric = FALSE;
19160 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019161#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019163#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164 info.item_compare_func = NULL;
19165 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 if (argvars[1].v_type != VAR_UNKNOWN)
19167 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019168 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019169 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019170 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019171 else
19172 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019173 int error = FALSE;
19174
19175 i = get_tv_number_chk(&argvars[1], &error);
19176 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019177 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 info.item_compare_func = get_tv_string(&argvars[1]);
19182 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019183 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019185 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 info.item_compare_func = NULL;
19187 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019188 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019189 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019190 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 info.item_compare_func = NULL;
19192 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019193 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019194#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019195 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019196 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197 info.item_compare_func = NULL;
19198 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019199 }
19200#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019202 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 info.item_compare_func = NULL;
19204 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019205 }
19206 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019207 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019208
19209 if (argvars[2].v_type != VAR_UNKNOWN)
19210 {
19211 /* optional third argument: {dict} */
19212 if (argvars[2].v_type != VAR_DICT)
19213 {
19214 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019216 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019217 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019218 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019222 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019224 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225
Bram Moolenaar327aa022014-03-25 18:24:23 +010019226 i = 0;
19227 if (sort)
19228 {
19229 /* sort(): ptrs will be the list to sort */
19230 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019231 {
19232 ptrs[i].item = li;
19233 ptrs[i].idx = i;
19234 ++i;
19235 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019236
Bram Moolenaar0b962472016-02-22 22:51:33 +010019237 info.item_compare_func_err = FALSE;
19238 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019239 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019240 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019241 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019242 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019243 EMSG(_("E702: Sort compare function failed"));
19244 else
19245 {
19246 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019247 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 info.item_compare_func == NULL
19249 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019250
Bram Moolenaar0b962472016-02-22 22:51:33 +010019251 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019252 {
19253 /* Clear the List and append the items in sorted order. */
19254 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19255 l->lv_len = 0;
19256 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019257 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019258 }
19259 }
19260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019262 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019263 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019264
19265 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019266 info.item_compare_func_err = FALSE;
19267 info.item_compare_keep_zero = TRUE;
19268 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019269 ? item_compare2 : item_compare;
19270
19271 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19272 li = li->li_next)
19273 {
19274 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19275 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019276 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019277 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019278 {
19279 EMSG(_("E882: Uniq compare function failed"));
19280 break;
19281 }
19282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019283
Bram Moolenaar0b962472016-02-22 22:51:33 +010019284 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019285 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019286 while (--i >= 0)
19287 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019288 li = ptrs[i].item->li_next;
19289 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019290 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019291 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019292 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019293 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019294 list_fix_watch(l, li);
19295 listitem_free(li);
19296 l->lv_len--;
19297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019298 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019299 }
19300
19301 vim_free(ptrs);
19302 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019303theend:
19304 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019305}
19306
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019308 * "sort({list})" function
19309 */
19310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019311f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019312{
19313 do_sort_uniq(argvars, rettv, TRUE);
19314}
19315
19316/*
19317 * "uniq({list})" function
19318 */
19319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019320f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019321{
19322 do_sort_uniq(argvars, rettv, FALSE);
19323}
19324
19325/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019326 * "soundfold({word})" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019330{
19331 char_u *s;
19332
19333 rettv->v_type = VAR_STRING;
19334 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019335#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019336 rettv->vval.v_string = eval_soundfold(s);
19337#else
19338 rettv->vval.v_string = vim_strsave(s);
19339#endif
19340}
19341
19342/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343 * "spellbadword()" function
19344 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019347{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019348 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019349 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019350 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019351
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019352 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019353 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019354
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019355#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019356 if (argvars[0].v_type == VAR_UNKNOWN)
19357 {
19358 /* Find the start and length of the badly spelled word. */
19359 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19360 if (len != 0)
19361 word = ml_get_cursor();
19362 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019363 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019364 {
19365 char_u *str = get_tv_string_chk(&argvars[0]);
19366 int capcol = -1;
19367
19368 if (str != NULL)
19369 {
19370 /* Check the argument for spelling. */
19371 while (*str != NUL)
19372 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019373 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019374 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019375 {
19376 word = str;
19377 break;
19378 }
19379 str += len;
19380 }
19381 }
19382 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019383#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019384
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019385 list_append_string(rettv->vval.v_list, word, len);
19386 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019387 attr == HLF_SPB ? "bad" :
19388 attr == HLF_SPR ? "rare" :
19389 attr == HLF_SPL ? "local" :
19390 attr == HLF_SPC ? "caps" :
19391 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019392}
19393
19394/*
19395 * "spellsuggest()" function
19396 */
19397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019398f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019399{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019400#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019401 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019402 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019403 int maxcount;
19404 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019405 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019406 listitem_T *li;
19407 int need_capital = FALSE;
19408#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019411 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019412
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019413#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019414 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019415 {
19416 str = get_tv_string(&argvars[0]);
19417 if (argvars[1].v_type != VAR_UNKNOWN)
19418 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019419 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019420 if (maxcount <= 0)
19421 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019422 if (argvars[2].v_type != VAR_UNKNOWN)
19423 {
19424 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19425 if (typeerr)
19426 return;
19427 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019428 }
19429 else
19430 maxcount = 25;
19431
Bram Moolenaar4770d092006-01-12 23:22:24 +000019432 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019433
19434 for (i = 0; i < ga.ga_len; ++i)
19435 {
19436 str = ((char_u **)ga.ga_data)[i];
19437
19438 li = listitem_alloc();
19439 if (li == NULL)
19440 vim_free(str);
19441 else
19442 {
19443 li->li_tv.v_type = VAR_STRING;
19444 li->li_tv.v_lock = 0;
19445 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019446 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019447 }
19448 }
19449 ga_clear(&ga);
19450 }
19451#endif
19452}
19453
Bram Moolenaar0d660222005-01-07 21:51:51 +000019454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019456{
19457 char_u *str;
19458 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019459 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019460 regmatch_T regmatch;
19461 char_u patbuf[NUMBUFLEN];
19462 char_u *save_cpo;
19463 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019464 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019465 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019466 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019467
19468 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19469 save_cpo = p_cpo;
19470 p_cpo = (char_u *)"";
19471
19472 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019473 if (argvars[1].v_type != VAR_UNKNOWN)
19474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019475 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19476 if (pat == NULL)
19477 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019478 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019479 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019480 }
19481 if (pat == NULL || *pat == NUL)
19482 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019483
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019484 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 if (typeerr)
19487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
Bram Moolenaar0d660222005-01-07 21:51:51 +000019489 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19490 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019492 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019493 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019495 if (*str == NUL)
19496 match = FALSE; /* empty item at the end */
19497 else
19498 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019499 if (match)
19500 end = regmatch.startp[0];
19501 else
19502 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019503 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19504 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019505 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019506 if (list_append_string(rettv->vval.v_list, str,
19507 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019509 }
19510 if (!match)
19511 break;
19512 /* Advance to just after the match. */
19513 if (regmatch.endp[0] > str)
19514 col = 0;
19515 else
19516 {
19517 /* Don't get stuck at the same match. */
19518#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019519 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019520#else
19521 col = 1;
19522#endif
19523 }
19524 str = regmatch.endp[0];
19525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526
Bram Moolenaar473de612013-06-08 18:19:48 +020019527 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529
Bram Moolenaar0d660222005-01-07 21:51:51 +000019530 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531}
19532
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019533#ifdef FEAT_FLOAT
19534/*
19535 * "sqrt()" function
19536 */
19537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019538f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019539{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019540 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019541
19542 rettv->v_type = VAR_FLOAT;
19543 if (get_float_arg(argvars, &f) == OK)
19544 rettv->vval.v_float = sqrt(f);
19545 else
19546 rettv->vval.v_float = 0.0;
19547}
19548
19549/*
19550 * "str2float()" function
19551 */
19552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019553f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019554{
19555 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19556
19557 if (*p == '+')
19558 p = skipwhite(p + 1);
19559 (void)string2float(p, &rettv->vval.v_float);
19560 rettv->v_type = VAR_FLOAT;
19561}
19562#endif
19563
Bram Moolenaar2c932302006-03-18 21:42:09 +000019564/*
19565 * "str2nr()" function
19566 */
19567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019568f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019569{
19570 int base = 10;
19571 char_u *p;
19572 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019573 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019574
19575 if (argvars[1].v_type != VAR_UNKNOWN)
19576 {
19577 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019578 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019579 {
19580 EMSG(_(e_invarg));
19581 return;
19582 }
19583 }
19584
19585 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019586 if (*p == '+')
19587 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019588 switch (base)
19589 {
19590 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19591 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19592 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19593 default: what = 0;
19594 }
19595 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019596 rettv->vval.v_number = n;
19597}
19598
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599#ifdef HAVE_STRFTIME
19600/*
19601 * "strftime({format}[, {time}])" function
19602 */
19603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019604f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605{
19606 char_u result_buf[256];
19607 struct tm *curtime;
19608 time_t seconds;
19609 char_u *p;
19610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019613 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019614 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 seconds = time(NULL);
19616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019617 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 curtime = localtime(&seconds);
19619 /* MSVC returns NULL for an invalid value of seconds. */
19620 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019621 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 else
19623 {
19624# ifdef FEAT_MBYTE
19625 vimconv_T conv;
19626 char_u *enc;
19627
19628 conv.vc_type = CONV_NONE;
19629 enc = enc_locale();
19630 convert_setup(&conv, p_enc, enc);
19631 if (conv.vc_type != CONV_NONE)
19632 p = string_convert(&conv, p, NULL);
19633# endif
19634 if (p != NULL)
19635 (void)strftime((char *)result_buf, sizeof(result_buf),
19636 (char *)p, curtime);
19637 else
19638 result_buf[0] = NUL;
19639
19640# ifdef FEAT_MBYTE
19641 if (conv.vc_type != CONV_NONE)
19642 vim_free(p);
19643 convert_setup(&conv, enc, p_enc);
19644 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 else
19647# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649
19650# ifdef FEAT_MBYTE
19651 /* Release conversion descriptors */
19652 convert_setup(&conv, NULL, NULL);
19653 vim_free(enc);
19654# endif
19655 }
19656}
19657#endif
19658
19659/*
19660 * "stridx()" function
19661 */
19662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019663f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664{
19665 char_u buf[NUMBUFLEN];
19666 char_u *needle;
19667 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019672 needle = get_tv_string_chk(&argvars[1]);
19673 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019675 if (needle == NULL || haystack == NULL)
19676 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 if (argvars[2].v_type != VAR_UNKNOWN)
19679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019680 int error = FALSE;
19681
19682 start_idx = get_tv_number_chk(&argvars[2], &error);
19683 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019685 if (start_idx >= 0)
19686 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 }
19688
19689 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19690 if (pos != NULL)
19691 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692}
19693
19694/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019695 * "string()" function
19696 */
19697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019698f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699{
19700 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019701 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019703 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019704 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019705 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019706 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708}
19709
19710/*
19711 * "strlen()" function
19712 */
19713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019714f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 rettv->vval.v_number = (varnumber_T)(STRLEN(
19717 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718}
19719
19720/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019721 * "strchars()" function
19722 */
19723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019724f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019725{
19726 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019727 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019728#ifdef FEAT_MBYTE
19729 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019730 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019731#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019732
19733 if (argvars[1].v_type != VAR_UNKNOWN)
19734 skipcc = get_tv_number_chk(&argvars[1], NULL);
19735 if (skipcc < 0 || skipcc > 1)
19736 EMSG(_(e_invarg));
19737 else
19738 {
19739#ifdef FEAT_MBYTE
19740 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19741 while (*s != NUL)
19742 {
19743 func_mb_ptr2char_adv(&s);
19744 ++len;
19745 }
19746 rettv->vval.v_number = len;
19747#else
19748 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19749#endif
19750 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019751}
19752
19753/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019754 * "strdisplaywidth()" function
19755 */
19756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019757f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019758{
19759 char_u *s = get_tv_string(&argvars[0]);
19760 int col = 0;
19761
19762 if (argvars[1].v_type != VAR_UNKNOWN)
19763 col = get_tv_number(&argvars[1]);
19764
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019765 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019766}
19767
19768/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019769 * "strwidth()" function
19770 */
19771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019773{
19774 char_u *s = get_tv_string(&argvars[0]);
19775
19776 rettv->vval.v_number = (varnumber_T)(
19777#ifdef FEAT_MBYTE
19778 mb_string2cells(s, -1)
19779#else
19780 STRLEN(s)
19781#endif
19782 );
19783}
19784
19785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 * "strpart()" function
19787 */
19788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019789f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790{
19791 char_u *p;
19792 int n;
19793 int len;
19794 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 slen = (int)STRLEN(p);
19799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019800 n = get_tv_number_chk(&argvars[1], &error);
19801 if (error)
19802 len = 0;
19803 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019804 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 else
19806 len = slen - n; /* default len: all bytes that are available. */
19807
19808 /*
19809 * Only return the overlap between the specified part and the actual
19810 * string.
19811 */
19812 if (n < 0)
19813 {
19814 len += n;
19815 n = 0;
19816 }
19817 else if (n > slen)
19818 n = slen;
19819 if (len < 0)
19820 len = 0;
19821 else if (n + len > slen)
19822 len = slen - n;
19823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 rettv->v_type = VAR_STRING;
19825 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826}
19827
19828/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019829 * "strridx()" function
19830 */
19831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019832f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019833{
19834 char_u buf[NUMBUFLEN];
19835 char_u *needle;
19836 char_u *haystack;
19837 char_u *rest;
19838 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019839 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019841 needle = get_tv_string_chk(&argvars[1]);
19842 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019843
19844 rettv->vval.v_number = -1;
19845 if (needle == NULL || haystack == NULL)
19846 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019847
19848 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019849 if (argvars[2].v_type != VAR_UNKNOWN)
19850 {
19851 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019852 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019853 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019854 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019855 }
19856 else
19857 end_idx = haystack_len;
19858
Bram Moolenaar0d660222005-01-07 21:51:51 +000019859 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019860 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019861 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019862 lastmatch = haystack + end_idx;
19863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019864 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019865 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019866 for (rest = haystack; *rest != '\0'; ++rest)
19867 {
19868 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019869 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019870 break;
19871 lastmatch = rest;
19872 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019873 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019874
19875 if (lastmatch == NULL)
19876 rettv->vval.v_number = -1;
19877 else
19878 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19879}
19880
19881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 * "strtrans()" function
19883 */
19884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019885f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 rettv->v_type = VAR_STRING;
19888 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889}
19890
19891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019892 * "submatch()" function
19893 */
19894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019896{
Bram Moolenaar41571762014-04-02 19:00:58 +020019897 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019898 int no;
19899 int retList = 0;
19900
19901 no = (int)get_tv_number_chk(&argvars[0], &error);
19902 if (error)
19903 return;
19904 error = FALSE;
19905 if (argvars[1].v_type != VAR_UNKNOWN)
19906 retList = get_tv_number_chk(&argvars[1], &error);
19907 if (error)
19908 return;
19909
19910 if (retList == 0)
19911 {
19912 rettv->v_type = VAR_STRING;
19913 rettv->vval.v_string = reg_submatch(no);
19914 }
19915 else
19916 {
19917 rettv->v_type = VAR_LIST;
19918 rettv->vval.v_list = reg_submatch_list(no);
19919 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019920}
19921
19922/*
19923 * "substitute()" function
19924 */
19925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019926f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019927{
19928 char_u patbuf[NUMBUFLEN];
19929 char_u subbuf[NUMBUFLEN];
19930 char_u flagsbuf[NUMBUFLEN];
19931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019932 char_u *str = get_tv_string_chk(&argvars[0]);
19933 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19934 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19935 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19936
Bram Moolenaar0d660222005-01-07 21:51:51 +000019937 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019938 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19939 rettv->vval.v_string = NULL;
19940 else
19941 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019942}
19943
19944/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019945 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019948f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949{
19950 int id = 0;
19951#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019952 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 long col;
19954 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019955 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019957 lnum = get_tv_lnum(argvars); /* -1 on type error */
19958 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19959 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019962 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019963 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964#endif
19965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967}
19968
19969/*
19970 * "synIDattr(id, what [, mode])" function
19971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019973f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974{
19975 char_u *p = NULL;
19976#ifdef FEAT_SYN_HL
19977 int id;
19978 char_u *what;
19979 char_u *mode;
19980 char_u modebuf[NUMBUFLEN];
19981 int modec;
19982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 id = get_tv_number(&argvars[0]);
19984 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019989 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 modec = 0; /* replace invalid with current */
19991 }
19992 else
19993 {
19994#ifdef FEAT_GUI
19995 if (gui.in_use)
19996 modec = 'g';
19997 else
19998#endif
19999 if (t_colors > 1)
20000 modec = 'c';
20001 else
20002 modec = 't';
20003 }
20004
20005
20006 switch (TOLOWER_ASC(what[0]))
20007 {
20008 case 'b':
20009 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20010 p = highlight_color(id, what, modec);
20011 else /* bold */
20012 p = highlight_has_attr(id, HL_BOLD, modec);
20013 break;
20014
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020015 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 p = highlight_color(id, what, modec);
20017 break;
20018
20019 case 'i':
20020 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20021 p = highlight_has_attr(id, HL_INVERSE, modec);
20022 else /* italic */
20023 p = highlight_has_attr(id, HL_ITALIC, modec);
20024 break;
20025
20026 case 'n': /* name */
20027 p = get_highlight_name(NULL, id - 1);
20028 break;
20029
20030 case 'r': /* reverse */
20031 p = highlight_has_attr(id, HL_INVERSE, modec);
20032 break;
20033
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020034 case 's':
20035 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20036 p = highlight_color(id, what, modec);
20037 else /* standout */
20038 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 break;
20040
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020041 case 'u':
20042 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20043 /* underline */
20044 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20045 else
20046 /* undercurl */
20047 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 break;
20049 }
20050
20051 if (p != NULL)
20052 p = vim_strsave(p);
20053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 rettv->v_type = VAR_STRING;
20055 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056}
20057
20058/*
20059 * "synIDtrans(id)" function
20060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020062f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063{
20064 int id;
20065
20066#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068
20069 if (id > 0)
20070 id = syn_get_final_id(id);
20071 else
20072#endif
20073 id = 0;
20074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076}
20077
20078/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020079 * "synconcealed(lnum, col)" function
20080 */
20081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020082f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020083{
20084#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20085 long lnum;
20086 long col;
20087 int syntax_flags = 0;
20088 int cchar;
20089 int matchid = 0;
20090 char_u str[NUMBUFLEN];
20091#endif
20092
20093 rettv->v_type = VAR_LIST;
20094 rettv->vval.v_list = NULL;
20095
20096#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20097 lnum = get_tv_lnum(argvars); /* -1 on type error */
20098 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20099
20100 vim_memset(str, NUL, sizeof(str));
20101
20102 if (rettv_list_alloc(rettv) != FAIL)
20103 {
20104 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20105 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20106 && curwin->w_p_cole > 0)
20107 {
20108 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20109 syntax_flags = get_syntax_info(&matchid);
20110
20111 /* get the conceal character */
20112 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20113 {
20114 cchar = syn_get_sub_char();
20115 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20116 cchar = lcs_conceal;
20117 if (cchar != NUL)
20118 {
20119# ifdef FEAT_MBYTE
20120 if (has_mbyte)
20121 (*mb_char2bytes)(cchar, str);
20122 else
20123# endif
20124 str[0] = cchar;
20125 }
20126 }
20127 }
20128
20129 list_append_number(rettv->vval.v_list,
20130 (syntax_flags & HL_CONCEAL) != 0);
20131 /* -1 to auto-determine strlen */
20132 list_append_string(rettv->vval.v_list, str, -1);
20133 list_append_number(rettv->vval.v_list, matchid);
20134 }
20135#endif
20136}
20137
20138/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020139 * "synstack(lnum, col)" function
20140 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020142f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020143{
20144#ifdef FEAT_SYN_HL
20145 long lnum;
20146 long col;
20147 int i;
20148 int id;
20149#endif
20150
20151 rettv->v_type = VAR_LIST;
20152 rettv->vval.v_list = NULL;
20153
20154#ifdef FEAT_SYN_HL
20155 lnum = get_tv_lnum(argvars); /* -1 on type error */
20156 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20157
20158 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020159 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020160 && rettv_list_alloc(rettv) != FAIL)
20161 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020162 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020163 for (i = 0; ; ++i)
20164 {
20165 id = syn_get_stack_item(i);
20166 if (id < 0)
20167 break;
20168 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20169 break;
20170 }
20171 }
20172#endif
20173}
20174
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020176get_cmd_output_as_rettv(
20177 typval_T *argvars,
20178 typval_T *rettv,
20179 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020181 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020183 char_u *infile = NULL;
20184 char_u buf[NUMBUFLEN];
20185 int err = FALSE;
20186 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020187 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020188 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020190 rettv->v_type = VAR_STRING;
20191 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020192 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020193 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020194
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020195 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020196 {
20197 /*
20198 * Write the string to a temp file, to be used for input of the shell
20199 * command.
20200 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020201 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020202 {
20203 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020204 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020205 }
20206
20207 fd = mch_fopen((char *)infile, WRITEBIN);
20208 if (fd == NULL)
20209 {
20210 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020211 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020212 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020213 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020214 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020215 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20216 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020217 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020218 else
20219 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020220 size_t len;
20221
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020222 p = get_tv_string_buf_chk(&argvars[1], buf);
20223 if (p == NULL)
20224 {
20225 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020226 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020227 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020228 len = STRLEN(p);
20229 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020230 err = TRUE;
20231 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020232 if (fclose(fd) != 0)
20233 err = TRUE;
20234 if (err)
20235 {
20236 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020237 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020238 }
20239 }
20240
Bram Moolenaar52a72462014-08-29 15:53:52 +020020241 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20242 * echoes typeahead, that messes up the display. */
20243 if (!msg_silent)
20244 flags += SHELL_COOKED;
20245
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020246 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020248 int len;
20249 listitem_T *li;
20250 char_u *s = NULL;
20251 char_u *start;
20252 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020253 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254
Bram Moolenaar52a72462014-08-29 15:53:52 +020020255 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020256 if (res == NULL)
20257 goto errret;
20258
20259 list = list_alloc();
20260 if (list == NULL)
20261 goto errret;
20262
20263 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020266 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020267 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020268 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020269
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020270 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020271 if (s == NULL)
20272 goto errret;
20273
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020274 for (p = s; start < end; ++p, ++start)
20275 *p = *start == NUL ? NL : *start;
20276 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020277
20278 li = listitem_alloc();
20279 if (li == NULL)
20280 {
20281 vim_free(s);
20282 goto errret;
20283 }
20284 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020285 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020286 li->li_tv.vval.v_string = s;
20287 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020289
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020290 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020291 rettv->v_type = VAR_LIST;
20292 rettv->vval.v_list = list;
20293 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020295 else
20296 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020297 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020298#ifdef USE_CR
20299 /* translate <CR> into <NL> */
20300 if (res != NULL)
20301 {
20302 char_u *s;
20303
20304 for (s = res; *s; ++s)
20305 {
20306 if (*s == CAR)
20307 *s = NL;
20308 }
20309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310#else
20311# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020312 /* translate <CR><NL> into <NL> */
20313 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020315 char_u *s, *d;
20316
20317 d = res;
20318 for (s = res; *s; ++s)
20319 {
20320 if (s[0] == CAR && s[1] == NL)
20321 ++s;
20322 *d++ = *s;
20323 }
20324 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326# endif
20327#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328 rettv->vval.v_string = res;
20329 res = NULL;
20330 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020331
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020333 if (infile != NULL)
20334 {
20335 mch_remove(infile);
20336 vim_free(infile);
20337 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020338 if (res != NULL)
20339 vim_free(res);
20340 if (list != NULL)
20341 list_free(list, TRUE);
20342}
20343
20344/*
20345 * "system()" function
20346 */
20347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020348f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020349{
20350 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20351}
20352
20353/*
20354 * "systemlist()" function
20355 */
20356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020357f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020358{
20359 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360}
20361
20362/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020363 * "tabpagebuflist()" function
20364 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020366f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020367{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020368#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020369 tabpage_T *tp;
20370 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020371
20372 if (argvars[0].v_type == VAR_UNKNOWN)
20373 wp = firstwin;
20374 else
20375 {
20376 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20377 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020378 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020379 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020380 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020381 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020382 for (; wp != NULL; wp = wp->w_next)
20383 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020384 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020385 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020386 }
20387#endif
20388}
20389
20390
20391/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020392 * "tabpagenr()" function
20393 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020395f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020396{
20397 int nr = 1;
20398#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020399 char_u *arg;
20400
20401 if (argvars[0].v_type != VAR_UNKNOWN)
20402 {
20403 arg = get_tv_string_chk(&argvars[0]);
20404 nr = 0;
20405 if (arg != NULL)
20406 {
20407 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020408 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020409 else
20410 EMSG2(_(e_invexpr2), arg);
20411 }
20412 }
20413 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020414 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020415#endif
20416 rettv->vval.v_number = nr;
20417}
20418
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020419
20420#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020421static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020422
20423/*
20424 * Common code for tabpagewinnr() and winnr().
20425 */
20426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020427get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020428{
20429 win_T *twin;
20430 int nr = 1;
20431 win_T *wp;
20432 char_u *arg;
20433
20434 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20435 if (argvar->v_type != VAR_UNKNOWN)
20436 {
20437 arg = get_tv_string_chk(argvar);
20438 if (arg == NULL)
20439 nr = 0; /* type error; errmsg already given */
20440 else if (STRCMP(arg, "$") == 0)
20441 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20442 else if (STRCMP(arg, "#") == 0)
20443 {
20444 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20445 if (twin == NULL)
20446 nr = 0;
20447 }
20448 else
20449 {
20450 EMSG2(_(e_invexpr2), arg);
20451 nr = 0;
20452 }
20453 }
20454
20455 if (nr > 0)
20456 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20457 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020458 {
20459 if (wp == NULL)
20460 {
20461 /* didn't find it in this tabpage */
20462 nr = 0;
20463 break;
20464 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020465 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020466 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020467 return nr;
20468}
20469#endif
20470
20471/*
20472 * "tabpagewinnr()" function
20473 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020475f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020476{
20477 int nr = 1;
20478#ifdef FEAT_WINDOWS
20479 tabpage_T *tp;
20480
20481 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20482 if (tp == NULL)
20483 nr = 0;
20484 else
20485 nr = get_winnr(tp, &argvars[1]);
20486#endif
20487 rettv->vval.v_number = nr;
20488}
20489
20490
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020491/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020492 * "tagfiles()" function
20493 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020495f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020496{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020497 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020498 tagname_T tn;
20499 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020502 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020503 fname = alloc(MAXPATHL);
20504 if (fname == NULL)
20505 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020507 for (first = TRUE; ; first = FALSE)
20508 if (get_tagfname(&tn, first, fname) == FAIL
20509 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020510 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020511 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020512 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020513}
20514
20515/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020516 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020517 */
20518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020519f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020520{
20521 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020522
20523 tag_pattern = get_tv_string(&argvars[0]);
20524
20525 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020526 if (*tag_pattern == NUL)
20527 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020528
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020529 if (rettv_list_alloc(rettv) == OK)
20530 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020531}
20532
20533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 * "tempname()" function
20535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538{
20539 static int x = 'A';
20540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020541 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020542 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543
20544 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20545 * names. Skip 'I' and 'O', they are used for shell redirection. */
20546 do
20547 {
20548 if (x == 'Z')
20549 x = '0';
20550 else if (x == '9')
20551 x = 'A';
20552 else
20553 {
20554#ifdef EBCDIC
20555 if (x == 'I')
20556 x = 'J';
20557 else if (x == 'R')
20558 x = 'S';
20559 else
20560#endif
20561 ++x;
20562 }
20563 } while (x == 'I' || x == 'O');
20564}
20565
20566/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020567 * "test(list)" function: Just checking the walls...
20568 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020570f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020571{
20572 /* Used for unit testing. Change the code below to your liking. */
20573#if 0
20574 listitem_T *li;
20575 list_T *l;
20576 char_u *bad, *good;
20577
20578 if (argvars[0].v_type != VAR_LIST)
20579 return;
20580 l = argvars[0].vval.v_list;
20581 if (l == NULL)
20582 return;
20583 li = l->lv_first;
20584 if (li == NULL)
20585 return;
20586 bad = get_tv_string(&li->li_tv);
20587 li = li->li_next;
20588 if (li == NULL)
20589 return;
20590 good = get_tv_string(&li->li_tv);
20591 rettv->vval.v_number = test_edit_score(bad, good);
20592#endif
20593}
20594
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020595#ifdef FEAT_FLOAT
20596/*
20597 * "tan()" function
20598 */
20599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020601{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020602 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020603
20604 rettv->v_type = VAR_FLOAT;
20605 if (get_float_arg(argvars, &f) == OK)
20606 rettv->vval.v_float = tan(f);
20607 else
20608 rettv->vval.v_float = 0.0;
20609}
20610
20611/*
20612 * "tanh()" function
20613 */
20614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020615f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020616{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020617 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020618
20619 rettv->v_type = VAR_FLOAT;
20620 if (get_float_arg(argvars, &f) == OK)
20621 rettv->vval.v_float = tanh(f);
20622 else
20623 rettv->vval.v_float = 0.0;
20624}
20625#endif
20626
Bram Moolenaard52d9742005-08-21 22:20:28 +000020627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 * "tolower(string)" function
20629 */
20630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020631f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632{
20633 char_u *p;
20634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020635 p = vim_strsave(get_tv_string(&argvars[0]));
20636 rettv->v_type = VAR_STRING;
20637 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638
20639 if (p != NULL)
20640 while (*p != NUL)
20641 {
20642#ifdef FEAT_MBYTE
20643 int l;
20644
20645 if (enc_utf8)
20646 {
20647 int c, lc;
20648
20649 c = utf_ptr2char(p);
20650 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020651 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 /* TODO: reallocate string when byte count changes. */
20653 if (utf_char2len(lc) == l)
20654 utf_char2bytes(lc, p);
20655 p += l;
20656 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020657 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 p += l; /* skip multi-byte character */
20659 else
20660#endif
20661 {
20662 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20663 ++p;
20664 }
20665 }
20666}
20667
20668/*
20669 * "toupper(string)" function
20670 */
20671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020672f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020675 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676}
20677
20678/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020679 * "tr(string, fromstr, tostr)" function
20680 */
20681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020682f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020683{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020684 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020685 char_u *fromstr;
20686 char_u *tostr;
20687 char_u *p;
20688#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020689 int inlen;
20690 int fromlen;
20691 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020692 int idx;
20693 char_u *cpstr;
20694 int cplen;
20695 int first = TRUE;
20696#endif
20697 char_u buf[NUMBUFLEN];
20698 char_u buf2[NUMBUFLEN];
20699 garray_T ga;
20700
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020701 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020702 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20703 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020704
20705 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706 rettv->v_type = VAR_STRING;
20707 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020708 if (fromstr == NULL || tostr == NULL)
20709 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020710 ga_init2(&ga, (int)sizeof(char), 80);
20711
20712#ifdef FEAT_MBYTE
20713 if (!has_mbyte)
20714#endif
20715 /* not multi-byte: fromstr and tostr must be the same length */
20716 if (STRLEN(fromstr) != STRLEN(tostr))
20717 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020718#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020719error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020720#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020721 EMSG2(_(e_invarg2), fromstr);
20722 ga_clear(&ga);
20723 return;
20724 }
20725
20726 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020727 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020728 {
20729#ifdef FEAT_MBYTE
20730 if (has_mbyte)
20731 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020732 inlen = (*mb_ptr2len)(in_str);
20733 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020734 cplen = inlen;
20735 idx = 0;
20736 for (p = fromstr; *p != NUL; p += fromlen)
20737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020738 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020739 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020740 {
20741 for (p = tostr; *p != NUL; p += tolen)
20742 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020743 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020744 if (idx-- == 0)
20745 {
20746 cplen = tolen;
20747 cpstr = p;
20748 break;
20749 }
20750 }
20751 if (*p == NUL) /* tostr is shorter than fromstr */
20752 goto error;
20753 break;
20754 }
20755 ++idx;
20756 }
20757
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020758 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020759 {
20760 /* Check that fromstr and tostr have the same number of
20761 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020762 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020763 first = FALSE;
20764 for (p = tostr; *p != NUL; p += tolen)
20765 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020766 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020767 --idx;
20768 }
20769 if (idx != 0)
20770 goto error;
20771 }
20772
Bram Moolenaarcde88542015-08-11 19:14:00 +020020773 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020774 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020775 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020776
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020777 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020778 }
20779 else
20780#endif
20781 {
20782 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020783 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020784 if (p != NULL)
20785 ga_append(&ga, tostr[p - fromstr]);
20786 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020787 ga_append(&ga, *in_str);
20788 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020789 }
20790 }
20791
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020792 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020793 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020794 ga_append(&ga, NUL);
20795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020796 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020797}
20798
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020799#ifdef FEAT_FLOAT
20800/*
20801 * "trunc({float})" function
20802 */
20803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020804f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020805{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020806 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020807
20808 rettv->v_type = VAR_FLOAT;
20809 if (get_float_arg(argvars, &f) == OK)
20810 /* trunc() is not in C90, use floor() or ceil() instead. */
20811 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20812 else
20813 rettv->vval.v_float = 0.0;
20814}
20815#endif
20816
Bram Moolenaar8299df92004-07-10 09:47:34 +000020817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818 * "type(expr)" function
20819 */
20820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020821f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020823 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020824
20825 switch (argvars[0].v_type)
20826 {
20827 case VAR_NUMBER: n = 0; break;
20828 case VAR_STRING: n = 1; break;
20829 case VAR_FUNC: n = 2; break;
20830 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020831 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020832 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020833 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020834 if (argvars[0].vval.v_number == VVAL_FALSE
20835 || argvars[0].vval.v_number == VVAL_TRUE)
20836 n = 6;
20837 else
20838 n = 7;
20839 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020840 case VAR_JOB: n = 8; break;
20841 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020842 case VAR_UNKNOWN:
20843 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20844 n = -1;
20845 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020846 }
20847 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848}
20849
20850/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020851 * "undofile(name)" function
20852 */
20853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020854f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020855{
20856 rettv->v_type = VAR_STRING;
20857#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020858 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020859 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020860
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020861 if (*fname == NUL)
20862 {
20863 /* If there is no file name there will be no undo file. */
20864 rettv->vval.v_string = NULL;
20865 }
20866 else
20867 {
20868 char_u *ffname = FullName_save(fname, FALSE);
20869
20870 if (ffname != NULL)
20871 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20872 vim_free(ffname);
20873 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020874 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020875#else
20876 rettv->vval.v_string = NULL;
20877#endif
20878}
20879
20880/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020881 * "undotree()" function
20882 */
20883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020884f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020885{
20886 if (rettv_dict_alloc(rettv) == OK)
20887 {
20888 dict_T *dict = rettv->vval.v_dict;
20889 list_T *list;
20890
Bram Moolenaar730cde92010-06-27 05:18:54 +020020891 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020892 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020893 dict_add_nr_str(dict, "save_last",
20894 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020895 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20896 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020897 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020898
20899 list = list_alloc();
20900 if (list != NULL)
20901 {
20902 u_eval_tree(curbuf->b_u_oldhead, list);
20903 dict_add_list(dict, "entries", list);
20904 }
20905 }
20906}
20907
20908/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020909 * "values(dict)" function
20910 */
20911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020912f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020913{
20914 dict_list(argvars, rettv, 1);
20915}
20916
20917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 * "virtcol(string)" function
20919 */
20920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020921f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922{
20923 colnr_T vcol = 0;
20924 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020925 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020927 fp = var2fpos(&argvars[0], FALSE, &fnum);
20928 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20929 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 {
20931 getvvcol(curwin, fp, NULL, NULL, &vcol);
20932 ++vcol;
20933 }
20934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020935 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936}
20937
20938/*
20939 * "visualmode()" function
20940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020942f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 char_u str[2];
20945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 str[0] = curbuf->b_visual_mode_eval;
20948 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950
20951 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020952 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954}
20955
20956/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020957 * "wildmenumode()" function
20958 */
20959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020961{
20962#ifdef FEAT_WILDMENU
20963 if (wild_menu_showing)
20964 rettv->vval.v_number = 1;
20965#endif
20966}
20967
20968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 * "winbufnr(nr)" function
20970 */
20971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020972f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973{
20974 win_T *wp;
20975
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020976 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020980 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981}
20982
20983/*
20984 * "wincol()" function
20985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020987f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988{
20989 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020990 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991}
20992
20993/*
20994 * "winheight(nr)" function
20995 */
20996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020997f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
20999 win_T *wp;
21000
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021001 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021005 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006}
21007
21008/*
21009 * "winline()" function
21010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021012f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013{
21014 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021015 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016}
21017
21018/*
21019 * "winnr()" function
21020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021022f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023{
21024 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021025
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021027 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021029 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
21032/*
21033 * "winrestcmd()" function
21034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021036f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037{
21038#ifdef FEAT_WINDOWS
21039 win_T *wp;
21040 int winnr = 1;
21041 garray_T ga;
21042 char_u buf[50];
21043
21044 ga_init2(&ga, (int)sizeof(char), 70);
21045 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21046 {
21047 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21048 ga_concat(&ga, buf);
21049# ifdef FEAT_VERTSPLIT
21050 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21051 ga_concat(&ga, buf);
21052# endif
21053 ++winnr;
21054 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021055 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021057 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021059 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021061 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062}
21063
21064/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021065 * "winrestview()" function
21066 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021068f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021069{
21070 dict_T *dict;
21071
21072 if (argvars[0].v_type != VAR_DICT
21073 || (dict = argvars[0].vval.v_dict) == NULL)
21074 EMSG(_(e_invarg));
21075 else
21076 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021077 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21078 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21079 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21080 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021081#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021082 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21083 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021084#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021085 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21086 {
21087 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21088 curwin->w_set_curswant = FALSE;
21089 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021090
Bram Moolenaar82c25852014-05-28 16:47:16 +020021091 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21092 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021093#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021094 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21095 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021096#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021097 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21098 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21099 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21100 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021101
21102 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021103 win_new_height(curwin, curwin->w_height);
21104# ifdef FEAT_VERTSPLIT
21105 win_new_width(curwin, W_WIDTH(curwin));
21106# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021107 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021108
Bram Moolenaarb851a962014-10-31 15:45:52 +010021109 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021110 curwin->w_topline = 1;
21111 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21112 curwin->w_topline = curbuf->b_ml.ml_line_count;
21113#ifdef FEAT_DIFF
21114 check_topfill(curwin, TRUE);
21115#endif
21116 }
21117}
21118
21119/*
21120 * "winsaveview()" function
21121 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021123f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021124{
21125 dict_T *dict;
21126
Bram Moolenaara800b422010-06-27 01:15:55 +020021127 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021128 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021129 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021130
21131 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21132 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21133#ifdef FEAT_VIRTUALEDIT
21134 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21135#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021136 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021137 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21138
21139 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21140#ifdef FEAT_DIFF
21141 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21142#endif
21143 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21144 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21145}
21146
21147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 * "winwidth(nr)" function
21149 */
21150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
21153 win_T *wp;
21154
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021155 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 else
21159#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021160 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021162 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163#endif
21164}
21165
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021167 * "wordcount()" function
21168 */
21169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021170f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021171{
21172 if (rettv_dict_alloc(rettv) == FAIL)
21173 return;
21174 cursor_pos_info(rettv->vval.v_dict);
21175}
21176
21177/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021178 * Write list of strings to file
21179 */
21180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021182{
21183 listitem_T *li;
21184 int c;
21185 int ret = OK;
21186 char_u *s;
21187
21188 for (li = list->lv_first; li != NULL; li = li->li_next)
21189 {
21190 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21191 {
21192 if (*s == '\n')
21193 c = putc(NUL, fd);
21194 else
21195 c = putc(*s, fd);
21196 if (c == EOF)
21197 {
21198 ret = FAIL;
21199 break;
21200 }
21201 }
21202 if (!binary || li->li_next != NULL)
21203 if (putc('\n', fd) == EOF)
21204 {
21205 ret = FAIL;
21206 break;
21207 }
21208 if (ret == FAIL)
21209 {
21210 EMSG(_(e_write));
21211 break;
21212 }
21213 }
21214 return ret;
21215}
21216
21217/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021218 * "writefile()" function
21219 */
21220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021222{
21223 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021224 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021225 char_u *fname;
21226 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021227 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021228
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021229 if (check_restricted() || check_secure())
21230 return;
21231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021232 if (argvars[0].v_type != VAR_LIST)
21233 {
21234 EMSG2(_(e_listarg), "writefile()");
21235 return;
21236 }
21237 if (argvars[0].vval.v_list == NULL)
21238 return;
21239
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021240 if (argvars[2].v_type != VAR_UNKNOWN)
21241 {
21242 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21243 binary = TRUE;
21244 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21245 append = TRUE;
21246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021247
21248 /* Always open the file in binary mode, library functions have a mind of
21249 * their own about CR-LF conversion. */
21250 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021251 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21252 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021253 {
21254 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21255 ret = -1;
21256 }
21257 else
21258 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021259 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21260 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021261 fclose(fd);
21262 }
21263
21264 rettv->vval.v_number = ret;
21265}
21266
21267/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021268 * "xor(expr, expr)" function
21269 */
21270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021271f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021272{
21273 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21274 ^ get_tv_number_chk(&argvars[1], NULL);
21275}
21276
21277
21278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021280 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 */
21282 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283var2fpos(
21284 typval_T *varp,
21285 int dollar_lnum, /* TRUE when $ is last line */
21286 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021288 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021290 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291
Bram Moolenaara5525202006-03-02 22:52:09 +000021292 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021293 if (varp->v_type == VAR_LIST)
21294 {
21295 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021296 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021297 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021298 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021299
21300 l = varp->vval.v_list;
21301 if (l == NULL)
21302 return NULL;
21303
21304 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021305 pos.lnum = list_find_nr(l, 0L, &error);
21306 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021307 return NULL; /* invalid line number */
21308
21309 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021310 pos.col = list_find_nr(l, 1L, &error);
21311 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021312 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021313 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021314
21315 /* We accept "$" for the column number: last column. */
21316 li = list_find(l, 1L);
21317 if (li != NULL && li->li_tv.v_type == VAR_STRING
21318 && li->li_tv.vval.v_string != NULL
21319 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21320 pos.col = len + 1;
21321
Bram Moolenaara5525202006-03-02 22:52:09 +000021322 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021323 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021324 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021325 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021326
Bram Moolenaara5525202006-03-02 22:52:09 +000021327#ifdef FEAT_VIRTUALEDIT
21328 /* Get the virtual offset. Defaults to zero. */
21329 pos.coladd = list_find_nr(l, 2L, &error);
21330 if (error)
21331 pos.coladd = 0;
21332#endif
21333
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021334 return &pos;
21335 }
21336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021337 name = get_tv_string_chk(varp);
21338 if (name == NULL)
21339 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021340 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021342 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21343 {
21344 if (VIsual_active)
21345 return &VIsual;
21346 return &curwin->w_cursor;
21347 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021348 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021350 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21352 return NULL;
21353 return pp;
21354 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021355
21356#ifdef FEAT_VIRTUALEDIT
21357 pos.coladd = 0;
21358#endif
21359
Bram Moolenaar477933c2007-07-17 14:32:23 +000021360 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021361 {
21362 pos.col = 0;
21363 if (name[1] == '0') /* "w0": first visible line */
21364 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021365 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021366 pos.lnum = curwin->w_topline;
21367 return &pos;
21368 }
21369 else if (name[1] == '$') /* "w$": last visible line */
21370 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021371 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021372 pos.lnum = curwin->w_botline - 1;
21373 return &pos;
21374 }
21375 }
21376 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021378 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
21380 pos.lnum = curbuf->b_ml.ml_line_count;
21381 pos.col = 0;
21382 }
21383 else
21384 {
21385 pos.lnum = curwin->w_cursor.lnum;
21386 pos.col = (colnr_T)STRLEN(ml_get_curline());
21387 }
21388 return &pos;
21389 }
21390 return NULL;
21391}
21392
21393/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021394 * Convert list in "arg" into a position and optional file number.
21395 * When "fnump" is NULL there is no file number, only 3 items.
21396 * Note that the column is passed on as-is, the caller may want to decrement
21397 * it to use 1 for the first column.
21398 * Return FAIL when conversion is not possible, doesn't check the position for
21399 * validity.
21400 */
21401 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021402list2fpos(
21403 typval_T *arg,
21404 pos_T *posp,
21405 int *fnump,
21406 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021407{
21408 list_T *l = arg->vval.v_list;
21409 long i = 0;
21410 long n;
21411
Bram Moolenaar493c1782014-05-28 14:34:46 +020021412 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21413 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021414 if (arg->v_type != VAR_LIST
21415 || l == NULL
21416 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021417 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021418 return FAIL;
21419
21420 if (fnump != NULL)
21421 {
21422 n = list_find_nr(l, i++, NULL); /* fnum */
21423 if (n < 0)
21424 return FAIL;
21425 if (n == 0)
21426 n = curbuf->b_fnum; /* current buffer */
21427 *fnump = n;
21428 }
21429
21430 n = list_find_nr(l, i++, NULL); /* lnum */
21431 if (n < 0)
21432 return FAIL;
21433 posp->lnum = n;
21434
21435 n = list_find_nr(l, i++, NULL); /* col */
21436 if (n < 0)
21437 return FAIL;
21438 posp->col = n;
21439
21440#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021441 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021442 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021443 posp->coladd = 0;
21444 else
21445 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021446#endif
21447
Bram Moolenaar493c1782014-05-28 14:34:46 +020021448 if (curswantp != NULL)
21449 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21450
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021451 return OK;
21452}
21453
21454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 * Get the length of an environment variable name.
21456 * Advance "arg" to the first character after the name.
21457 * Return 0 for error.
21458 */
21459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021460get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461{
21462 char_u *p;
21463 int len;
21464
21465 for (p = *arg; vim_isIDc(*p); ++p)
21466 ;
21467 if (p == *arg) /* no name found */
21468 return 0;
21469
21470 len = (int)(p - *arg);
21471 *arg = p;
21472 return len;
21473}
21474
21475/*
21476 * Get the length of the name of a function or internal variable.
21477 * "arg" is advanced to the first non-white character after the name.
21478 * Return 0 if something is wrong.
21479 */
21480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021481get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482{
21483 char_u *p;
21484 int len;
21485
21486 /* Find the end of the name. */
21487 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021488 {
21489 if (*p == ':')
21490 {
21491 /* "s:" is start of "s:var", but "n:" is not and can be used in
21492 * slice "[n:]". Also "xx:" is not a namespace. */
21493 len = (int)(p - *arg);
21494 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21495 || len > 1)
21496 break;
21497 }
21498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 if (p == *arg) /* no name found */
21500 return 0;
21501
21502 len = (int)(p - *arg);
21503 *arg = skipwhite(p);
21504
21505 return len;
21506}
21507
21508/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021509 * Get the length of the name of a variable or function.
21510 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021512 * Return -1 if curly braces expansion failed.
21513 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 * If the name contains 'magic' {}'s, expand them and return the
21515 * expanded name in an allocated string via 'alias' - caller must free.
21516 */
21517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021518get_name_len(
21519 char_u **arg,
21520 char_u **alias,
21521 int evaluate,
21522 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523{
21524 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 char_u *p;
21526 char_u *expr_start;
21527 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528
21529 *alias = NULL; /* default to no alias */
21530
21531 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21532 && (*arg)[2] == (int)KE_SNR)
21533 {
21534 /* hard coded <SNR>, already translated */
21535 *arg += 3;
21536 return get_id_len(arg) + 3;
21537 }
21538 len = eval_fname_script(*arg);
21539 if (len > 0)
21540 {
21541 /* literal "<SID>", "s:" or "<SNR>" */
21542 *arg += len;
21543 }
21544
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021546 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021548 p = find_name_end(*arg, &expr_start, &expr_end,
21549 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 if (expr_start != NULL)
21551 {
21552 char_u *temp_string;
21553
21554 if (!evaluate)
21555 {
21556 len += (int)(p - *arg);
21557 *arg = skipwhite(p);
21558 return len;
21559 }
21560
21561 /*
21562 * Include any <SID> etc in the expanded string:
21563 * Thus the -len here.
21564 */
21565 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21566 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021567 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 *alias = temp_string;
21569 *arg = skipwhite(p);
21570 return (int)STRLEN(temp_string);
21571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572
21573 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021574 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 EMSG2(_(e_invexpr2), *arg);
21576
21577 return len;
21578}
21579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021580/*
21581 * Find the end of a variable or function name, taking care of magic braces.
21582 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21583 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021584 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021585 * Return a pointer to just after the name. Equal to "arg" if there is no
21586 * valid name.
21587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021589find_name_end(
21590 char_u *arg,
21591 char_u **expr_start,
21592 char_u **expr_end,
21593 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021595 int mb_nest = 0;
21596 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021598 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021600 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021602 *expr_start = NULL;
21603 *expr_end = NULL;
21604 }
21605
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021606 /* Quick check for valid starting character. */
21607 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21608 return arg;
21609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610 for (p = arg; *p != NUL
21611 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021612 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021613 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021614 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021615 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021616 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021617 if (*p == '\'')
21618 {
21619 /* skip over 'string' to avoid counting [ and ] inside it. */
21620 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21621 ;
21622 if (*p == NUL)
21623 break;
21624 }
21625 else if (*p == '"')
21626 {
21627 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21628 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21629 if (*p == '\\' && p[1] != NUL)
21630 ++p;
21631 if (*p == NUL)
21632 break;
21633 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021634 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21635 {
21636 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021637 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021638 len = (int)(p - arg);
21639 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021640 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021641 break;
21642 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021644 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021646 if (*p == '[')
21647 ++br_nest;
21648 else if (*p == ']')
21649 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021654 if (*p == '{')
21655 {
21656 mb_nest++;
21657 if (expr_start != NULL && *expr_start == NULL)
21658 *expr_start = p;
21659 }
21660 else if (*p == '}')
21661 {
21662 mb_nest--;
21663 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21664 *expr_end = p;
21665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
21668
21669 return p;
21670}
21671
21672/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021673 * Expands out the 'magic' {}'s in a variable/function name.
21674 * Note that this can call itself recursively, to deal with
21675 * constructs like foo{bar}{baz}{bam}
21676 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21677 * "in_start" ^
21678 * "expr_start" ^
21679 * "expr_end" ^
21680 * "in_end" ^
21681 *
21682 * Returns a new allocated string, which the caller must free.
21683 * Returns NULL for failure.
21684 */
21685 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021686make_expanded_name(
21687 char_u *in_start,
21688 char_u *expr_start,
21689 char_u *expr_end,
21690 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021691{
21692 char_u c1;
21693 char_u *retval = NULL;
21694 char_u *temp_result;
21695 char_u *nextcmd = NULL;
21696
21697 if (expr_end == NULL || in_end == NULL)
21698 return NULL;
21699 *expr_start = NUL;
21700 *expr_end = NUL;
21701 c1 = *in_end;
21702 *in_end = NUL;
21703
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021704 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021705 if (temp_result != NULL && nextcmd == NULL)
21706 {
21707 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21708 + (in_end - expr_end) + 1));
21709 if (retval != NULL)
21710 {
21711 STRCPY(retval, in_start);
21712 STRCAT(retval, temp_result);
21713 STRCAT(retval, expr_end + 1);
21714 }
21715 }
21716 vim_free(temp_result);
21717
21718 *in_end = c1; /* put char back for error messages */
21719 *expr_start = '{';
21720 *expr_end = '}';
21721
21722 if (retval != NULL)
21723 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021724 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021725 if (expr_start != NULL)
21726 {
21727 /* Further expansion! */
21728 temp_result = make_expanded_name(retval, expr_start,
21729 expr_end, temp_result);
21730 vim_free(retval);
21731 retval = temp_result;
21732 }
21733 }
21734
21735 return retval;
21736}
21737
21738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021740 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 */
21742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021743eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021745 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21746}
21747
21748/*
21749 * Return TRUE if character "c" can be used as the first character in a
21750 * variable or function name (excluding '{' and '}').
21751 */
21752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021753eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021754{
21755 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756}
21757
21758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 * Set number v: variable to "val".
21760 */
21761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021762set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021764 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765}
21766
21767/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021768 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 */
21770 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021771get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021773 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774}
21775
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021776/*
21777 * Get string v: variable value. Uses a static buffer, can only be used once.
21778 */
21779 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021780get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021781{
21782 return get_tv_string(&vimvars[idx].vv_tv);
21783}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021784
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021786 * Get List v: variable value. Caller must take care of reference count when
21787 * needed.
21788 */
21789 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021790get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021791{
21792 return vimvars[idx].vv_list;
21793}
21794
21795/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021796 * Set v:char to character "c".
21797 */
21798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021799set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021800{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021801 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021802
21803#ifdef FEAT_MBYTE
21804 if (has_mbyte)
21805 buf[(*mb_char2bytes)(c, buf)] = NUL;
21806 else
21807#endif
21808 {
21809 buf[0] = c;
21810 buf[1] = NUL;
21811 }
21812 set_vim_var_string(VV_CHAR, buf, -1);
21813}
21814
21815/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021816 * Set v:count to "count" and v:count1 to "count1".
21817 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 */
21819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021820set_vcount(
21821 long count,
21822 long count1,
21823 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021825 if (set_prevcount)
21826 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021827 vimvars[VV_COUNT].vv_nr = count;
21828 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829}
21830
21831/*
21832 * Set string v: variable to a copy of "val".
21833 */
21834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021835set_vim_var_string(
21836 int idx,
21837 char_u *val,
21838 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839{
Bram Moolenaara542c682016-01-31 16:28:04 +010021840 clear_tv(&vimvars[idx].vv_di.di_tv);
21841 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021843 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021845 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021847 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848}
21849
21850/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021851 * Set List v: variable to "val".
21852 */
21853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021854set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021855{
Bram Moolenaara542c682016-01-31 16:28:04 +010021856 clear_tv(&vimvars[idx].vv_di.di_tv);
21857 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021858 vimvars[idx].vv_list = val;
21859 if (val != NULL)
21860 ++val->lv_refcount;
21861}
21862
21863/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021864 * Set Dictionary v: variable to "val".
21865 */
21866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021867set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021868{
21869 int todo;
21870 hashitem_T *hi;
21871
Bram Moolenaara542c682016-01-31 16:28:04 +010021872 clear_tv(&vimvars[idx].vv_di.di_tv);
21873 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021874 vimvars[idx].vv_dict = val;
21875 if (val != NULL)
21876 {
21877 ++val->dv_refcount;
21878
21879 /* Set readonly */
21880 todo = (int)val->dv_hashtab.ht_used;
21881 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21882 {
21883 if (HASHITEM_EMPTY(hi))
21884 continue;
21885 --todo;
21886 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21887 }
21888 }
21889}
21890
21891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 * Set v:register if needed.
21893 */
21894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896{
21897 char_u regname;
21898
21899 if (c == 0 || c == ' ')
21900 regname = '"';
21901 else
21902 regname = c;
21903 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021904 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 set_vim_var_string(VV_REG, &regname, 1);
21906}
21907
21908/*
21909 * Get or set v:exception. If "oldval" == NULL, return the current value.
21910 * Otherwise, restore the value to "oldval" and return NULL.
21911 * Must always be called in pairs to save and restore v:exception! Does not
21912 * take care of memory allocations.
21913 */
21914 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021915v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916{
21917 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021918 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919
Bram Moolenaare9a41262005-01-15 22:18:47 +000021920 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 return NULL;
21922}
21923
21924/*
21925 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21926 * Otherwise, restore the value to "oldval" and return NULL.
21927 * Must always be called in pairs to save and restore v:throwpoint! Does not
21928 * take care of memory allocations.
21929 */
21930 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021931v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932{
21933 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021934 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 return NULL;
21938}
21939
21940#if defined(FEAT_AUTOCMD) || defined(PROTO)
21941/*
21942 * Set v:cmdarg.
21943 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21944 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21945 * Must always be called in pairs!
21946 */
21947 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021948set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949{
21950 char_u *oldval;
21951 char_u *newval;
21952 unsigned len;
21953
Bram Moolenaare9a41262005-01-15 22:18:47 +000021954 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021955 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021957 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021958 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021959 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 }
21961
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021962 if (eap->force_bin == FORCE_BIN)
21963 len = 6;
21964 else if (eap->force_bin == FORCE_NOBIN)
21965 len = 8;
21966 else
21967 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021968
21969 if (eap->read_edit)
21970 len += 7;
21971
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021972 if (eap->force_ff != 0)
21973 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21974# ifdef FEAT_MBYTE
21975 if (eap->force_enc != 0)
21976 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021977 if (eap->bad_char != 0)
21978 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021979# endif
21980
21981 newval = alloc(len + 1);
21982 if (newval == NULL)
21983 return NULL;
21984
21985 if (eap->force_bin == FORCE_BIN)
21986 sprintf((char *)newval, " ++bin");
21987 else if (eap->force_bin == FORCE_NOBIN)
21988 sprintf((char *)newval, " ++nobin");
21989 else
21990 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021991
21992 if (eap->read_edit)
21993 STRCAT(newval, " ++edit");
21994
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021995 if (eap->force_ff != 0)
21996 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21997 eap->cmd + eap->force_ff);
21998# ifdef FEAT_MBYTE
21999 if (eap->force_enc != 0)
22000 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22001 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022002 if (eap->bad_char == BAD_KEEP)
22003 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22004 else if (eap->bad_char == BAD_DROP)
22005 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22006 else if (eap->bad_char != 0)
22007 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022008# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022009 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022010 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011}
22012#endif
22013
22014/*
22015 * Get the value of internal variable "name".
22016 * Return OK or FAIL.
22017 */
22018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022019get_var_tv(
22020 char_u *name,
22021 int len, /* length of "name" */
22022 typval_T *rettv, /* NULL when only checking existence */
22023 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22024 int verbose, /* may give error message */
22025 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026{
22027 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 typval_T *tv = NULL;
22029 typval_T atv;
22030 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032
22033 /* truncate the name, so that we can use strcmp() */
22034 cc = name[len];
22035 name[len] = NUL;
22036
22037 /*
22038 * Check for "b:changedtick".
22039 */
22040 if (STRCMP(name, "b:changedtick") == 0)
22041 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 atv.v_type = VAR_NUMBER;
22043 atv.vval.v_number = curbuf->b_changedtick;
22044 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 }
22046
22047 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 * Check for user-defined variables.
22049 */
22050 else
22051 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022052 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022054 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022056 if (dip != NULL)
22057 *dip = v;
22058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 }
22060
Bram Moolenaare9a41262005-01-15 22:18:47 +000022061 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022063 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 ret = FAIL;
22066 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022068 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069
22070 name[len] = cc;
22071
22072 return ret;
22073}
22074
22075/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022076 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22077 * Also handle function call with Funcref variable: func(expr)
22078 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22079 */
22080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022081handle_subscript(
22082 char_u **arg,
22083 typval_T *rettv,
22084 int evaluate, /* do more than finding the end */
22085 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022086{
22087 int ret = OK;
22088 dict_T *selfdict = NULL;
22089 char_u *s;
22090 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022091 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022092
22093 while (ret == OK
22094 && (**arg == '['
22095 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022096 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022097 && !vim_iswhite(*(*arg - 1)))
22098 {
22099 if (**arg == '(')
22100 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022101 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022102 if (evaluate)
22103 {
22104 functv = *rettv;
22105 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022106
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022107 /* Invoke the function. Recursive! */
22108 s = functv.vval.v_string;
22109 }
22110 else
22111 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022112 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022113 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22114 &len, evaluate, selfdict);
22115
22116 /* Clear the funcref afterwards, so that deleting it while
22117 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022118 if (evaluate)
22119 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022120
22121 /* Stop the expression evaluation when immediately aborting on
22122 * error, or when an interrupt occurred or an exception was thrown
22123 * but not caught. */
22124 if (aborting())
22125 {
22126 if (ret == OK)
22127 clear_tv(rettv);
22128 ret = FAIL;
22129 }
22130 dict_unref(selfdict);
22131 selfdict = NULL;
22132 }
22133 else /* **arg == '[' || **arg == '.' */
22134 {
22135 dict_unref(selfdict);
22136 if (rettv->v_type == VAR_DICT)
22137 {
22138 selfdict = rettv->vval.v_dict;
22139 if (selfdict != NULL)
22140 ++selfdict->dv_refcount;
22141 }
22142 else
22143 selfdict = NULL;
22144 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22145 {
22146 clear_tv(rettv);
22147 ret = FAIL;
22148 }
22149 }
22150 }
22151 dict_unref(selfdict);
22152 return ret;
22153}
22154
22155/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022156 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 * value).
22158 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022159 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022161{
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163}
22164
22165/*
22166 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 * The string "s" must have been allocated, it is consumed.
22168 * Return NULL for out of memory, the variable otherwise.
22169 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022171alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172{
Bram Moolenaar33570922005-01-25 22:26:29 +000022173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022175 rettv = alloc_tv();
22176 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022178 rettv->v_type = VAR_STRING;
22179 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 }
22181 else
22182 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022183 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184}
22185
22186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191{
22192 if (varp != NULL)
22193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022194 switch (varp->v_type)
22195 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022196 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022197 func_unref(varp->vval.v_string);
22198 /*FALLTHROUGH*/
22199 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022200 vim_free(varp->vval.v_string);
22201 break;
22202 case VAR_LIST:
22203 list_unref(varp->vval.v_list);
22204 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022205 case VAR_DICT:
22206 dict_unref(varp->vval.v_dict);
22207 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022208 case VAR_JOB:
22209#ifdef FEAT_JOB
22210 job_unref(varp->vval.v_job);
22211 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022212#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022213 case VAR_CHANNEL:
22214#ifdef FEAT_CHANNEL
22215 channel_unref(varp->vval.v_channel);
22216 break;
22217#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022218 case VAR_NUMBER:
22219 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022220 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022221 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022222 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 vim_free(varp);
22225 }
22226}
22227
22228/*
22229 * Free the memory for a variable value and set the value to NULL or 0.
22230 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022232clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233{
22234 if (varp != NULL)
22235 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022236 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022238 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022239 func_unref(varp->vval.v_string);
22240 /*FALLTHROUGH*/
22241 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022242 vim_free(varp->vval.v_string);
22243 varp->vval.v_string = NULL;
22244 break;
22245 case VAR_LIST:
22246 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022247 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022249 case VAR_DICT:
22250 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022251 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022252 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022253 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022254 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022255 varp->vval.v_number = 0;
22256 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022257 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022258#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022259 varp->vval.v_float = 0.0;
22260 break;
22261#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022262 case VAR_JOB:
22263#ifdef FEAT_JOB
22264 job_unref(varp->vval.v_job);
22265 varp->vval.v_job = NULL;
22266#endif
22267 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022268 case VAR_CHANNEL:
22269#ifdef FEAT_CHANNEL
22270 channel_unref(varp->vval.v_channel);
22271 varp->vval.v_channel = NULL;
22272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 case VAR_UNKNOWN:
22274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022276 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 }
22278}
22279
22280/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022281 * Set the value of a variable to NULL without freeing items.
22282 */
22283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022284init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022285{
22286 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288}
22289
22290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 * Get the number value of a variable.
22292 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022293 * For incompatible types, return 0.
22294 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22295 * caller of incompatible types: it sets *denote to TRUE if "denote"
22296 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 */
22298 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022299get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022301 int error = FALSE;
22302
22303 return get_tv_number_chk(varp, &error); /* return 0L on error */
22304}
22305
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022306 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022307get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022308{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022311 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022314 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022315 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022316#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022317 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022318 break;
22319#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022320 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022321 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022322 break;
22323 case VAR_STRING:
22324 if (varp->vval.v_string != NULL)
22325 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022326 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022327 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022328 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022329 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022330 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022331 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022332 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022333 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022334 case VAR_SPECIAL:
22335 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22336 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022337 case VAR_JOB:
22338#ifdef FEAT_JOB
22339 EMSG(_("E910: Using a Job as a Number"));
22340 break;
22341#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022342 case VAR_CHANNEL:
22343#ifdef FEAT_CHANNEL
22344 EMSG(_("E913: Using a Channel as a Number"));
22345 break;
22346#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022347 case VAR_UNKNOWN:
22348 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022351 if (denote == NULL) /* useful for values that must be unsigned */
22352 n = -1;
22353 else
22354 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022355 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356}
22357
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022358#ifdef FEAT_FLOAT
22359 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022360get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022361{
22362 switch (varp->v_type)
22363 {
22364 case VAR_NUMBER:
22365 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022366 case VAR_FLOAT:
22367 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022368 case VAR_FUNC:
22369 EMSG(_("E891: Using a Funcref as a Float"));
22370 break;
22371 case VAR_STRING:
22372 EMSG(_("E892: Using a String as a Float"));
22373 break;
22374 case VAR_LIST:
22375 EMSG(_("E893: Using a List as a Float"));
22376 break;
22377 case VAR_DICT:
22378 EMSG(_("E894: Using a Dictionary as a Float"));
22379 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022380 case VAR_SPECIAL:
22381 EMSG(_("E907: Using a special value as a Float"));
22382 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022383 case VAR_JOB:
22384# ifdef FEAT_JOB
22385 EMSG(_("E911: Using a Job as a Float"));
22386 break;
22387# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022388 case VAR_CHANNEL:
22389# ifdef FEAT_CHANNEL
22390 EMSG(_("E914: Using a Channel as a Float"));
22391 break;
22392# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022393 case VAR_UNKNOWN:
22394 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022395 break;
22396 }
22397 return 0;
22398}
22399#endif
22400
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022402 * Get the lnum from the first argument.
22403 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022404 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 */
22406 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022407get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408{
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 linenr_T lnum;
22411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022412 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 if (lnum == 0) /* no valid number, try using line() */
22414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 rettv.v_type = VAR_NUMBER;
22416 f_line(argvars, &rettv);
22417 lnum = rettv.vval.v_number;
22418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 }
22420 return lnum;
22421}
22422
22423/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022424 * Get the lnum from the first argument.
22425 * Also accepts "$", then "buf" is used.
22426 * Returns 0 on error.
22427 */
22428 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022429get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022430{
22431 if (argvars[0].v_type == VAR_STRING
22432 && argvars[0].vval.v_string != NULL
22433 && argvars[0].vval.v_string[0] == '$'
22434 && buf != NULL)
22435 return buf->b_ml.ml_line_count;
22436 return get_tv_number_chk(&argvars[0], NULL);
22437}
22438
22439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 * Get the string value of a variable.
22441 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022442 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22443 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 * If the String variable has never been set, return an empty string.
22445 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022446 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22447 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 */
22449 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022450get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451{
22452 static char_u mybuf[NUMBUFLEN];
22453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022454 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455}
22456
22457 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022458get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022460 char_u *res = get_tv_string_buf_chk(varp, buf);
22461
22462 return res != NULL ? res : (char_u *)"";
22463}
22464
Bram Moolenaar7d647822014-04-05 21:28:56 +020022465/*
22466 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22467 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022468 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022469get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022470{
22471 static char_u mybuf[NUMBUFLEN];
22472
22473 return get_tv_string_buf_chk(varp, mybuf);
22474}
22475
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022477get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022478{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022479 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 case VAR_NUMBER:
22482 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22483 return buf;
22484 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 break;
22487 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022488 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022489 break;
22490 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022491 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022492 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022493 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022494#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022495 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022496 break;
22497#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 case VAR_STRING:
22499 if (varp->vval.v_string != NULL)
22500 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022501 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022502 case VAR_SPECIAL:
22503 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22504 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022505 case VAR_JOB:
22506#ifdef FEAT_JOB
22507 {
22508 job_T *job = varp->vval.v_job;
22509 char *status = job->jv_status == JOB_FAILED ? "fail"
22510 : job->jv_status == JOB_ENDED ? "dead"
22511 : "run";
22512# ifdef UNIX
22513 vim_snprintf((char *)buf, NUMBUFLEN,
22514 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022515# elif defined(WIN32)
22516 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022517 "process %ld %s",
22518 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022519 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022520# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022521 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022522 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22523# endif
22524 return buf;
22525 }
22526#endif
22527 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022528 case VAR_CHANNEL:
22529#ifdef FEAT_CHANNEL
22530 {
22531 channel_T *channel = varp->vval.v_channel;
22532 char *status = channel_status(channel);
22533
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022534 if (channel == NULL)
22535 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22536 else
22537 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022538 "channel %d %s", channel->ch_id, status);
22539 return buf;
22540 }
22541#endif
22542 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022543 case VAR_UNKNOWN:
22544 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022547 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548}
22549
22550/*
22551 * Find variable "name" in the list of variables.
22552 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022554 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022558find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562
Bram Moolenaara7043832005-01-21 11:56:39 +000022563 ht = find_var_ht(name, &varname);
22564 if (htp != NULL)
22565 *htp = ht;
22566 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022568 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569}
22570
22571/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022572 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022573 * Returns NULL if not found.
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_in_ht(
22577 hashtab_T *ht,
22578 int htname,
22579 char_u *varname,
22580 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022581{
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 hashitem_T *hi;
22583
22584 if (*varname == NUL)
22585 {
22586 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022587 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022589 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 case 'g': return &globvars_var;
22591 case 'v': return &vimvars_var;
22592 case 'b': return &curbuf->b_bufvar;
22593 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022594#ifdef FEAT_WINDOWS
22595 case 't': return &curtab->tp_winvar;
22596#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022597 case 'l': return current_funccal == NULL
22598 ? NULL : &current_funccal->l_vars_var;
22599 case 'a': return current_funccal == NULL
22600 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 }
22602 return NULL;
22603 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022604
22605 hi = hash_find(ht, varname);
22606 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022607 {
22608 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022609 * worked find the variable again. Don't auto-load a script if it was
22610 * loaded already, otherwise it would be loaded every time when
22611 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022612 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022613 {
22614 /* Note: script_autoload() may make "hi" invalid. It must either
22615 * be obtained again or not used. */
22616 if (!script_autoload(varname, FALSE) || aborting())
22617 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022618 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022619 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022620 if (HASHITEM_EMPTY(hi))
22621 return NULL;
22622 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022623 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022624}
22625
22626/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022628 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022629 * Set "varname" to the start of name without ':'.
22630 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022631 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022632find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022634 hashitem_T *hi;
22635
Bram Moolenaar73627d02015-08-11 15:46:09 +020022636 if (name[0] == NUL)
22637 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 if (name[1] != ':')
22639 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022640 /* The name must not start with a colon or #. */
22641 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 return NULL;
22643 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022644
22645 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022646 hi = hash_find(&compat_hashtab, name);
22647 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022648 return &compat_hashtab;
22649
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022652 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653 }
22654 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022655 if (*name == 'g') /* global variable */
22656 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022657 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22658 */
22659 if (vim_strchr(name + 2, ':') != NULL
22660 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022661 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022663 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022665 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022666#ifdef FEAT_WINDOWS
22667 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022668 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 if (*name == 'v') /* v: variable */
22671 return &vimvarht;
22672 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022673 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022675 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 if (*name == 's' /* script variable */
22677 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22678 return &SCRIPT_VARS(current_SID);
22679 return NULL;
22680}
22681
22682/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022683 * Get function call environment based on bactrace debug level
22684 */
22685 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022686get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022687{
22688 int i;
22689 funccall_T *funccal;
22690 funccall_T *temp_funccal;
22691
22692 funccal = current_funccal;
22693 if (debug_backtrace_level > 0)
22694 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022695 for (i = 0; i < debug_backtrace_level; i++)
22696 {
22697 temp_funccal = funccal->caller;
22698 if (temp_funccal)
22699 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022700 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022701 /* backtrace level overflow. reset to max */
22702 debug_backtrace_level = i;
22703 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022704 }
22705 return funccal;
22706}
22707
22708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022710 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 * Returns NULL when it doesn't exist.
22712 */
22713 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022714get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715{
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022718 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 if (v == NULL)
22720 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722}
22723
22724/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022725 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 * sourcing this script and when executing functions defined in the script.
22727 */
22728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022729new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730{
Bram Moolenaara7043832005-01-21 11:56:39 +000022731 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 hashtab_T *ht;
22733 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022734
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022737 /* Re-allocating ga_data means that an ht_array pointing to
22738 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022740 for (i = 1; i <= ga_scripts.ga_len; ++i)
22741 {
22742 ht = &SCRIPT_VARS(i);
22743 if (ht->ht_mask == HT_INIT_SIZE - 1)
22744 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022745 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 }
22748
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 while (ga_scripts.ga_len < id)
22750 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022751 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022752 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022753 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 }
22756 }
22757}
22758
22759/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022760 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22761 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 */
22763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022764init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765{
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022767 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022768 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022769 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022770 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 dict_var->di_tv.vval.v_dict = dict;
22772 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022773 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22775 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776}
22777
22778/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022779 * Unreference a dictionary initialized by init_var_dict().
22780 */
22781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022782unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022783{
22784 /* Now the dict needs to be freed if no one else is using it, go back to
22785 * normal reference counting. */
22786 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22787 dict_unref(dict);
22788}
22789
22790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 * Frees all allocated variables and the value they contain.
22793 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 */
22795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022796vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022797{
22798 vars_clear_ext(ht, TRUE);
22799}
22800
22801/*
22802 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22803 */
22804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022805vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806{
Bram Moolenaara7043832005-01-21 11:56:39 +000022807 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022808 hashitem_T *hi;
22809 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810
Bram Moolenaar33570922005-01-25 22:26:29 +000022811 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022812 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022813 for (hi = ht->ht_array; todo > 0; ++hi)
22814 {
22815 if (!HASHITEM_EMPTY(hi))
22816 {
22817 --todo;
22818
Bram Moolenaar33570922005-01-25 22:26:29 +000022819 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022820 * ht_array might change then. hash_clear() takes care of it
22821 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 v = HI2DI(hi);
22823 if (free_val)
22824 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022825 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022826 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022827 }
22828 }
22829 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022830 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831}
22832
Bram Moolenaara7043832005-01-21 11:56:39 +000022833/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 * Delete a variable from hashtab "ht" at item "hi".
22835 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022841
22842 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 clear_tv(&di->di_tv);
22844 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845}
22846
22847/*
22848 * List the value of one internal variable.
22849 */
22850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022851list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 char_u *tofree;
22854 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022855 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022856
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022857 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022859 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022860 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861}
22862
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022864list_one_var_a(
22865 char_u *prefix,
22866 char_u *name,
22867 int type,
22868 char_u *string,
22869 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870{
Bram Moolenaar31859182007-08-14 20:41:13 +000022871 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22872 msg_start();
22873 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 if (name != NULL) /* "a:" vars don't have a name stored */
22875 msg_puts(name);
22876 msg_putchar(' ');
22877 msg_advance(22);
22878 if (type == VAR_NUMBER)
22879 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022880 else if (type == VAR_FUNC)
22881 msg_putchar('*');
22882 else if (type == VAR_LIST)
22883 {
22884 msg_putchar('[');
22885 if (*string == '[')
22886 ++string;
22887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022888 else if (type == VAR_DICT)
22889 {
22890 msg_putchar('{');
22891 if (*string == '{')
22892 ++string;
22893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 else
22895 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022896
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022898
22899 if (type == VAR_FUNC)
22900 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022901 if (*first)
22902 {
22903 msg_clr_eos();
22904 *first = FALSE;
22905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906}
22907
22908/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022909 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 * If the variable already exists, the value is updated.
22911 * Otherwise the variable is created.
22912 */
22913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022914set_var(
22915 char_u *name,
22916 typval_T *tv,
22917 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918{
Bram Moolenaar33570922005-01-25 22:26:29 +000022919 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022923 ht = find_var_ht(name, &varname);
22924 if (ht == NULL || *varname == NUL)
22925 {
22926 EMSG2(_(e_illvar), name);
22927 return;
22928 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022929 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022930
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022931 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22932 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022933
Bram Moolenaar33570922005-01-25 22:26:29 +000022934 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022937 if (var_check_ro(v->di_flags, name, FALSE)
22938 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 return;
22940 if (v->di_tv.v_type != tv->v_type
22941 && !((v->di_tv.v_type == VAR_STRING
22942 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022944 || tv->v_type == VAR_NUMBER))
22945#ifdef FEAT_FLOAT
22946 && !((v->di_tv.v_type == VAR_NUMBER
22947 || v->di_tv.v_type == VAR_FLOAT)
22948 && (tv->v_type == VAR_NUMBER
22949 || tv->v_type == VAR_FLOAT))
22950#endif
22951 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022952 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022953 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022954 return;
22955 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022956
22957 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022958 * Handle setting internal v: variables separately where needed to
22959 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022960 */
22961 if (ht == &vimvarht)
22962 {
22963 if (v->di_tv.v_type == VAR_STRING)
22964 {
22965 vim_free(v->di_tv.vval.v_string);
22966 if (copy || tv->v_type != VAR_STRING)
22967 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22968 else
22969 {
22970 /* Take over the string to avoid an extra alloc/free. */
22971 v->di_tv.vval.v_string = tv->vval.v_string;
22972 tv->vval.v_string = NULL;
22973 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022974 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022976 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022979 if (STRCMP(varname, "searchforward") == 0)
22980 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022981#ifdef FEAT_SEARCH_EXTRA
22982 else if (STRCMP(varname, "hlsearch") == 0)
22983 {
22984 no_hlsearch = !v->di_tv.vval.v_number;
22985 redraw_all_later(SOME_VALID);
22986 }
22987#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022988 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022989 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022990 else if (v->di_tv.v_type != tv->v_type)
22991 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 }
22993
22994 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 }
22996 else /* add a new variable */
22997 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022998 /* Can't add "v:" variable. */
22999 if (ht == &vimvarht)
23000 {
23001 EMSG2(_(e_illvar), name);
23002 return;
23003 }
23004
Bram Moolenaar92124a32005-06-17 22:03:40 +000023005 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023006 if (!valid_varname(varname))
23007 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023008
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023009 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23010 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023011 if (v == NULL)
23012 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023016 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 return;
23018 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023019 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023021
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023023 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023024 else
23025 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023026 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023027 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030}
23031
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023032/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023033 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023034 * Also give an error message.
23035 */
23036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023037var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023038{
23039 if (flags & DI_FLAGS_RO)
23040 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023041 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 return TRUE;
23043 }
23044 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23045 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023046 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 return TRUE;
23048 }
23049 return FALSE;
23050}
23051
23052/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023053 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23054 * Also give an error message.
23055 */
23056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023057var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023058{
23059 if (flags & DI_FLAGS_FIX)
23060 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023061 EMSG2(_("E795: Cannot delete variable %s"),
23062 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023063 return TRUE;
23064 }
23065 return FALSE;
23066}
23067
23068/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023069 * Check if a funcref is assigned to a valid variable name.
23070 * Return TRUE and give an error if not.
23071 */
23072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023073var_check_func_name(
23074 char_u *name, /* points to start of variable name */
23075 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023076{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023077 /* Allow for w: b: s: and t:. */
23078 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023079 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23080 ? name[2] : name[0]))
23081 {
23082 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23083 name);
23084 return TRUE;
23085 }
23086 /* Don't allow hiding a function. When "v" is not NULL we might be
23087 * assigning another function to the same var, the type is checked
23088 * below. */
23089 if (new_var && function_exists(name))
23090 {
23091 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23092 name);
23093 return TRUE;
23094 }
23095 return FALSE;
23096}
23097
23098/*
23099 * Check if a variable name is valid.
23100 * Return FALSE and give an error if not.
23101 */
23102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023103valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023104{
23105 char_u *p;
23106
23107 for (p = varname; *p != NUL; ++p)
23108 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23109 && *p != AUTOLOAD_CHAR)
23110 {
23111 EMSG2(_(e_illvar), varname);
23112 return FALSE;
23113 }
23114 return TRUE;
23115}
23116
23117/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023118 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023119 * Also give an error message, using "name" or _("name") when use_gettext is
23120 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023121 */
23122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023123tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023124{
23125 if (lock & VAR_LOCKED)
23126 {
23127 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023128 name == NULL ? (char_u *)_("Unknown")
23129 : use_gettext ? (char_u *)_(name)
23130 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023131 return TRUE;
23132 }
23133 if (lock & VAR_FIXED)
23134 {
23135 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023136 name == NULL ? (char_u *)_("Unknown")
23137 : use_gettext ? (char_u *)_(name)
23138 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023139 return TRUE;
23140 }
23141 return FALSE;
23142}
23143
23144/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023146 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023147 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023148 * It is OK for "from" and "to" to point to the same item. This is used to
23149 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023150 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023152copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023154 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023155 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023156 switch (from->v_type)
23157 {
23158 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023159 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023160 to->vval.v_number = from->vval.v_number;
23161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023162 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023163#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023164 to->vval.v_float = from->vval.v_float;
23165 break;
23166#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023167 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023168#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023169 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023170 if (to->vval.v_job != NULL)
23171 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023172 break;
23173#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023174 case VAR_CHANNEL:
23175#ifdef FEAT_CHANNEL
23176 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023177 if (to->vval.v_channel != NULL)
23178 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023179 break;
23180#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023181 case VAR_STRING:
23182 case VAR_FUNC:
23183 if (from->vval.v_string == NULL)
23184 to->vval.v_string = NULL;
23185 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023186 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023187 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023188 if (from->v_type == VAR_FUNC)
23189 func_ref(to->vval.v_string);
23190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023191 break;
23192 case VAR_LIST:
23193 if (from->vval.v_list == NULL)
23194 to->vval.v_list = NULL;
23195 else
23196 {
23197 to->vval.v_list = from->vval.v_list;
23198 ++to->vval.v_list->lv_refcount;
23199 }
23200 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023201 case VAR_DICT:
23202 if (from->vval.v_dict == NULL)
23203 to->vval.v_dict = NULL;
23204 else
23205 {
23206 to->vval.v_dict = from->vval.v_dict;
23207 ++to->vval.v_dict->dv_refcount;
23208 }
23209 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023210 case VAR_UNKNOWN:
23211 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023212 break;
23213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214}
23215
23216/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023217 * Make a copy of an item.
23218 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023219 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23220 * reference to an already copied list/dict can be used.
23221 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023222 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023224item_copy(
23225 typval_T *from,
23226 typval_T *to,
23227 int deep,
23228 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023229{
23230 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023231 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023232
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023234 {
23235 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023236 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023237 }
23238 ++recurse;
23239
23240 switch (from->v_type)
23241 {
23242 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023243 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023244 case VAR_STRING:
23245 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023246 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023247 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023248 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023249 copy_tv(from, to);
23250 break;
23251 case VAR_LIST:
23252 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023253 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023254 if (from->vval.v_list == NULL)
23255 to->vval.v_list = NULL;
23256 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23257 {
23258 /* use the copy made earlier */
23259 to->vval.v_list = from->vval.v_list->lv_copylist;
23260 ++to->vval.v_list->lv_refcount;
23261 }
23262 else
23263 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23264 if (to->vval.v_list == NULL)
23265 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023266 break;
23267 case VAR_DICT:
23268 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023269 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023270 if (from->vval.v_dict == NULL)
23271 to->vval.v_dict = NULL;
23272 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23273 {
23274 /* use the copy made earlier */
23275 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23276 ++to->vval.v_dict->dv_refcount;
23277 }
23278 else
23279 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23280 if (to->vval.v_dict == NULL)
23281 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023282 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023283 case VAR_UNKNOWN:
23284 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023285 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023286 }
23287 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023288 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023289}
23290
23291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 * ":echo expr1 ..." print each argument separated with a space, add a
23293 * newline at the end.
23294 * ":echon expr1 ..." print each argument plain.
23295 */
23296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023297ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298{
23299 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023300 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023301 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 char_u *p;
23303 int needclr = TRUE;
23304 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023305 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306
23307 if (eap->skip)
23308 ++emsg_skip;
23309 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23310 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023311 /* If eval1() causes an error message the text from the command may
23312 * still need to be cleared. E.g., "echo 22,44". */
23313 need_clr_eos = needclr;
23314
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023316 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 {
23318 /*
23319 * Report the invalid expression unless the expression evaluation
23320 * has been cancelled due to an aborting error, an interrupt, or an
23321 * exception.
23322 */
23323 if (!aborting())
23324 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023325 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 break;
23327 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023328 need_clr_eos = FALSE;
23329
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 if (!eap->skip)
23331 {
23332 if (atstart)
23333 {
23334 atstart = FALSE;
23335 /* Call msg_start() after eval1(), evaluating the expression
23336 * may cause a message to appear. */
23337 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023338 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023339 /* Mark the saved text as finishing the line, so that what
23340 * follows is displayed on a new line when scrolling back
23341 * at the more prompt. */
23342 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 }
23346 else if (eap->cmdidx == CMD_echo)
23347 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023348 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023349 if (p != NULL)
23350 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023352 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023354 if (*p != TAB && needclr)
23355 {
23356 /* remove any text still there from the command */
23357 msg_clr_eos();
23358 needclr = FALSE;
23359 }
23360 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 }
23362 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023363 {
23364#ifdef FEAT_MBYTE
23365 if (has_mbyte)
23366 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023367 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023368
23369 (void)msg_outtrans_len_attr(p, i, echo_attr);
23370 p += i - 1;
23371 }
23372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023374 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023377 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023379 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 arg = skipwhite(arg);
23381 }
23382 eap->nextcmd = check_nextcmd(arg);
23383
23384 if (eap->skip)
23385 --emsg_skip;
23386 else
23387 {
23388 /* remove text that may still be there from the command */
23389 if (needclr)
23390 msg_clr_eos();
23391 if (eap->cmdidx == CMD_echo)
23392 msg_end();
23393 }
23394}
23395
23396/*
23397 * ":echohl {name}".
23398 */
23399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023400ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401{
23402 int id;
23403
23404 id = syn_name2id(eap->arg);
23405 if (id == 0)
23406 echo_attr = 0;
23407 else
23408 echo_attr = syn_id2attr(id);
23409}
23410
23411/*
23412 * ":execute expr1 ..." execute the result of an expression.
23413 * ":echomsg expr1 ..." Print a message
23414 * ":echoerr expr1 ..." Print an error
23415 * Each gets spaces around each argument and a newline at the end for
23416 * echo commands
23417 */
23418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023419ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420{
23421 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023422 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 int ret = OK;
23424 char_u *p;
23425 garray_T ga;
23426 int len;
23427 int save_did_emsg;
23428
23429 ga_init2(&ga, 1, 80);
23430
23431 if (eap->skip)
23432 ++emsg_skip;
23433 while (*arg != NUL && *arg != '|' && *arg != '\n')
23434 {
23435 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023436 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 {
23438 /*
23439 * Report the invalid expression unless the expression evaluation
23440 * has been cancelled due to an aborting error, an interrupt, or an
23441 * exception.
23442 */
23443 if (!aborting())
23444 EMSG2(_(e_invexpr2), p);
23445 ret = FAIL;
23446 break;
23447 }
23448
23449 if (!eap->skip)
23450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023451 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 len = (int)STRLEN(p);
23453 if (ga_grow(&ga, len + 2) == FAIL)
23454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023455 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 ret = FAIL;
23457 break;
23458 }
23459 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 ga.ga_len += len;
23463 }
23464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023465 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 arg = skipwhite(arg);
23467 }
23468
23469 if (ret != FAIL && ga.ga_data != NULL)
23470 {
23471 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023474 out_flush();
23475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 else if (eap->cmdidx == CMD_echoerr)
23477 {
23478 /* We don't want to abort following commands, restore did_emsg. */
23479 save_did_emsg = did_emsg;
23480 EMSG((char_u *)ga.ga_data);
23481 if (!force_abort)
23482 did_emsg = save_did_emsg;
23483 }
23484 else if (eap->cmdidx == CMD_execute)
23485 do_cmdline((char_u *)ga.ga_data,
23486 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23487 }
23488
23489 ga_clear(&ga);
23490
23491 if (eap->skip)
23492 --emsg_skip;
23493
23494 eap->nextcmd = check_nextcmd(arg);
23495}
23496
23497/*
23498 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23499 * "arg" points to the "&" or '+' when called, to "option" when returning.
23500 * Returns NULL when no option name found. Otherwise pointer to the char
23501 * after the option name.
23502 */
23503 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023504find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505{
23506 char_u *p = *arg;
23507
23508 ++p;
23509 if (*p == 'g' && p[1] == ':')
23510 {
23511 *opt_flags = OPT_GLOBAL;
23512 p += 2;
23513 }
23514 else if (*p == 'l' && p[1] == ':')
23515 {
23516 *opt_flags = OPT_LOCAL;
23517 p += 2;
23518 }
23519 else
23520 *opt_flags = 0;
23521
23522 if (!ASCII_ISALPHA(*p))
23523 return NULL;
23524 *arg = p;
23525
23526 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23527 p += 4; /* termcap option */
23528 else
23529 while (ASCII_ISALPHA(*p))
23530 ++p;
23531 return p;
23532}
23533
23534/*
23535 * ":function"
23536 */
23537 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023538ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539{
23540 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023541 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 int j;
23543 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023545 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 char_u *name = NULL;
23547 char_u *p;
23548 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023549 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 garray_T newargs;
23551 garray_T newlines;
23552 int varargs = FALSE;
23553 int mustend = FALSE;
23554 int flags = 0;
23555 ufunc_T *fp;
23556 int indent;
23557 int nesting;
23558 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023559 dictitem_T *v;
23560 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023561 static int func_nr = 0; /* number for nameless function */
23562 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023563 hashtab_T *ht;
23564 int todo;
23565 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023566 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567
23568 /*
23569 * ":function" without argument: list functions.
23570 */
23571 if (ends_excmd(*eap->arg))
23572 {
23573 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023574 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023575 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023576 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023577 {
23578 if (!HASHITEM_EMPTY(hi))
23579 {
23580 --todo;
23581 fp = HI2UF(hi);
23582 if (!isdigit(*fp->uf_name))
23583 list_func_head(fp, FALSE);
23584 }
23585 }
23586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 eap->nextcmd = check_nextcmd(eap->arg);
23588 return;
23589 }
23590
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023591 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023592 * ":function /pat": list functions matching pattern.
23593 */
23594 if (*eap->arg == '/')
23595 {
23596 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23597 if (!eap->skip)
23598 {
23599 regmatch_T regmatch;
23600
23601 c = *p;
23602 *p = NUL;
23603 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23604 *p = c;
23605 if (regmatch.regprog != NULL)
23606 {
23607 regmatch.rm_ic = p_ic;
23608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023609 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023610 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23611 {
23612 if (!HASHITEM_EMPTY(hi))
23613 {
23614 --todo;
23615 fp = HI2UF(hi);
23616 if (!isdigit(*fp->uf_name)
23617 && vim_regexec(&regmatch, fp->uf_name, 0))
23618 list_func_head(fp, FALSE);
23619 }
23620 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023621 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023622 }
23623 }
23624 if (*p == '/')
23625 ++p;
23626 eap->nextcmd = check_nextcmd(p);
23627 return;
23628 }
23629
23630 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023631 * Get the function name. There are these situations:
23632 * func normal function name
23633 * "name" == func, "fudi.fd_dict" == NULL
23634 * dict.func new dictionary entry
23635 * "name" == NULL, "fudi.fd_dict" set,
23636 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23637 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023638 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023639 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23640 * dict.func existing dict entry that's not a Funcref
23641 * "name" == NULL, "fudi.fd_dict" set,
23642 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023643 * s:func script-local function name
23644 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023647 name = trans_function_name(&p, eap->skip, 0, &fudi);
23648 paren = (vim_strchr(p, '(') != NULL);
23649 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 {
23651 /*
23652 * Return on an invalid expression in braces, unless the expression
23653 * evaluation has been cancelled due to an aborting error, an
23654 * interrupt, or an exception.
23655 */
23656 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023658 if (!eap->skip && fudi.fd_newkey != NULL)
23659 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023660 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 else
23664 eap->skip = TRUE;
23665 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023666
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 /* An error in a function call during evaluation of an expression in magic
23668 * braces should not cause the function not to be defined. */
23669 saved_did_emsg = did_emsg;
23670 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671
23672 /*
23673 * ":function func" with only function name: list function.
23674 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 {
23677 if (!ends_excmd(*skipwhite(p)))
23678 {
23679 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023680 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
23682 eap->nextcmd = check_nextcmd(p);
23683 if (eap->nextcmd != NULL)
23684 *p = NUL;
23685 if (!eap->skip && !got_int)
23686 {
23687 fp = find_func(name);
23688 if (fp != NULL)
23689 {
23690 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023691 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023693 if (FUNCLINE(fp, j) == NULL)
23694 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 msg_putchar('\n');
23696 msg_outnum((long)(j + 1));
23697 if (j < 9)
23698 msg_putchar(' ');
23699 if (j < 99)
23700 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023701 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 out_flush(); /* show a line at a time */
23703 ui_breakcheck();
23704 }
23705 if (!got_int)
23706 {
23707 msg_putchar('\n');
23708 msg_puts((char_u *)" endfunction");
23709 }
23710 }
23711 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023712 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715 }
23716
23717 /*
23718 * ":function name(arg1, arg2)" Define function.
23719 */
23720 p = skipwhite(p);
23721 if (*p != '(')
23722 {
23723 if (!eap->skip)
23724 {
23725 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023726 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 }
23728 /* attempt to continue by skipping some text */
23729 if (vim_strchr(p, '(') != NULL)
23730 p = vim_strchr(p, '(');
23731 }
23732 p = skipwhite(p + 1);
23733
23734 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23735 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23736
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023737 if (!eap->skip)
23738 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023739 /* Check the name of the function. Unless it's a dictionary function
23740 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023741 if (name != NULL)
23742 arg = name;
23743 else
23744 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023745 if (arg != NULL && (fudi.fd_di == NULL
23746 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023747 {
23748 if (*arg == K_SPECIAL)
23749 j = 3;
23750 else
23751 j = 0;
23752 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23753 : eval_isnamec(arg[j])))
23754 ++j;
23755 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023756 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023757 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023758 /* Disallow using the g: dict. */
23759 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23760 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023761 }
23762
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 /*
23764 * Isolate the arguments: "arg1, arg2, ...)"
23765 */
23766 while (*p != ')')
23767 {
23768 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23769 {
23770 varargs = TRUE;
23771 p += 3;
23772 mustend = TRUE;
23773 }
23774 else
23775 {
23776 arg = p;
23777 while (ASCII_ISALNUM(*p) || *p == '_')
23778 ++p;
23779 if (arg == p || isdigit(*arg)
23780 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23781 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23782 {
23783 if (!eap->skip)
23784 EMSG2(_("E125: Illegal argument: %s"), arg);
23785 break;
23786 }
23787 if (ga_grow(&newargs, 1) == FAIL)
23788 goto erret;
23789 c = *p;
23790 *p = NUL;
23791 arg = vim_strsave(arg);
23792 if (arg == NULL)
23793 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023794
23795 /* Check for duplicate argument name. */
23796 for (i = 0; i < newargs.ga_len; ++i)
23797 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23798 {
23799 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023800 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023801 goto erret;
23802 }
23803
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23805 *p = c;
23806 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 if (*p == ',')
23808 ++p;
23809 else
23810 mustend = TRUE;
23811 }
23812 p = skipwhite(p);
23813 if (mustend && *p != ')')
23814 {
23815 if (!eap->skip)
23816 EMSG2(_(e_invarg2), eap->arg);
23817 break;
23818 }
23819 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023820 if (*p != ')')
23821 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 ++p; /* skip the ')' */
23823
Bram Moolenaare9a41262005-01-15 22:18:47 +000023824 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 for (;;)
23826 {
23827 p = skipwhite(p);
23828 if (STRNCMP(p, "range", 5) == 0)
23829 {
23830 flags |= FC_RANGE;
23831 p += 5;
23832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023833 else if (STRNCMP(p, "dict", 4) == 0)
23834 {
23835 flags |= FC_DICT;
23836 p += 4;
23837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 else if (STRNCMP(p, "abort", 5) == 0)
23839 {
23840 flags |= FC_ABORT;
23841 p += 5;
23842 }
23843 else
23844 break;
23845 }
23846
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023847 /* When there is a line break use what follows for the function body.
23848 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23849 if (*p == '\n')
23850 line_arg = p + 1;
23851 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 EMSG(_(e_trailing));
23853
23854 /*
23855 * Read the body of the function, until ":endfunction" is found.
23856 */
23857 if (KeyTyped)
23858 {
23859 /* Check if the function already exists, don't let the user type the
23860 * whole function before telling him it doesn't work! For a script we
23861 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023862 if (!eap->skip && !eap->forceit)
23863 {
23864 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23865 EMSG(_(e_funcdict));
23866 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023867 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023870 if (!eap->skip && did_emsg)
23871 goto erret;
23872
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 msg_putchar('\n'); /* don't overwrite the function name */
23874 cmdline_row = msg_row;
23875 }
23876
23877 indent = 2;
23878 nesting = 0;
23879 for (;;)
23880 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023881 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023882 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023883 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023884 saved_wait_return = FALSE;
23885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023887 sourcing_lnum_off = sourcing_lnum;
23888
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023889 if (line_arg != NULL)
23890 {
23891 /* Use eap->arg, split up in parts by line breaks. */
23892 theline = line_arg;
23893 p = vim_strchr(theline, '\n');
23894 if (p == NULL)
23895 line_arg += STRLEN(line_arg);
23896 else
23897 {
23898 *p = NUL;
23899 line_arg = p + 1;
23900 }
23901 }
23902 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 theline = getcmdline(':', 0L, indent);
23904 else
23905 theline = eap->getline(':', eap->cookie, indent);
23906 if (KeyTyped)
23907 lines_left = Rows - 1;
23908 if (theline == NULL)
23909 {
23910 EMSG(_("E126: Missing :endfunction"));
23911 goto erret;
23912 }
23913
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023914 /* Detect line continuation: sourcing_lnum increased more than one. */
23915 if (sourcing_lnum > sourcing_lnum_off + 1)
23916 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23917 else
23918 sourcing_lnum_off = 0;
23919
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 if (skip_until != NULL)
23921 {
23922 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23923 * don't check for ":endfunc". */
23924 if (STRCMP(theline, skip_until) == 0)
23925 {
23926 vim_free(skip_until);
23927 skip_until = NULL;
23928 }
23929 }
23930 else
23931 {
23932 /* skip ':' and blanks*/
23933 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23934 ;
23935
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023936 /* Check for "endfunction". */
23937 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023939 if (line_arg == NULL)
23940 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 break;
23942 }
23943
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023944 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 * at "end". */
23946 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23947 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023948 else if (STRNCMP(p, "if", 2) == 0
23949 || STRNCMP(p, "wh", 2) == 0
23950 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 || STRNCMP(p, "try", 3) == 0)
23952 indent += 2;
23953
23954 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023955 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023957 if (*p == '!')
23958 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023960 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23961 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023963 ++nesting;
23964 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 }
23966 }
23967
23968 /* Check for ":append" or ":insert". */
23969 p = skip_range(p, NULL);
23970 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23971 || (p[0] == 'i'
23972 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23973 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23974 skip_until = vim_strsave((char_u *)".");
23975
23976 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23977 arg = skipwhite(skiptowhite(p));
23978 if (arg[0] == '<' && arg[1] =='<'
23979 && ((p[0] == 'p' && p[1] == 'y'
23980 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23981 || (p[0] == 'p' && p[1] == 'e'
23982 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23983 || (p[0] == 't' && p[1] == 'c'
23984 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023985 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23986 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23988 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023989 || (p[0] == 'm' && p[1] == 'z'
23990 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 ))
23992 {
23993 /* ":python <<" continues until a dot, like ":append" */
23994 p = skipwhite(arg + 2);
23995 if (*p == NUL)
23996 skip_until = vim_strsave((char_u *)".");
23997 else
23998 skip_until = vim_strsave(p);
23999 }
24000 }
24001
24002 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024003 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024004 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024005 if (line_arg == NULL)
24006 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024008 }
24009
24010 /* Copy the line to newly allocated memory. get_one_sourceline()
24011 * allocates 250 bytes per line, this saves 80% on average. The cost
24012 * is an extra alloc/free. */
24013 p = vim_strsave(theline);
24014 if (p != NULL)
24015 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024016 if (line_arg == NULL)
24017 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024018 theline = p;
24019 }
24020
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024021 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24022
24023 /* Add NULL lines for continuation lines, so that the line count is
24024 * equal to the index in the growarray. */
24025 while (sourcing_lnum_off-- > 0)
24026 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024027
24028 /* Check for end of eap->arg. */
24029 if (line_arg != NULL && *line_arg == NUL)
24030 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 }
24032
24033 /* Don't define the function when skipping commands or when an error was
24034 * detected. */
24035 if (eap->skip || did_emsg)
24036 goto erret;
24037
24038 /*
24039 * If there are no errors, add the function
24040 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024041 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024042 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024043 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024044 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024045 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024046 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024047 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048 goto erret;
24049 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024050
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024051 fp = find_func(name);
24052 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024054 if (!eap->forceit)
24055 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024056 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024057 goto erret;
24058 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024059 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024061 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 name);
24063 goto erret;
24064 }
24065 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 ga_clear_strings(&(fp->uf_args));
24067 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024068 vim_free(name);
24069 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 }
24072 else
24073 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024074 char numbuf[20];
24075
24076 fp = NULL;
24077 if (fudi.fd_newkey == NULL && !eap->forceit)
24078 {
24079 EMSG(_(e_funcdict));
24080 goto erret;
24081 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024082 if (fudi.fd_di == NULL)
24083 {
24084 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024085 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024086 goto erret;
24087 }
24088 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024089 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024090 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091
24092 /* Give the function a sequential number. Can only be used with a
24093 * Funcref! */
24094 vim_free(name);
24095 sprintf(numbuf, "%d", ++func_nr);
24096 name = vim_strsave((char_u *)numbuf);
24097 if (name == NULL)
24098 goto erret;
24099 }
24100
24101 if (fp == NULL)
24102 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024103 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 {
24105 int slen, plen;
24106 char_u *scriptname;
24107
24108 /* Check that the autoload name matches the script name. */
24109 j = FAIL;
24110 if (sourcing_name != NULL)
24111 {
24112 scriptname = autoload_name(name);
24113 if (scriptname != NULL)
24114 {
24115 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024116 plen = (int)STRLEN(p);
24117 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 if (slen > plen && fnamecmp(p,
24119 sourcing_name + slen - plen) == 0)
24120 j = OK;
24121 vim_free(scriptname);
24122 }
24123 }
24124 if (j == FAIL)
24125 {
24126 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24127 goto erret;
24128 }
24129 }
24130
24131 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132 if (fp == NULL)
24133 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024134
24135 if (fudi.fd_dict != NULL)
24136 {
24137 if (fudi.fd_di == NULL)
24138 {
24139 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024140 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024141 if (fudi.fd_di == NULL)
24142 {
24143 vim_free(fp);
24144 goto erret;
24145 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024146 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24147 {
24148 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024149 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024150 goto erret;
24151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024152 }
24153 else
24154 /* overwrite existing dict entry */
24155 clear_tv(&fudi.fd_di->di_tv);
24156 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024157 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024160
24161 /* behave like "dict" was used */
24162 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024163 }
24164
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024166 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024167 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24168 {
24169 vim_free(fp);
24170 goto erret;
24171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024173 fp->uf_args = newargs;
24174 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024175#ifdef FEAT_PROFILE
24176 fp->uf_tml_count = NULL;
24177 fp->uf_tml_total = NULL;
24178 fp->uf_tml_self = NULL;
24179 fp->uf_profiling = FALSE;
24180 if (prof_def_func())
24181 func_do_profile(fp);
24182#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 fp->uf_varargs = varargs;
24184 fp->uf_flags = flags;
24185 fp->uf_calls = 0;
24186 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024187 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188
24189erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 ga_clear_strings(&newargs);
24191 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024192ret_free:
24193 vim_free(skip_until);
24194 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024197 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198}
24199
24200/*
24201 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024202 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024205 * TFN_INT: internal function name OK
24206 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024207 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 * Advances "pp" to just after the function name (if no error).
24209 */
24210 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024211trans_function_name(
24212 char_u **pp,
24213 int skip, /* only find the end, don't evaluate */
24214 int flags,
24215 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024217 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 char_u *start;
24219 char_u *end;
24220 int lead;
24221 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224
24225 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024226 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024228
24229 /* Check for hard coded <SNR>: already translated function ID (from a user
24230 * command). */
24231 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24232 && (*pp)[2] == (int)KE_SNR)
24233 {
24234 *pp += 3;
24235 len = get_id_len(pp) + 3;
24236 return vim_strnsave(start, len);
24237 }
24238
24239 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24240 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024242 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024244
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024245 /* Note that TFN_ flags use the same values as GLV_ flags. */
24246 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024247 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024248 if (end == start)
24249 {
24250 if (!skip)
24251 EMSG(_("E129: Function name required"));
24252 goto theend;
24253 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024254 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024255 {
24256 /*
24257 * Report an invalid expression in braces, unless the expression
24258 * evaluation has been cancelled due to an aborting error, an
24259 * interrupt, or an exception.
24260 */
24261 if (!aborting())
24262 {
24263 if (end != NULL)
24264 EMSG2(_(e_invarg2), start);
24265 }
24266 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024267 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024268 goto theend;
24269 }
24270
24271 if (lv.ll_tv != NULL)
24272 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024273 if (fdp != NULL)
24274 {
24275 fdp->fd_dict = lv.ll_dict;
24276 fdp->fd_newkey = lv.ll_newkey;
24277 lv.ll_newkey = NULL;
24278 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024279 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024280 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24281 {
24282 name = vim_strsave(lv.ll_tv->vval.v_string);
24283 *pp = end;
24284 }
24285 else
24286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024287 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24288 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024289 EMSG(_(e_funcref));
24290 else
24291 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024292 name = NULL;
24293 }
24294 goto theend;
24295 }
24296
24297 if (lv.ll_name == NULL)
24298 {
24299 /* Error found, but continue after the function name. */
24300 *pp = end;
24301 goto theend;
24302 }
24303
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024304 /* Check if the name is a Funcref. If so, use the value. */
24305 if (lv.ll_exp_name != NULL)
24306 {
24307 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024308 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024309 if (name == lv.ll_exp_name)
24310 name = NULL;
24311 }
24312 else
24313 {
24314 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024315 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024316 if (name == *pp)
24317 name = NULL;
24318 }
24319 if (name != NULL)
24320 {
24321 name = vim_strsave(name);
24322 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024323 if (STRNCMP(name, "<SNR>", 5) == 0)
24324 {
24325 /* Change "<SNR>" to the byte sequence. */
24326 name[0] = K_SPECIAL;
24327 name[1] = KS_EXTRA;
24328 name[2] = (int)KE_SNR;
24329 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24330 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024331 goto theend;
24332 }
24333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024334 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024335 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024336 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024337 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24338 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24339 {
24340 /* When there was "s:" already or the name expanded to get a
24341 * leading "s:" then remove it. */
24342 lv.ll_name += 2;
24343 len -= 2;
24344 lead = 2;
24345 }
24346 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024347 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024348 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024349 /* skip over "s:" and "g:" */
24350 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024351 lv.ll_name += 2;
24352 len = (int)(end - lv.ll_name);
24353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024354
24355 /*
24356 * Copy the function name to allocated memory.
24357 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24358 * Accept <SNR>123_name() outside a script.
24359 */
24360 if (skip)
24361 lead = 0; /* do nothing */
24362 else if (lead > 0)
24363 {
24364 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024365 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24366 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024367 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024368 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024369 if (current_SID <= 0)
24370 {
24371 EMSG(_(e_usingsid));
24372 goto theend;
24373 }
24374 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24375 lead += (int)STRLEN(sid_buf);
24376 }
24377 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024378 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024379 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024380 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024381 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024382 goto theend;
24383 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024384 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024385 {
24386 char_u *cp = vim_strchr(lv.ll_name, ':');
24387
24388 if (cp != NULL && cp < end)
24389 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024390 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024391 goto theend;
24392 }
24393 }
24394
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024395 name = alloc((unsigned)(len + lead + 1));
24396 if (name != NULL)
24397 {
24398 if (lead > 0)
24399 {
24400 name[0] = K_SPECIAL;
24401 name[1] = KS_EXTRA;
24402 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024403 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024404 STRCPY(name + 3, sid_buf);
24405 }
24406 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024407 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024408 }
24409 *pp = end;
24410
24411theend:
24412 clear_lval(&lv);
24413 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414}
24415
24416/*
24417 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24418 * Return 2 if "p" starts with "s:".
24419 * Return 0 otherwise.
24420 */
24421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024422eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024424 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24425 * the standard library function. */
24426 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24427 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 return 5;
24429 if (p[0] == 's' && p[1] == ':')
24430 return 2;
24431 return 0;
24432}
24433
24434/*
24435 * Return TRUE if "p" starts with "<SID>" or "s:".
24436 * Only works if eval_fname_script() returned non-zero for "p"!
24437 */
24438 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024439eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440{
24441 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24442}
24443
24444/*
24445 * List the head of the function: "name(arg1, arg2)".
24446 */
24447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024448list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449{
24450 int j;
24451
24452 msg_start();
24453 if (indent)
24454 MSG_PUTS(" ");
24455 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024456 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 {
24458 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024459 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 }
24461 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024462 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024464 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465 {
24466 if (j)
24467 MSG_PUTS(", ");
24468 msg_puts(FUNCARG(fp, j));
24469 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 {
24472 if (j)
24473 MSG_PUTS(", ");
24474 MSG_PUTS("...");
24475 }
24476 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024477 if (fp->uf_flags & FC_ABORT)
24478 MSG_PUTS(" abort");
24479 if (fp->uf_flags & FC_RANGE)
24480 MSG_PUTS(" range");
24481 if (fp->uf_flags & FC_DICT)
24482 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024483 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024484 if (p_verbose > 0)
24485 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486}
24487
24488/*
24489 * Find a function by name, return pointer to it in ufuncs.
24490 * Return NULL for unknown function.
24491 */
24492 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024493find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024495 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024497 hi = hash_find(&func_hashtab, name);
24498 if (!HASHITEM_EMPTY(hi))
24499 return HI2UF(hi);
24500 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501}
24502
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024503#if defined(EXITFREE) || defined(PROTO)
24504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024505free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024506{
24507 hashitem_T *hi;
24508
24509 /* Need to start all over every time, because func_free() may change the
24510 * hash table. */
24511 while (func_hashtab.ht_used > 0)
24512 for (hi = func_hashtab.ht_array; ; ++hi)
24513 if (!HASHITEM_EMPTY(hi))
24514 {
24515 func_free(HI2UF(hi));
24516 break;
24517 }
24518}
24519#endif
24520
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024522translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024523{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024524 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024525 return find_internal_func(name) >= 0;
24526 return find_func(name) != NULL;
24527}
24528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024529/*
24530 * Return TRUE if a function "name" exists.
24531 */
24532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024533function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024534{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024535 char_u *nm = name;
24536 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024537 int n = FALSE;
24538
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024539 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24540 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024541 nm = skipwhite(nm);
24542
24543 /* Only accept "funcname", "funcname ", "funcname (..." and
24544 * "funcname(...", not "funcname!...". */
24545 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024546 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024547 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024548 return n;
24549}
24550
Bram Moolenaara1544c02013-05-30 12:35:52 +020024551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024552get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024553{
24554 char_u *nm = name;
24555 char_u *p;
24556
24557 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24558
24559 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024560 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024561 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024562
Bram Moolenaara1544c02013-05-30 12:35:52 +020024563 vim_free(p);
24564 return NULL;
24565}
24566
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024567/*
24568 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024569 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24570 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024571 */
24572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024573builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024574{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024575 char_u *p;
24576
24577 if (!ASCII_ISLOWER(name[0]))
24578 return FALSE;
24579 p = vim_strchr(name, AUTOLOAD_CHAR);
24580 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024581}
24582
Bram Moolenaar05159a02005-02-26 23:04:13 +000024583#if defined(FEAT_PROFILE) || defined(PROTO)
24584/*
24585 * Start profiling function "fp".
24586 */
24587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024588func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024589{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024590 int len = fp->uf_lines.ga_len;
24591
24592 if (len == 0)
24593 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024594 fp->uf_tm_count = 0;
24595 profile_zero(&fp->uf_tm_self);
24596 profile_zero(&fp->uf_tm_total);
24597 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024598 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024599 if (fp->uf_tml_total == NULL)
24600 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024601 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024602 if (fp->uf_tml_self == NULL)
24603 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024604 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024605 fp->uf_tml_idx = -1;
24606 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24607 || fp->uf_tml_self == NULL)
24608 return; /* out of memory */
24609
24610 fp->uf_profiling = TRUE;
24611}
24612
24613/*
24614 * Dump the profiling results for all functions in file "fd".
24615 */
24616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024617func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024618{
24619 hashitem_T *hi;
24620 int todo;
24621 ufunc_T *fp;
24622 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024623 ufunc_T **sorttab;
24624 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024626 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024627 if (todo == 0)
24628 return; /* nothing to dump */
24629
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024630 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024631
Bram Moolenaar05159a02005-02-26 23:04:13 +000024632 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24633 {
24634 if (!HASHITEM_EMPTY(hi))
24635 {
24636 --todo;
24637 fp = HI2UF(hi);
24638 if (fp->uf_profiling)
24639 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024640 if (sorttab != NULL)
24641 sorttab[st_len++] = fp;
24642
Bram Moolenaar05159a02005-02-26 23:04:13 +000024643 if (fp->uf_name[0] == K_SPECIAL)
24644 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24645 else
24646 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24647 if (fp->uf_tm_count == 1)
24648 fprintf(fd, "Called 1 time\n");
24649 else
24650 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24651 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24652 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24653 fprintf(fd, "\n");
24654 fprintf(fd, "count total (s) self (s)\n");
24655
24656 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24657 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024658 if (FUNCLINE(fp, i) == NULL)
24659 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024660 prof_func_line(fd, fp->uf_tml_count[i],
24661 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024662 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24663 }
24664 fprintf(fd, "\n");
24665 }
24666 }
24667 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024668
24669 if (sorttab != NULL && st_len > 0)
24670 {
24671 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24672 prof_total_cmp);
24673 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24674 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24675 prof_self_cmp);
24676 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24677 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024678
24679 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024680}
Bram Moolenaar73830342005-02-28 22:48:19 +000024681
24682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024683prof_sort_list(
24684 FILE *fd,
24685 ufunc_T **sorttab,
24686 int st_len,
24687 char *title,
24688 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024689{
24690 int i;
24691 ufunc_T *fp;
24692
24693 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24694 fprintf(fd, "count total (s) self (s) function\n");
24695 for (i = 0; i < 20 && i < st_len; ++i)
24696 {
24697 fp = sorttab[i];
24698 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24699 prefer_self);
24700 if (fp->uf_name[0] == K_SPECIAL)
24701 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24702 else
24703 fprintf(fd, " %s()\n", fp->uf_name);
24704 }
24705 fprintf(fd, "\n");
24706}
24707
24708/*
24709 * Print the count and times for one function or function line.
24710 */
24711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024712prof_func_line(
24713 FILE *fd,
24714 int count,
24715 proftime_T *total,
24716 proftime_T *self,
24717 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024718{
24719 if (count > 0)
24720 {
24721 fprintf(fd, "%5d ", count);
24722 if (prefer_self && profile_equal(total, self))
24723 fprintf(fd, " ");
24724 else
24725 fprintf(fd, "%s ", profile_msg(total));
24726 if (!prefer_self && profile_equal(total, self))
24727 fprintf(fd, " ");
24728 else
24729 fprintf(fd, "%s ", profile_msg(self));
24730 }
24731 else
24732 fprintf(fd, " ");
24733}
24734
24735/*
24736 * Compare function for total time sorting.
24737 */
24738 static int
24739#ifdef __BORLANDC__
24740_RTLENTRYF
24741#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024742prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024743{
24744 ufunc_T *p1, *p2;
24745
24746 p1 = *(ufunc_T **)s1;
24747 p2 = *(ufunc_T **)s2;
24748 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24749}
24750
24751/*
24752 * Compare function for self time sorting.
24753 */
24754 static int
24755#ifdef __BORLANDC__
24756_RTLENTRYF
24757#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024758prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024759{
24760 ufunc_T *p1, *p2;
24761
24762 p1 = *(ufunc_T **)s1;
24763 p2 = *(ufunc_T **)s2;
24764 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24765}
24766
Bram Moolenaar05159a02005-02-26 23:04:13 +000024767#endif
24768
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024769/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024771 * Return TRUE if a package was loaded.
24772 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024774script_autoload(
24775 char_u *name,
24776 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024777{
24778 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024779 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024780 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024781 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024782
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024783 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024784 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024785 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024786 return FALSE;
24787
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024788 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024789
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024790 /* Find the name in the list of previously loaded package names. Skip
24791 * "autoload/", it's always the same. */
24792 for (i = 0; i < ga_loaded.ga_len; ++i)
24793 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24794 break;
24795 if (!reload && i < ga_loaded.ga_len)
24796 ret = FALSE; /* was loaded already */
24797 else
24798 {
24799 /* Remember the name if it wasn't loaded already. */
24800 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24801 {
24802 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24803 tofree = NULL;
24804 }
24805
24806 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024807 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024808 ret = TRUE;
24809 }
24810
24811 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024812 return ret;
24813}
24814
24815/*
24816 * Return the autoload script name for a function or variable name.
24817 * Returns NULL when out of memory.
24818 */
24819 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024820autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024821{
24822 char_u *p;
24823 char_u *scriptname;
24824
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024825 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024826 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24827 if (scriptname == NULL)
24828 return FALSE;
24829 STRCPY(scriptname, "autoload/");
24830 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024831 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024832 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024833 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024834 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024835 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024836}
24837
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24839
24840/*
24841 * Function given to ExpandGeneric() to obtain the list of user defined
24842 * function names.
24843 */
24844 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024847 static long_u done;
24848 static hashitem_T *hi;
24849 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850
24851 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024853 done = 0;
24854 hi = func_hashtab.ht_array;
24855 }
24856 if (done < func_hashtab.ht_used)
24857 {
24858 if (done++ > 0)
24859 ++hi;
24860 while (HASHITEM_EMPTY(hi))
24861 ++hi;
24862 fp = HI2UF(hi);
24863
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024864 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024865 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024866
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024867 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24868 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869
24870 cat_func_name(IObuff, fp);
24871 if (xp->xp_context != EXPAND_USER_FUNC)
24872 {
24873 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024874 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 STRCAT(IObuff, ")");
24876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 return IObuff;
24878 }
24879 return NULL;
24880}
24881
24882#endif /* FEAT_CMDL_COMPL */
24883
24884/*
24885 * Copy the function name of "fp" to buffer "buf".
24886 * "buf" must be able to hold the function name plus three bytes.
24887 * Takes care of script-local function names.
24888 */
24889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024890cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024892 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 {
24894 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024895 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 }
24897 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024898 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899}
24900
24901/*
24902 * ":delfunction {name}"
24903 */
24904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024905ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024907 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 char_u *p;
24909 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024910 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911
24912 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024913 name = trans_function_name(&p, eap->skip, 0, &fudi);
24914 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024916 {
24917 if (fudi.fd_dict != NULL && !eap->skip)
24918 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 if (!ends_excmd(*skipwhite(p)))
24922 {
24923 vim_free(name);
24924 EMSG(_(e_trailing));
24925 return;
24926 }
24927 eap->nextcmd = check_nextcmd(p);
24928 if (eap->nextcmd != NULL)
24929 *p = NUL;
24930
24931 if (!eap->skip)
24932 fp = find_func(name);
24933 vim_free(name);
24934
24935 if (!eap->skip)
24936 {
24937 if (fp == NULL)
24938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024939 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 return;
24941 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024942 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 {
24944 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24945 return;
24946 }
24947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024948 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024950 /* Delete the dict item that refers to the function, it will
24951 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024952 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024954 else
24955 func_free(fp);
24956 }
24957}
24958
24959/*
24960 * Free a function and remove it from the list of functions.
24961 */
24962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024963func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024964{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024965 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024966
24967 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024968 ga_clear_strings(&(fp->uf_args));
24969 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024970#ifdef FEAT_PROFILE
24971 vim_free(fp->uf_tml_count);
24972 vim_free(fp->uf_tml_total);
24973 vim_free(fp->uf_tml_self);
24974#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024975
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024976 /* remove the function from the function hashtable */
24977 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24978 if (HASHITEM_EMPTY(hi))
24979 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024980 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024981 hash_remove(&func_hashtab, hi);
24982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024983 vim_free(fp);
24984}
24985
24986/*
24987 * Unreference a Function: decrement the reference count and free it when it
24988 * becomes zero. Only for numbered functions.
24989 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024991func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024992{
24993 ufunc_T *fp;
24994
24995 if (name != NULL && isdigit(*name))
24996 {
24997 fp = find_func(name);
24998 if (fp == NULL)
24999 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025000 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025001 {
25002 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025003 * when "uf_calls" becomes zero. */
25004 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025005 func_free(fp);
25006 }
25007 }
25008}
25009
25010/*
25011 * Count a reference to a Function.
25012 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025014func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025015{
25016 ufunc_T *fp;
25017
25018 if (name != NULL && isdigit(*name))
25019 {
25020 fp = find_func(name);
25021 if (fp == NULL)
25022 EMSG2(_(e_intern2), "func_ref()");
25023 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025024 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 }
25026}
25027
25028/*
25029 * Call a user function.
25030 */
25031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025032call_user_func(
25033 ufunc_T *fp, /* pointer to function */
25034 int argcount, /* nr of args */
25035 typval_T *argvars, /* arguments */
25036 typval_T *rettv, /* return value */
25037 linenr_T firstline, /* first line of range */
25038 linenr_T lastline, /* last line of range */
25039 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040{
Bram Moolenaar33570922005-01-25 22:26:29 +000025041 char_u *save_sourcing_name;
25042 linenr_T save_sourcing_lnum;
25043 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025044 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025045 int save_did_emsg;
25046 static int depth = 0;
25047 dictitem_T *v;
25048 int fixvar_idx = 0; /* index in fixvar[] */
25049 int i;
25050 int ai;
25051 char_u numbuf[NUMBUFLEN];
25052 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025053 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025054#ifdef FEAT_PROFILE
25055 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025056 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058
25059 /* If depth of calling is getting too high, don't execute the function */
25060 if (depth >= p_mfd)
25061 {
25062 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025063 rettv->v_type = VAR_NUMBER;
25064 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 return;
25066 }
25067 ++depth;
25068
25069 line_breakcheck(); /* check for CTRL-C hit */
25070
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025071 fc = (funccall_T *)alloc(sizeof(funccall_T));
25072 fc->caller = current_funccal;
25073 current_funccal = fc;
25074 fc->func = fp;
25075 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025076 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025077 fc->linenr = 0;
25078 fc->returned = FALSE;
25079 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025081 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25082 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083
Bram Moolenaar33570922005-01-25 22:26:29 +000025084 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025085 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025086 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25087 * each argument variable and saves a lot of time.
25088 */
25089 /*
25090 * Init l: variables.
25091 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025092 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025093 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025094 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025095 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25096 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025097 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025098 name = v->di_key;
25099 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025100 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025101 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025102 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025103 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025104 v->di_tv.vval.v_dict = selfdict;
25105 ++selfdict->dv_refcount;
25106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025107
Bram Moolenaar33570922005-01-25 22:26:29 +000025108 /*
25109 * Init a: variables.
25110 * Set a:0 to "argcount".
25111 * Set a:000 to a list with room for the "..." arguments.
25112 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025113 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025114 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025115 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025116 /* Use "name" to avoid a warning from some compiler that checks the
25117 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025118 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025119 name = v->di_key;
25120 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025121 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025122 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025123 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025124 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025125 v->di_tv.vval.v_list = &fc->l_varlist;
25126 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25127 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25128 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025129
25130 /*
25131 * Set a:firstline to "firstline" and a:lastline to "lastline".
25132 * Set a:name to named arguments.
25133 * Set a:N to the "..." arguments.
25134 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025135 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025136 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025138 (varnumber_T)lastline);
25139 for (i = 0; i < argcount; ++i)
25140 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025141 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025142 if (ai < 0)
25143 /* named argument a:name */
25144 name = FUNCARG(fp, i);
25145 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025146 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025147 /* "..." argument a:1, a:2, etc. */
25148 sprintf((char *)numbuf, "%d", ai + 1);
25149 name = numbuf;
25150 }
25151 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25152 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025153 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025154 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25155 }
25156 else
25157 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025158 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25159 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025160 if (v == NULL)
25161 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025162 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025163 }
25164 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025165 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025166
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025167 /* Note: the values are copied directly to avoid alloc/free.
25168 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025169 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025170 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025171
25172 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25173 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025174 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25175 fc->l_listitems[ai].li_tv = argvars[i];
25176 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025177 }
25178 }
25179
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 /* Don't redraw while executing the function. */
25181 ++RedrawingDisabled;
25182 save_sourcing_name = sourcing_name;
25183 save_sourcing_lnum = sourcing_lnum;
25184 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025185 /* need space for function name + ("function " + 3) or "[number]" */
25186 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25187 + STRLEN(fp->uf_name) + 20;
25188 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 if (sourcing_name != NULL)
25190 {
25191 if (save_sourcing_name != NULL
25192 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025193 sprintf((char *)sourcing_name, "%s[%d]..",
25194 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 else
25196 STRCPY(sourcing_name, "function ");
25197 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25198
25199 if (p_verbose >= 12)
25200 {
25201 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025202 verbose_enter_scroll();
25203
Bram Moolenaar555b2802005-05-19 21:08:39 +000025204 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205 if (p_verbose >= 14)
25206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025208 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025209 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025210 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211
25212 msg_puts((char_u *)"(");
25213 for (i = 0; i < argcount; ++i)
25214 {
25215 if (i > 0)
25216 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025217 if (argvars[i].v_type == VAR_NUMBER)
25218 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 else
25220 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025221 /* Do not want errors such as E724 here. */
25222 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025223 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025224 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025225 if (s != NULL)
25226 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025227 if (vim_strsize(s) > MSG_BUF_CLEN)
25228 {
25229 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25230 s = buf;
25231 }
25232 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025233 vim_free(tofree);
25234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 }
25236 }
25237 msg_puts((char_u *)")");
25238 }
25239 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025240
25241 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242 --no_wait_return;
25243 }
25244 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025245#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025246 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247 {
25248 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25249 func_do_profile(fp);
25250 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025251 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025252 {
25253 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025254 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025255 profile_zero(&fp->uf_tm_children);
25256 }
25257 script_prof_save(&wait_start);
25258 }
25259#endif
25260
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025262 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 save_did_emsg = did_emsg;
25264 did_emsg = FALSE;
25265
25266 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025267 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25269
25270 --RedrawingDisabled;
25271
25272 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025273 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025275 clear_tv(rettv);
25276 rettv->v_type = VAR_NUMBER;
25277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 }
25279
Bram Moolenaar05159a02005-02-26 23:04:13 +000025280#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025281 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025282 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025283 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025284 profile_end(&call_start);
25285 profile_sub_wait(&wait_start, &call_start);
25286 profile_add(&fp->uf_tm_total, &call_start);
25287 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025288 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025289 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025290 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25291 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025292 }
25293 }
25294#endif
25295
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 /* when being verbose, mention the return value */
25297 if (p_verbose >= 12)
25298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025300 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025303 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025304 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025305 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025306 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025309 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025310 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025311 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025312 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025313
Bram Moolenaar555b2802005-05-19 21:08:39 +000025314 /* The value may be very long. Skip the middle part, so that we
25315 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025316 * truncate it at the end. Don't want errors such as E724 here. */
25317 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025318 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025319 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025320 if (s != NULL)
25321 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025322 if (vim_strsize(s) > MSG_BUF_CLEN)
25323 {
25324 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25325 s = buf;
25326 }
25327 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025328 vim_free(tofree);
25329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 }
25331 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025332
25333 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 --no_wait_return;
25335 }
25336
25337 vim_free(sourcing_name);
25338 sourcing_name = save_sourcing_name;
25339 sourcing_lnum = save_sourcing_lnum;
25340 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025341#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025342 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025343 script_prof_restore(&wait_start);
25344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345
25346 if (p_verbose >= 12 && sourcing_name != NULL)
25347 {
25348 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025349 verbose_enter_scroll();
25350
Bram Moolenaar555b2802005-05-19 21:08:39 +000025351 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025353
25354 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 --no_wait_return;
25356 }
25357
25358 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025359 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025361
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025362 /* If the a:000 list and the l: and a: dicts are not referenced we can
25363 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025364 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25365 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25366 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25367 {
25368 free_funccal(fc, FALSE);
25369 }
25370 else
25371 {
25372 hashitem_T *hi;
25373 listitem_T *li;
25374 int todo;
25375
25376 /* "fc" is still in use. This can happen when returning "a:000" or
25377 * assigning "l:" to a global variable.
25378 * Link "fc" in the list for garbage collection later. */
25379 fc->caller = previous_funccal;
25380 previous_funccal = fc;
25381
25382 /* Make a copy of the a: variables, since we didn't do that above. */
25383 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25384 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25385 {
25386 if (!HASHITEM_EMPTY(hi))
25387 {
25388 --todo;
25389 v = HI2DI(hi);
25390 copy_tv(&v->di_tv, &v->di_tv);
25391 }
25392 }
25393
25394 /* Make a copy of the a:000 items, since we didn't do that above. */
25395 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25396 copy_tv(&li->li_tv, &li->li_tv);
25397 }
25398}
25399
25400/*
25401 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025402 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025403 */
25404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025405can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025406{
25407 return (fc->l_varlist.lv_copyID != copyID
25408 && fc->l_vars.dv_copyID != copyID
25409 && fc->l_avars.dv_copyID != copyID);
25410}
25411
25412/*
25413 * Free "fc" and what it contains.
25414 */
25415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025416free_funccal(
25417 funccall_T *fc,
25418 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025419{
25420 listitem_T *li;
25421
25422 /* The a: variables typevals may not have been allocated, only free the
25423 * allocated variables. */
25424 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25425
25426 /* free all l: variables */
25427 vars_clear(&fc->l_vars.dv_hashtab);
25428
25429 /* Free the a:000 variables if they were allocated. */
25430 if (free_val)
25431 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25432 clear_tv(&li->li_tv);
25433
25434 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435}
25436
25437/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025438 * Add a number variable "name" to dict "dp" with value "nr".
25439 */
25440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025441add_nr_var(
25442 dict_T *dp,
25443 dictitem_T *v,
25444 char *name,
25445 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025446{
25447 STRCPY(v->di_key, name);
25448 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25449 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25450 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025451 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025452 v->di_tv.vval.v_number = nr;
25453}
25454
25455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 * ":return [expr]"
25457 */
25458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025459ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460{
25461 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 int returning = FALSE;
25464
25465 if (current_funccal == NULL)
25466 {
25467 EMSG(_("E133: :return not inside a function"));
25468 return;
25469 }
25470
25471 if (eap->skip)
25472 ++emsg_skip;
25473
25474 eap->nextcmd = NULL;
25475 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025476 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 {
25478 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025479 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025481 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483 /* It's safer to return also on error. */
25484 else if (!eap->skip)
25485 {
25486 /*
25487 * Return unless the expression evaluation has been cancelled due to an
25488 * aborting error, an interrupt, or an exception.
25489 */
25490 if (!aborting())
25491 returning = do_return(eap, FALSE, TRUE, NULL);
25492 }
25493
25494 /* When skipping or the return gets pending, advance to the next command
25495 * in this line (!returning). Otherwise, ignore the rest of the line.
25496 * Following lines will be ignored by get_func_line(). */
25497 if (returning)
25498 eap->nextcmd = NULL;
25499 else if (eap->nextcmd == NULL) /* no argument */
25500 eap->nextcmd = check_nextcmd(arg);
25501
25502 if (eap->skip)
25503 --emsg_skip;
25504}
25505
25506/*
25507 * Return from a function. Possibly makes the return pending. Also called
25508 * for a pending return at the ":endtry" or after returning from an extra
25509 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025510 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025511 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 * FALSE when the return gets pending.
25513 */
25514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025515do_return(
25516 exarg_T *eap,
25517 int reanimate,
25518 int is_cmd,
25519 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520{
25521 int idx;
25522 struct condstack *cstack = eap->cstack;
25523
25524 if (reanimate)
25525 /* Undo the return. */
25526 current_funccal->returned = FALSE;
25527
25528 /*
25529 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25530 * not in its finally clause (which then is to be executed next) is found.
25531 * In this case, make the ":return" pending for execution at the ":endtry".
25532 * Otherwise, return normally.
25533 */
25534 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25535 if (idx >= 0)
25536 {
25537 cstack->cs_pending[idx] = CSTP_RETURN;
25538
25539 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025540 /* A pending return again gets pending. "rettv" points to an
25541 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025543 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 else
25545 {
25546 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025547 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025549 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025551 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 {
25553 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025554 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025555 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 else
25557 EMSG(_(e_outofmem));
25558 }
25559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025560 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561
25562 if (reanimate)
25563 {
25564 /* The pending return value could be overwritten by a ":return"
25565 * without argument in a finally clause; reset the default
25566 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025567 current_funccal->rettv->v_type = VAR_NUMBER;
25568 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 }
25570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025571 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572 }
25573 else
25574 {
25575 current_funccal->returned = TRUE;
25576
25577 /* If the return is carried out now, store the return value. For
25578 * a return immediately after reanimation, the value is already
25579 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025580 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025582 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025583 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025585 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 }
25587 }
25588
25589 return idx < 0;
25590}
25591
25592/*
25593 * Free the variable with a pending return value.
25594 */
25595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025596discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597{
Bram Moolenaar33570922005-01-25 22:26:29 +000025598 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599}
25600
25601/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025602 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603 * is an allocated string. Used by report_pending() for verbose messages.
25604 */
25605 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025606get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025608 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025609 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025610 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025612 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025613 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025614 if (s == NULL)
25615 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025616
25617 STRCPY(IObuff, ":return ");
25618 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25619 if (STRLEN(s) + 8 >= IOSIZE)
25620 STRCPY(IObuff + IOSIZE - 4, "...");
25621 vim_free(tofree);
25622 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623}
25624
25625/*
25626 * Get next function line.
25627 * Called by do_cmdline() to get the next line.
25628 * Returns allocated string, or NULL for end of function.
25629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025631get_func_line(
25632 int c UNUSED,
25633 void *cookie,
25634 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635{
Bram Moolenaar33570922005-01-25 22:26:29 +000025636 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025637 ufunc_T *fp = fcp->func;
25638 char_u *retval;
25639 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640
25641 /* If breakpoints have been added/deleted need to check for it. */
25642 if (fcp->dbg_tick != debug_tick)
25643 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025644 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 sourcing_lnum);
25646 fcp->dbg_tick = debug_tick;
25647 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025648#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025649 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025650 func_line_end(cookie);
25651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652
Bram Moolenaar05159a02005-02-26 23:04:13 +000025653 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025654 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25655 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 retval = NULL;
25657 else
25658 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025659 /* Skip NULL lines (continuation lines). */
25660 while (fcp->linenr < gap->ga_len
25661 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25662 ++fcp->linenr;
25663 if (fcp->linenr >= gap->ga_len)
25664 retval = NULL;
25665 else
25666 {
25667 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25668 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025669#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025670 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025671 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025672#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 }
25675
25676 /* Did we encounter a breakpoint? */
25677 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25678 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025679 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025680 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025681 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 sourcing_lnum);
25683 fcp->dbg_tick = debug_tick;
25684 }
25685
25686 return retval;
25687}
25688
Bram Moolenaar05159a02005-02-26 23:04:13 +000025689#if defined(FEAT_PROFILE) || defined(PROTO)
25690/*
25691 * Called when starting to read a function line.
25692 * "sourcing_lnum" must be correct!
25693 * When skipping lines it may not actually be executed, but we won't find out
25694 * until later and we need to store the time now.
25695 */
25696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025697func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025698{
25699 funccall_T *fcp = (funccall_T *)cookie;
25700 ufunc_T *fp = fcp->func;
25701
25702 if (fp->uf_profiling && sourcing_lnum >= 1
25703 && sourcing_lnum <= fp->uf_lines.ga_len)
25704 {
25705 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025706 /* Skip continuation lines. */
25707 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25708 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025709 fp->uf_tml_execed = FALSE;
25710 profile_start(&fp->uf_tml_start);
25711 profile_zero(&fp->uf_tml_children);
25712 profile_get_wait(&fp->uf_tml_wait);
25713 }
25714}
25715
25716/*
25717 * Called when actually executing a function line.
25718 */
25719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025720func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025721{
25722 funccall_T *fcp = (funccall_T *)cookie;
25723 ufunc_T *fp = fcp->func;
25724
25725 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25726 fp->uf_tml_execed = TRUE;
25727}
25728
25729/*
25730 * Called when done with a function line.
25731 */
25732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025733func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025734{
25735 funccall_T *fcp = (funccall_T *)cookie;
25736 ufunc_T *fp = fcp->func;
25737
25738 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25739 {
25740 if (fp->uf_tml_execed)
25741 {
25742 ++fp->uf_tml_count[fp->uf_tml_idx];
25743 profile_end(&fp->uf_tml_start);
25744 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025745 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025746 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25747 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025748 }
25749 fp->uf_tml_idx = -1;
25750 }
25751}
25752#endif
25753
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754/*
25755 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025756 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757 */
25758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025759func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760{
Bram Moolenaar33570922005-01-25 22:26:29 +000025761 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762
25763 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25764 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025765 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766 || fcp->returned);
25767}
25768
25769/*
25770 * return TRUE if cookie indicates a function which "abort"s on errors.
25771 */
25772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773func_has_abort(
25774 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025776 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777}
25778
25779#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25780typedef enum
25781{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025782 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25783 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25784 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025785} var_flavour_T;
25786
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025787static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788
25789 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025790var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791{
25792 char_u *p = varname;
25793
25794 if (ASCII_ISUPPER(*p))
25795 {
25796 while (*(++p))
25797 if (ASCII_ISLOWER(*p))
25798 return VAR_FLAVOUR_SESSION;
25799 return VAR_FLAVOUR_VIMINFO;
25800 }
25801 else
25802 return VAR_FLAVOUR_DEFAULT;
25803}
25804#endif
25805
25806#if defined(FEAT_VIMINFO) || defined(PROTO)
25807/*
25808 * Restore global vars that start with a capital from the viminfo file
25809 */
25810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025811read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812{
25813 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025814 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025815 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025816 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817
25818 if (!writing && (find_viminfo_parameter('!') != NULL))
25819 {
25820 tab = vim_strchr(virp->vir_line + 1, '\t');
25821 if (tab != NULL)
25822 {
25823 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025824 switch (*tab)
25825 {
25826 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025827#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025828 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025829#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025830 case 'D': type = VAR_DICT; break;
25831 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025832 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834
25835 tab = vim_strchr(tab, '\t');
25836 if (tab != NULL)
25837 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025838 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025839 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025840 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025842#ifdef FEAT_FLOAT
25843 else if (type == VAR_FLOAT)
25844 (void)string2float(tab + 1, &tv.vval.v_float);
25845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025847 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025848 if (type == VAR_DICT || type == VAR_LIST)
25849 {
25850 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25851
25852 if (etv == NULL)
25853 /* Failed to parse back the dict or list, use it as a
25854 * string. */
25855 tv.v_type = VAR_STRING;
25856 else
25857 {
25858 vim_free(tv.vval.v_string);
25859 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025860 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025861 }
25862 }
25863
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025864 /* when in a function use global variables */
25865 save_funccal = current_funccal;
25866 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025867 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025868 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025869
25870 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025871 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025872 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25873 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 }
25875 }
25876 }
25877
25878 return viminfo_readline(virp);
25879}
25880
25881/*
25882 * Write global vars that start with a capital to the viminfo file
25883 */
25884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025885write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886{
Bram Moolenaar33570922005-01-25 22:26:29 +000025887 hashitem_T *hi;
25888 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025889 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025890 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025891 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025892 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025893 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025894
25895 if (find_viminfo_parameter('!') == NULL)
25896 return;
25897
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025898 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025899
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025900 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025901 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025903 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025905 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025906 this_var = HI2DI(hi);
25907 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025908 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025909 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025910 {
25911 case VAR_STRING: s = "STR"; break;
25912 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025913 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025914 case VAR_DICT: s = "DIC"; break;
25915 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025916 case VAR_SPECIAL: s = "XPL"; break;
25917
25918 case VAR_UNKNOWN:
25919 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025920 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025921 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025922 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025923 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025924 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025925 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025926 if (p != NULL)
25927 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025928 vim_free(tofree);
25929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 }
25931 }
25932}
25933#endif
25934
25935#if defined(FEAT_SESSION) || defined(PROTO)
25936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025937store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938{
Bram Moolenaar33570922005-01-25 22:26:29 +000025939 hashitem_T *hi;
25940 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025941 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 char_u *p, *t;
25943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025944 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025945 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025949 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025950 this_var = HI2DI(hi);
25951 if ((this_var->di_tv.v_type == VAR_NUMBER
25952 || this_var->di_tv.v_type == VAR_STRING)
25953 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025954 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025955 /* Escape special characters with a backslash. Turn a LF and
25956 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025957 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025958 (char_u *)"\\\"\n\r");
25959 if (p == NULL) /* out of memory */
25960 break;
25961 for (t = p; *t != NUL; ++t)
25962 if (*t == '\n')
25963 *t = 'n';
25964 else if (*t == '\r')
25965 *t = 'r';
25966 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025967 this_var->di_key,
25968 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25969 : ' ',
25970 p,
25971 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25972 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025973 || put_eol(fd) == FAIL)
25974 {
25975 vim_free(p);
25976 return FAIL;
25977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978 vim_free(p);
25979 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025980#ifdef FEAT_FLOAT
25981 else if (this_var->di_tv.v_type == VAR_FLOAT
25982 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25983 {
25984 float_T f = this_var->di_tv.vval.v_float;
25985 int sign = ' ';
25986
25987 if (f < 0)
25988 {
25989 f = -f;
25990 sign = '-';
25991 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025992 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025993 this_var->di_key, sign, f) < 0)
25994 || put_eol(fd) == FAIL)
25995 return FAIL;
25996 }
25997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 }
25999 }
26000 return OK;
26001}
26002#endif
26003
Bram Moolenaar661b1822005-07-28 22:36:45 +000026004/*
26005 * Display script name where an item was last set.
26006 * Should only be invoked when 'verbose' is non-zero.
26007 */
26008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026009last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026010{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026011 char_u *p;
26012
Bram Moolenaar661b1822005-07-28 22:36:45 +000026013 if (scriptID != 0)
26014 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026015 p = home_replace_save(NULL, get_scriptname(scriptID));
26016 if (p != NULL)
26017 {
26018 verbose_enter();
26019 MSG_PUTS(_("\n\tLast set from "));
26020 MSG_PUTS(p);
26021 vim_free(p);
26022 verbose_leave();
26023 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026024 }
26025}
26026
Bram Moolenaard812df62008-11-09 12:46:09 +000026027/*
26028 * List v:oldfiles in a nice way.
26029 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026031ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026032{
26033 list_T *l = vimvars[VV_OLDFILES].vv_list;
26034 listitem_T *li;
26035 int nr = 0;
26036
26037 if (l == NULL)
26038 msg((char_u *)_("No old files"));
26039 else
26040 {
26041 msg_start();
26042 msg_scroll = TRUE;
26043 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26044 {
26045 msg_outnum((long)++nr);
26046 MSG_PUTS(": ");
26047 msg_outtrans(get_tv_string(&li->li_tv));
26048 msg_putchar('\n');
26049 out_flush(); /* output one line at a time */
26050 ui_breakcheck();
26051 }
26052 /* Assume "got_int" was set to truncate the listing. */
26053 got_int = FALSE;
26054
26055#ifdef FEAT_BROWSE_CMD
26056 if (cmdmod.browse)
26057 {
26058 quit_more = FALSE;
26059 nr = prompt_for_number(FALSE);
26060 msg_starthere();
26061 if (nr > 0)
26062 {
26063 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26064 (long)nr);
26065
26066 if (p != NULL)
26067 {
26068 p = expand_env_save(p);
26069 eap->arg = p;
26070 eap->cmdidx = CMD_edit;
26071 cmdmod.browse = FALSE;
26072 do_exedit(eap, NULL);
26073 vim_free(p);
26074 }
26075 }
26076 }
26077#endif
26078 }
26079}
26080
Bram Moolenaar53744302015-07-17 17:38:22 +020026081/* reset v:option_new, v:option_old and v:option_type */
26082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026083reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026084{
26085 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26086 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26087 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26088}
26089
26090
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091#endif /* FEAT_EVAL */
26092
Bram Moolenaar071d4272004-06-13 20:20:40 +000026093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026094#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095
26096#ifdef WIN3264
26097/*
26098 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26099 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026100static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26101static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26102static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103
26104/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026105 * Get the short path (8.3) for the filename in "fnamep".
26106 * Only works for a valid file name.
26107 * When the path gets longer "fnamep" is changed and the allocated buffer
26108 * is put in "bufp".
26109 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26110 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 */
26112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026113get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026115 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116 char_u *newbuf;
26117
26118 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026119 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120 if (l > len - 1)
26121 {
26122 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026123 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 newbuf = vim_strnsave(*fnamep, l);
26125 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026126 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127
26128 vim_free(*bufp);
26129 *fnamep = *bufp = newbuf;
26130
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026131 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026132 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 }
26134
26135 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026136 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137}
26138
26139/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026140 * Get the short path (8.3) for the filename in "fname". The converted
26141 * path is returned in "bufp".
26142 *
26143 * Some of the directories specified in "fname" may not exist. This function
26144 * will shorten the existing directories at the beginning of the path and then
26145 * append the remaining non-existing path.
26146 *
26147 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026148 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026149 * bufp - Pointer to an allocated buffer for the filename.
26150 * fnamelen - Length of the filename pointed to by fname
26151 *
26152 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026153 */
26154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026155shortpath_for_invalid_fname(
26156 char_u **fname,
26157 char_u **bufp,
26158 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026159{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026160 char_u *short_fname, *save_fname, *pbuf_unused;
26161 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026162 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026163 int old_len, len;
26164 int new_len, sfx_len;
26165 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166
26167 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026168 old_len = *fnamelen;
26169 save_fname = vim_strnsave(*fname, old_len);
26170 pbuf_unused = NULL;
26171 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026172
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026173 endp = save_fname + old_len - 1; /* Find the end of the copy */
26174 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026176 /*
26177 * Try shortening the supplied path till it succeeds by removing one
26178 * directory at a time from the tail of the path.
26179 */
26180 len = 0;
26181 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026183 /* go back one path-separator */
26184 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26185 --endp;
26186 if (endp <= save_fname)
26187 break; /* processed the complete path */
26188
26189 /*
26190 * Replace the path separator with a NUL and try to shorten the
26191 * resulting path.
26192 */
26193 ch = *endp;
26194 *endp = 0;
26195 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026196 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026197 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26198 {
26199 retval = FAIL;
26200 goto theend;
26201 }
26202 *endp = ch; /* preserve the string */
26203
26204 if (len > 0)
26205 break; /* successfully shortened the path */
26206
26207 /* failed to shorten the path. Skip the path separator */
26208 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209 }
26210
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026211 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026213 /*
26214 * Succeeded in shortening the path. Now concatenate the shortened
26215 * path with the remaining path at the tail.
26216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026218 /* Compute the length of the new path. */
26219 sfx_len = (int)(save_endp - endp) + 1;
26220 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026222 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026223 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026224 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026226 /* There is not enough space in the currently allocated string,
26227 * copy it to a buffer big enough. */
26228 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026229 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026230 {
26231 retval = FAIL;
26232 goto theend;
26233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234 }
26235 else
26236 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026237 /* Transfer short_fname to the main buffer (it's big enough),
26238 * unless get_short_pathname() did its work in-place. */
26239 *fname = *bufp = save_fname;
26240 if (short_fname != save_fname)
26241 vim_strncpy(save_fname, short_fname, len);
26242 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026244
26245 /* concat the not-shortened part of the path */
26246 vim_strncpy(*fname + len, endp, sfx_len);
26247 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026249
26250theend:
26251 vim_free(pbuf_unused);
26252 vim_free(save_fname);
26253
26254 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255}
26256
26257/*
26258 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026259 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026260 */
26261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026262shortpath_for_partial(
26263 char_u **fnamep,
26264 char_u **bufp,
26265 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026266{
26267 int sepcount, len, tflen;
26268 char_u *p;
26269 char_u *pbuf, *tfname;
26270 int hasTilde;
26271
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026272 /* Count up the path separators from the RHS.. so we know which part
26273 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026275 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276 if (vim_ispathsep(*p))
26277 ++sepcount;
26278
26279 /* Need full path first (use expand_env() to remove a "~/") */
26280 hasTilde = (**fnamep == '~');
26281 if (hasTilde)
26282 pbuf = tfname = expand_env_save(*fnamep);
26283 else
26284 pbuf = tfname = FullName_save(*fnamep, FALSE);
26285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026286 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026288 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026290
26291 if (len == 0)
26292 {
26293 /* Don't have a valid filename, so shorten the rest of the
26294 * path if we can. This CAN give us invalid 8.3 filenames, but
26295 * there's not a lot of point in guessing what it might be.
26296 */
26297 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026298 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 }
26301
26302 /* Count the paths backward to find the beginning of the desired string. */
26303 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026304 {
26305#ifdef FEAT_MBYTE
26306 if (has_mbyte)
26307 p -= mb_head_off(tfname, p);
26308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026309 if (vim_ispathsep(*p))
26310 {
26311 if (sepcount == 0 || (hasTilde && sepcount == 1))
26312 break;
26313 else
26314 sepcount --;
26315 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 if (hasTilde)
26318 {
26319 --p;
26320 if (p >= tfname)
26321 *p = '~';
26322 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 }
26325 else
26326 ++p;
26327
26328 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26329 vim_free(*bufp);
26330 *fnamelen = (int)STRLEN(p);
26331 *bufp = pbuf;
26332 *fnamep = p;
26333
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026334 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335}
26336#endif /* WIN3264 */
26337
26338/*
26339 * Adjust a filename, according to a string of modifiers.
26340 * *fnamep must be NUL terminated when called. When returning, the length is
26341 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026342 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343 * When there is an error, *fnamep is set to NULL.
26344 */
26345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026346modify_fname(
26347 char_u *src, /* string with modifiers */
26348 int *usedlen, /* characters after src that are used */
26349 char_u **fnamep, /* file name so far */
26350 char_u **bufp, /* buffer for allocated file name or NULL */
26351 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352{
26353 int valid = 0;
26354 char_u *tail;
26355 char_u *s, *p, *pbuf;
26356 char_u dirname[MAXPATHL];
26357 int c;
26358 int has_fullname = 0;
26359#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026360 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361 int has_shortname = 0;
26362#endif
26363
26364repeat:
26365 /* ":p" - full path/file_name */
26366 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26367 {
26368 has_fullname = 1;
26369
26370 valid |= VALID_PATH;
26371 *usedlen += 2;
26372
26373 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26374 if ((*fnamep)[0] == '~'
26375#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26376 && ((*fnamep)[1] == '/'
26377# ifdef BACKSLASH_IN_FILENAME
26378 || (*fnamep)[1] == '\\'
26379# endif
26380 || (*fnamep)[1] == NUL)
26381
26382#endif
26383 )
26384 {
26385 *fnamep = expand_env_save(*fnamep);
26386 vim_free(*bufp); /* free any allocated file name */
26387 *bufp = *fnamep;
26388 if (*fnamep == NULL)
26389 return -1;
26390 }
26391
26392 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026393 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394 {
26395 if (vim_ispathsep(*p)
26396 && p[1] == '.'
26397 && (p[2] == NUL
26398 || vim_ispathsep(p[2])
26399 || (p[2] == '.'
26400 && (p[3] == NUL || vim_ispathsep(p[3])))))
26401 break;
26402 }
26403
26404 /* FullName_save() is slow, don't use it when not needed. */
26405 if (*p != NUL || !vim_isAbsName(*fnamep))
26406 {
26407 *fnamep = FullName_save(*fnamep, *p != NUL);
26408 vim_free(*bufp); /* free any allocated file name */
26409 *bufp = *fnamep;
26410 if (*fnamep == NULL)
26411 return -1;
26412 }
26413
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026414#ifdef WIN3264
26415# if _WIN32_WINNT >= 0x0500
26416 if (vim_strchr(*fnamep, '~') != NULL)
26417 {
26418 /* Expand 8.3 filename to full path. Needed to make sure the same
26419 * file does not have two different names.
26420 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26421 p = alloc(_MAX_PATH + 1);
26422 if (p != NULL)
26423 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026424 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026425 {
26426 vim_free(*bufp);
26427 *bufp = *fnamep = p;
26428 }
26429 else
26430 vim_free(p);
26431 }
26432 }
26433# endif
26434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 /* Append a path separator to a directory. */
26436 if (mch_isdir(*fnamep))
26437 {
26438 /* Make room for one or two extra characters. */
26439 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26440 vim_free(*bufp); /* free any allocated file name */
26441 *bufp = *fnamep;
26442 if (*fnamep == NULL)
26443 return -1;
26444 add_pathsep(*fnamep);
26445 }
26446 }
26447
26448 /* ":." - path relative to the current directory */
26449 /* ":~" - path relative to the home directory */
26450 /* ":8" - shortname path - postponed till after */
26451 while (src[*usedlen] == ':'
26452 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26453 {
26454 *usedlen += 2;
26455 if (c == '8')
26456 {
26457#ifdef WIN3264
26458 has_shortname = 1; /* Postpone this. */
26459#endif
26460 continue;
26461 }
26462 pbuf = NULL;
26463 /* Need full path first (use expand_env() to remove a "~/") */
26464 if (!has_fullname)
26465 {
26466 if (c == '.' && **fnamep == '~')
26467 p = pbuf = expand_env_save(*fnamep);
26468 else
26469 p = pbuf = FullName_save(*fnamep, FALSE);
26470 }
26471 else
26472 p = *fnamep;
26473
26474 has_fullname = 0;
26475
26476 if (p != NULL)
26477 {
26478 if (c == '.')
26479 {
26480 mch_dirname(dirname, MAXPATHL);
26481 s = shorten_fname(p, dirname);
26482 if (s != NULL)
26483 {
26484 *fnamep = s;
26485 if (pbuf != NULL)
26486 {
26487 vim_free(*bufp); /* free any allocated file name */
26488 *bufp = pbuf;
26489 pbuf = NULL;
26490 }
26491 }
26492 }
26493 else
26494 {
26495 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26496 /* Only replace it when it starts with '~' */
26497 if (*dirname == '~')
26498 {
26499 s = vim_strsave(dirname);
26500 if (s != NULL)
26501 {
26502 *fnamep = s;
26503 vim_free(*bufp);
26504 *bufp = s;
26505 }
26506 }
26507 }
26508 vim_free(pbuf);
26509 }
26510 }
26511
26512 tail = gettail(*fnamep);
26513 *fnamelen = (int)STRLEN(*fnamep);
26514
26515 /* ":h" - head, remove "/file_name", can be repeated */
26516 /* Don't remove the first "/" or "c:\" */
26517 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26518 {
26519 valid |= VALID_HEAD;
26520 *usedlen += 2;
26521 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026522 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026523 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524 *fnamelen = (int)(tail - *fnamep);
26525#ifdef VMS
26526 if (*fnamelen > 0)
26527 *fnamelen += 1; /* the path separator is part of the path */
26528#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026529 if (*fnamelen == 0)
26530 {
26531 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26532 p = vim_strsave((char_u *)".");
26533 if (p == NULL)
26534 return -1;
26535 vim_free(*bufp);
26536 *bufp = *fnamep = tail = p;
26537 *fnamelen = 1;
26538 }
26539 else
26540 {
26541 while (tail > s && !after_pathsep(s, tail))
26542 mb_ptr_back(*fnamep, tail);
26543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544 }
26545
26546 /* ":8" - shortname */
26547 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26548 {
26549 *usedlen += 2;
26550#ifdef WIN3264
26551 has_shortname = 1;
26552#endif
26553 }
26554
26555#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026556 /*
26557 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558 */
26559 if (has_shortname)
26560 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026561 /* Copy the string if it is shortened by :h and when it wasn't copied
26562 * yet, because we are going to change it in place. Avoids changing
26563 * the buffer name for "%:8". */
26564 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026565 {
26566 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026567 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026568 return -1;
26569 vim_free(*bufp);
26570 *bufp = *fnamep = p;
26571 }
26572
26573 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026574 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 if (!has_fullname && !vim_isAbsName(*fnamep))
26576 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026577 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026578 return -1;
26579 }
26580 else
26581 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026582 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583
Bram Moolenaardc935552011-08-17 15:23:23 +020026584 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026586 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587 return -1;
26588
26589 if (l == 0)
26590 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026591 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026593 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026594 return -1;
26595 }
26596 *fnamelen = l;
26597 }
26598 }
26599#endif /* WIN3264 */
26600
26601 /* ":t" - tail, just the basename */
26602 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26603 {
26604 *usedlen += 2;
26605 *fnamelen -= (int)(tail - *fnamep);
26606 *fnamep = tail;
26607 }
26608
26609 /* ":e" - extension, can be repeated */
26610 /* ":r" - root, without extension, can be repeated */
26611 while (src[*usedlen] == ':'
26612 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26613 {
26614 /* find a '.' in the tail:
26615 * - for second :e: before the current fname
26616 * - otherwise: The last '.'
26617 */
26618 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26619 s = *fnamep - 2;
26620 else
26621 s = *fnamep + *fnamelen - 1;
26622 for ( ; s > tail; --s)
26623 if (s[0] == '.')
26624 break;
26625 if (src[*usedlen + 1] == 'e') /* :e */
26626 {
26627 if (s > tail)
26628 {
26629 *fnamelen += (int)(*fnamep - (s + 1));
26630 *fnamep = s + 1;
26631#ifdef VMS
26632 /* cut version from the extension */
26633 s = *fnamep + *fnamelen - 1;
26634 for ( ; s > *fnamep; --s)
26635 if (s[0] == ';')
26636 break;
26637 if (s > *fnamep)
26638 *fnamelen = s - *fnamep;
26639#endif
26640 }
26641 else if (*fnamep <= tail)
26642 *fnamelen = 0;
26643 }
26644 else /* :r */
26645 {
26646 if (s > tail) /* remove one extension */
26647 *fnamelen = (int)(s - *fnamep);
26648 }
26649 *usedlen += 2;
26650 }
26651
26652 /* ":s?pat?foo?" - substitute */
26653 /* ":gs?pat?foo?" - global substitute */
26654 if (src[*usedlen] == ':'
26655 && (src[*usedlen + 1] == 's'
26656 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26657 {
26658 char_u *str;
26659 char_u *pat;
26660 char_u *sub;
26661 int sep;
26662 char_u *flags;
26663 int didit = FALSE;
26664
26665 flags = (char_u *)"";
26666 s = src + *usedlen + 2;
26667 if (src[*usedlen + 1] == 'g')
26668 {
26669 flags = (char_u *)"g";
26670 ++s;
26671 }
26672
26673 sep = *s++;
26674 if (sep)
26675 {
26676 /* find end of pattern */
26677 p = vim_strchr(s, sep);
26678 if (p != NULL)
26679 {
26680 pat = vim_strnsave(s, (int)(p - s));
26681 if (pat != NULL)
26682 {
26683 s = p + 1;
26684 /* find end of substitution */
26685 p = vim_strchr(s, sep);
26686 if (p != NULL)
26687 {
26688 sub = vim_strnsave(s, (int)(p - s));
26689 str = vim_strnsave(*fnamep, *fnamelen);
26690 if (sub != NULL && str != NULL)
26691 {
26692 *usedlen = (int)(p + 1 - src);
26693 s = do_string_sub(str, pat, sub, flags);
26694 if (s != NULL)
26695 {
26696 *fnamep = s;
26697 *fnamelen = (int)STRLEN(s);
26698 vim_free(*bufp);
26699 *bufp = s;
26700 didit = TRUE;
26701 }
26702 }
26703 vim_free(sub);
26704 vim_free(str);
26705 }
26706 vim_free(pat);
26707 }
26708 }
26709 /* after using ":s", repeat all the modifiers */
26710 if (didit)
26711 goto repeat;
26712 }
26713 }
26714
Bram Moolenaar26df0922014-02-23 23:39:13 +010026715 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26716 {
26717 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26718 if (p == NULL)
26719 return -1;
26720 vim_free(*bufp);
26721 *bufp = *fnamep = p;
26722 *fnamelen = (int)STRLEN(p);
26723 *usedlen += 2;
26724 }
26725
Bram Moolenaar071d4272004-06-13 20:20:40 +000026726 return valid;
26727}
26728
26729/*
26730 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26731 * "flags" can be "g" to do a global substitute.
26732 * Returns an allocated string, NULL for error.
26733 */
26734 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026735do_string_sub(
26736 char_u *str,
26737 char_u *pat,
26738 char_u *sub,
26739 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026740{
26741 int sublen;
26742 regmatch_T regmatch;
26743 int i;
26744 int do_all;
26745 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026746 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026747 garray_T ga;
26748 char_u *ret;
26749 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026750 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751
26752 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26753 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026754 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026755
26756 ga_init2(&ga, 1, 200);
26757
26758 do_all = (flags[0] == 'g');
26759
26760 regmatch.rm_ic = p_ic;
26761 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26762 if (regmatch.regprog != NULL)
26763 {
26764 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026765 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026766 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26767 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026768 /* Skip empty match except for first match. */
26769 if (regmatch.startp[0] == regmatch.endp[0])
26770 {
26771 if (zero_width == regmatch.startp[0])
26772 {
26773 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026774 i = MB_PTR2LEN(tail);
26775 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26776 (size_t)i);
26777 ga.ga_len += i;
26778 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026779 continue;
26780 }
26781 zero_width = regmatch.startp[0];
26782 }
26783
Bram Moolenaar071d4272004-06-13 20:20:40 +000026784 /*
26785 * Get some space for a temporary buffer to do the substitution
26786 * into. It will contain:
26787 * - The text up to where the match is.
26788 * - The substituted text.
26789 * - The text after the match.
26790 */
26791 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026792 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026793 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26794 {
26795 ga_clear(&ga);
26796 break;
26797 }
26798
26799 /* copy the text up to where the match is */
26800 i = (int)(regmatch.startp[0] - tail);
26801 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26802 /* add the substituted text */
26803 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26804 + ga.ga_len + i, TRUE, TRUE, FALSE);
26805 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026806 tail = regmatch.endp[0];
26807 if (*tail == NUL)
26808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809 if (!do_all)
26810 break;
26811 }
26812
26813 if (ga.ga_data != NULL)
26814 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26815
Bram Moolenaar473de612013-06-08 18:19:48 +020026816 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026817 }
26818
26819 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26820 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026821 if (p_cpo == empty_option)
26822 p_cpo = save_cpo;
26823 else
26824 /* Darn, evaluating {sub} expression changed the value. */
26825 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026826
26827 return ret;
26828}
26829
26830#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */