blob: 6e018c404dde240df47186e434524bdae1e18869 [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 Moolenaar02e83b42016-02-21 20:10:26 +0100502# ifdef FEAT_JOB
503static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
504# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
507static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100508static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100512static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
630static void f_isnan(typval_T *argvars, typval_T *rettv);
631#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100632static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100633#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100634# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100635static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100637static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100638static void f_job_start(typval_T *argvars, typval_T *rettv);
639static void f_job_stop(typval_T *argvars, typval_T *rettv);
640static void f_job_status(typval_T *argvars, typval_T *rettv);
641#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100643static void f_js_decode(typval_T *argvars, typval_T *rettv);
644static void f_js_encode(typval_T *argvars, typval_T *rettv);
645static void f_json_decode(typval_T *argvars, typval_T *rettv);
646static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_keys(typval_T *argvars, typval_T *rettv);
648static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
649static void f_len(typval_T *argvars, typval_T *rettv);
650static void f_libcall(typval_T *argvars, typval_T *rettv);
651static void f_libcallnr(typval_T *argvars, typval_T *rettv);
652static void f_line(typval_T *argvars, typval_T *rettv);
653static void f_line2byte(typval_T *argvars, typval_T *rettv);
654static void f_lispindent(typval_T *argvars, typval_T *rettv);
655static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_log(typval_T *argvars, typval_T *rettv);
658static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000659#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_map(typval_T *argvars, typval_T *rettv);
664static void f_maparg(typval_T *argvars, typval_T *rettv);
665static void f_mapcheck(typval_T *argvars, typval_T *rettv);
666static void f_match(typval_T *argvars, typval_T *rettv);
667static void f_matchadd(typval_T *argvars, typval_T *rettv);
668static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
669static void f_matcharg(typval_T *argvars, typval_T *rettv);
670static void f_matchdelete(typval_T *argvars, typval_T *rettv);
671static void f_matchend(typval_T *argvars, typval_T *rettv);
672static void f_matchlist(typval_T *argvars, typval_T *rettv);
673static void f_matchstr(typval_T *argvars, typval_T *rettv);
674static void f_max(typval_T *argvars, typval_T *rettv);
675static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
684static void f_nr2char(typval_T *argvars, typval_T *rettv);
685static void f_or(typval_T *argvars, typval_T *rettv);
686static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
694static void f_printf(typval_T *argvars, typval_T *rettv);
695static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#endif
699#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_range(typval_T *argvars, typval_T *rettv);
703static void f_readfile(typval_T *argvars, typval_T *rettv);
704static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100705#ifdef FEAT_FLOAT
706static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
707#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_reltimestr(typval_T *argvars, typval_T *rettv);
709static void f_remote_expr(typval_T *argvars, typval_T *rettv);
710static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
711static void f_remote_peek(typval_T *argvars, typval_T *rettv);
712static void f_remote_read(typval_T *argvars, typval_T *rettv);
713static void f_remote_send(typval_T *argvars, typval_T *rettv);
714static void f_remove(typval_T *argvars, typval_T *rettv);
715static void f_rename(typval_T *argvars, typval_T *rettv);
716static void f_repeat(typval_T *argvars, typval_T *rettv);
717static void f_resolve(typval_T *argvars, typval_T *rettv);
718static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_screenattr(typval_T *argvars, typval_T *rettv);
723static void f_screenchar(typval_T *argvars, typval_T *rettv);
724static void f_screencol(typval_T *argvars, typval_T *rettv);
725static void f_screenrow(typval_T *argvars, typval_T *rettv);
726static void f_search(typval_T *argvars, typval_T *rettv);
727static void f_searchdecl(typval_T *argvars, typval_T *rettv);
728static void f_searchpair(typval_T *argvars, typval_T *rettv);
729static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
730static void f_searchpos(typval_T *argvars, typval_T *rettv);
731static void f_server2client(typval_T *argvars, typval_T *rettv);
732static void f_serverlist(typval_T *argvars, typval_T *rettv);
733static void f_setbufvar(typval_T *argvars, typval_T *rettv);
734static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
735static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
736static void f_setline(typval_T *argvars, typval_T *rettv);
737static void f_setloclist(typval_T *argvars, typval_T *rettv);
738static void f_setmatches(typval_T *argvars, typval_T *rettv);
739static void f_setpos(typval_T *argvars, typval_T *rettv);
740static void f_setqflist(typval_T *argvars, typval_T *rettv);
741static void f_setreg(typval_T *argvars, typval_T *rettv);
742static void f_settabvar(typval_T *argvars, typval_T *rettv);
743static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
744static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100745#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_shellescape(typval_T *argvars, typval_T *rettv);
749static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
750static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sin(typval_T *argvars, typval_T *rettv);
753static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sort(typval_T *argvars, typval_T *rettv);
756static void f_soundfold(typval_T *argvars, typval_T *rettv);
757static void f_spellbadword(typval_T *argvars, typval_T *rettv);
758static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
759static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sqrt(typval_T *argvars, typval_T *rettv);
762static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_str2nr(typval_T *argvars, typval_T *rettv);
765static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000766#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_stridx(typval_T *argvars, typval_T *rettv);
770static void f_string(typval_T *argvars, typval_T *rettv);
771static void f_strlen(typval_T *argvars, typval_T *rettv);
772static void f_strpart(typval_T *argvars, typval_T *rettv);
773static void f_strridx(typval_T *argvars, typval_T *rettv);
774static void f_strtrans(typval_T *argvars, typval_T *rettv);
775static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
776static void f_strwidth(typval_T *argvars, typval_T *rettv);
777static void f_submatch(typval_T *argvars, typval_T *rettv);
778static void f_substitute(typval_T *argvars, typval_T *rettv);
779static void f_synID(typval_T *argvars, typval_T *rettv);
780static void f_synIDattr(typval_T *argvars, typval_T *rettv);
781static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
782static void f_synstack(typval_T *argvars, typval_T *rettv);
783static void f_synconcealed(typval_T *argvars, typval_T *rettv);
784static void f_system(typval_T *argvars, typval_T *rettv);
785static void f_systemlist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
788static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
789static void f_taglist(typval_T *argvars, typval_T *rettv);
790static void f_tagfiles(typval_T *argvars, typval_T *rettv);
791static void f_tempname(typval_T *argvars, typval_T *rettv);
792static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tan(typval_T *argvars, typval_T *rettv);
795static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tolower(typval_T *argvars, typval_T *rettv);
798static void f_toupper(typval_T *argvars, typval_T *rettv);
799static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000800#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100801static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_type(typval_T *argvars, typval_T *rettv);
804static void f_undofile(typval_T *argvars, typval_T *rettv);
805static void f_undotree(typval_T *argvars, typval_T *rettv);
806static void f_uniq(typval_T *argvars, typval_T *rettv);
807static void f_values(typval_T *argvars, typval_T *rettv);
808static void f_virtcol(typval_T *argvars, typval_T *rettv);
809static void f_visualmode(typval_T *argvars, typval_T *rettv);
810static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
811static void f_winbufnr(typval_T *argvars, typval_T *rettv);
812static void f_wincol(typval_T *argvars, typval_T *rettv);
813static void f_winheight(typval_T *argvars, typval_T *rettv);
814static void f_winline(typval_T *argvars, typval_T *rettv);
815static void f_winnr(typval_T *argvars, typval_T *rettv);
816static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
817static void f_winrestview(typval_T *argvars, typval_T *rettv);
818static void f_winsaveview(typval_T *argvars, typval_T *rettv);
819static void f_winwidth(typval_T *argvars, typval_T *rettv);
820static void f_writefile(typval_T *argvars, typval_T *rettv);
821static void f_wordcount(typval_T *argvars, typval_T *rettv);
822static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000823
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
825static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
826static int get_env_len(char_u **arg);
827static int get_id_len(char_u **arg);
828static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
829static 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 +0000830#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
831#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
832 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
834static int eval_isnamec(int c);
835static int eval_isnamec1(int c);
836static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
837static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100838static typval_T *alloc_string_tv(char_u *string);
839static void init_tv(typval_T *varp);
840static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100841#ifdef FEAT_FLOAT
842static float_T get_tv_float(typval_T *varp);
843#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100844static linenr_T get_tv_lnum(typval_T *argvars);
845static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
846static char_u *get_tv_string(typval_T *varp);
847static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
848static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
849static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
850static hashtab_T *find_var_ht(char_u *name, char_u **varname);
851static funccall_T *get_funccal(void);
852static void vars_clear_ext(hashtab_T *ht, int free_val);
853static void delete_var(hashtab_T *ht, hashitem_T *hi);
854static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
855static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
856static void set_var(char_u *name, typval_T *varp, int copy);
857static int var_check_ro(int flags, char_u *name, int use_gettext);
858static int var_check_fixed(int flags, char_u *name, int use_gettext);
859static int var_check_func_name(char_u *name, int new_var);
860static int valid_varname(char_u *varname);
861static int tv_check_lock(int lock, char_u *name, int use_gettext);
862static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
863static char_u *find_option_end(char_u **arg, int *opt_flags);
864static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
865static int eval_fname_script(char_u *p);
866static int eval_fname_sid(char_u *p);
867static void list_func_head(ufunc_T *fp, int indent);
868static ufunc_T *find_func(char_u *name);
869static int function_exists(char_u *name);
870static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872static void func_do_profile(ufunc_T *fp);
873static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
874static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000875static int
876# ifdef __BORLANDC__
877 _RTLENTRYF
878# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000880static int
881# ifdef __BORLANDC__
882 _RTLENTRYF
883# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000885#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static int script_autoload(char_u *name, int reload);
887static char_u *autoload_name(char_u *name);
888static void cat_func_name(char_u *buf, ufunc_T *fp);
889static void func_free(ufunc_T *fp);
890static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
891static int can_free_funccal(funccall_T *fc, int copyID) ;
892static void free_funccal(funccall_T *fc, int free_val);
893static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
894static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
895static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
896static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
897static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
898static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
899static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
900static int write_list(FILE *fd, list_T *list, int binary);
901static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000902
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200903
904#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static int compare_func_name(const void *s1, const void *s2);
906static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200907#endif
908
Bram Moolenaar33570922005-01-25 22:26:29 +0000909/*
910 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000911 */
912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000914{
Bram Moolenaar33570922005-01-25 22:26:29 +0000915 int i;
916 struct vimvar *p;
917
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200918 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
919 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200920 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000921 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000922 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000923
924 for (i = 0; i < VV_LEN; ++i)
925 {
926 p = &vimvars[i];
927 STRCPY(p->vv_di.di_key, p->vv_name);
928 if (p->vv_flags & VV_RO)
929 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
930 else if (p->vv_flags & VV_RO_SBX)
931 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
932 else
933 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000934
935 /* add to v: scope dict, unless the value is not always available */
936 if (p->vv_type != VAR_UNKNOWN)
937 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000939 /* add to compat scope dict */
940 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100942 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
943
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000944 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100945 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200946 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100947 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100948
949 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
950 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
951 set_vim_var_nr(VV_NONE, VVAL_NONE);
952 set_vim_var_nr(VV_NULL, VVAL_NULL);
953
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200954 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200955
956#ifdef EBCDIC
957 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100958 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200959 */
960 sortFunctions();
961#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000962}
963
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964#if defined(EXITFREE) || defined(PROTO)
965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100966eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967{
968 int i;
969 struct vimvar *p;
970
971 for (i = 0; i < VV_LEN; ++i)
972 {
973 p = &vimvars[i];
974 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000975 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000976 vim_free(p->vv_str);
977 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000978 }
979 else if (p->vv_di.di_tv.v_type == VAR_LIST)
980 {
981 list_unref(p->vv_list);
982 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000983 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984 }
985 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000986 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000987 hash_clear(&compat_hashtab);
988
Bram Moolenaard9fba312005-06-26 22:34:35 +0000989 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100990# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200991 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993
994 /* global variables */
995 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000996
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000997 /* autoloaded script names */
998 ga_clear_strings(&ga_loaded);
999
Bram Moolenaarcca74132013-09-25 21:00:28 +02001000 /* Script-local variables. First clear all the variables and in a second
1001 * loop free the scriptvar_T, because a variable in one script might hold
1002 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 ga_clear(&ga_scripts);
1008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001009 /* unreferenced lists and dicts */
1010 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001011
1012 /* functions */
1013 free_all_functions();
1014 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015}
1016#endif
1017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * Return the name of the executed function.
1020 */
1021 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001022func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
1028 * Return the address holding the next breakpoint line for a funccall cookie.
1029 */
1030 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001031func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
Bram Moolenaar33570922005-01-25 22:26:29 +00001033 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036/*
1037 * Return the address holding the debug tick for a funccall cookie.
1038 */
1039 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001040func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar33570922005-01-25 22:26:29 +00001042 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044
1045/*
1046 * Return the nesting level for a funccall cookie.
1047 */
1048 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001049func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar33570922005-01-25 22:26:29 +00001051 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052}
1053
1054/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001055funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001057/* pointer to list of previously used funccal, still around because some
1058 * item in it is still being used. */
1059funccall_T *previous_funccal = NULL;
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061/*
1062 * Return TRUE when a function was ended by a ":return" command.
1063 */
1064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001065current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
1067 return current_funccal->returned;
1068}
1069
1070
1071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * Set an internal variable to a string value. Creates the variable if it does
1073 * not already exist.
1074 */
1075 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001076set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001078 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 val = vim_strsave(value);
1082 if (val != NULL)
1083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001084 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001085 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091}
1092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static char_u *redir_endp = NULL;
1096static char_u *redir_varname = NULL;
1097
1098/*
1099 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001100 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 * Returns OK if successfully completed the setup. FAIL otherwise.
1102 */
1103 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001104var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105{
1106 int save_emsg;
1107 int err;
1108 typval_T tv;
1109
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001110 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001111 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 {
1113 EMSG(_(e_invarg));
1114 return FAIL;
1115 }
1116
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001117 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 redir_varname = vim_strsave(name);
1119 if (redir_varname == NULL)
1120 return FAIL;
1121
1122 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1123 if (redir_lval == NULL)
1124 {
1125 var_redir_stop();
1126 return FAIL;
1127 }
1128
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 /* The output is stored in growarray "redir_ga" until redirection ends. */
1130 ga_init2(&redir_ga, (int)sizeof(char), 500);
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001133 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001134 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1136 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001137 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_endp != NULL && *redir_endp != NUL)
1139 /* Trailing characters are present after the variable name */
1140 EMSG(_(e_trailing));
1141 else
1142 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 var_redir_stop();
1145 return FAIL;
1146 }
1147
1148 /* check if we can write to the variable: set it to or append an empty
1149 * string */
1150 save_emsg = did_emsg;
1151 did_emsg = FALSE;
1152 tv.v_type = VAR_STRING;
1153 tv.vval.v_string = (char_u *)"";
1154 if (append)
1155 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1156 else
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001160 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 if (err)
1162 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 var_redir_stop();
1165 return FAIL;
1166 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
1168 return OK;
1169}
1170
1171/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 * Append "value[value_len]" to the variable set by var_redir_start().
1173 * The actual appending is postponed until redirection ends, because the value
1174 * appended may in fact be the string we write to, changing it may cause freed
1175 * memory to be used:
1176 * :redir => foo
1177 * :let foo
1178 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 */
1180 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001181var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001183 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
1185 if (redir_lval == NULL)
1186 return;
1187
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001188 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 {
1195 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 }
1198 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200}
1201
1202/*
1203 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001204 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 */
1206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001207var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 typval_T tv;
1210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 if (redir_lval != NULL)
1212 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001213 /* If there was no error: assign the text to the variable. */
1214 if (redir_endp != NULL)
1215 {
1216 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1217 tv.v_type = VAR_STRING;
1218 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001219 /* Call get_lval() again, if it's inside a Dict or List it may
1220 * have changed. */
1221 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001222 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001223 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1224 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1225 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001226 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001227
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 /* free the collected output */
1229 vim_free(redir_ga.ga_data);
1230 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232 vim_free(redir_lval);
1233 redir_lval = NULL;
1234 }
1235 vim_free(redir_varname);
1236 redir_varname = NULL;
1237}
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239# if defined(FEAT_MBYTE) || defined(PROTO)
1240 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001241eval_charconvert(
1242 char_u *enc_from,
1243 char_u *enc_to,
1244 char_u *fname_from,
1245 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
1247 int err = FALSE;
1248
1249 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1250 set_vim_var_string(VV_CC_TO, enc_to, -1);
1251 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1252 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1253 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1254 err = TRUE;
1255 set_vim_var_string(VV_CC_FROM, NULL, -1);
1256 set_vim_var_string(VV_CC_TO, NULL, -1);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259
1260 if (err)
1261 return FAIL;
1262 return OK;
1263}
1264# endif
1265
1266# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, fname, -1);
1273 set_vim_var_string(VV_CMDARG, args, -1);
1274 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1275 err = TRUE;
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_CMDARG, NULL, -1);
1278
1279 if (err)
1280 {
1281 mch_remove(fname);
1282 return FAIL;
1283 }
1284 return OK;
1285}
1286# endif
1287
1288# if defined(FEAT_DIFF) || defined(PROTO)
1289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001290eval_diff(
1291 char_u *origfile,
1292 char_u *newfile,
1293 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 int err = FALSE;
1296
1297 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1298 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1299 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1300 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1301 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1302 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1303 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1304}
1305
1306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001307eval_patch(
1308 char_u *origfile,
1309 char_u *difffile,
1310 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err;
1313
1314 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1315 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1316 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1317 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1320 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1321}
1322# endif
1323
1324/*
1325 * Top level evaluation function, returning a boolean.
1326 * Sets "error" to TRUE if there was an error.
1327 * Return TRUE or FALSE.
1328 */
1329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001330eval_to_bool(
1331 char_u *arg,
1332 int *error,
1333 char_u **nextcmd,
1334 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 int retval = FALSE;
1338
1339 if (skip)
1340 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 {
1345 *error = FALSE;
1346 if (!skip)
1347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001348 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 }
1352 if (skip)
1353 --emsg_skip;
1354
1355 return retval;
1356}
1357
1358/*
1359 * Top level evaluation function, returning a string. If "skip" is TRUE,
1360 * only parsing to "nextcmd" is done, without reporting errors. Return
1361 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1362 */
1363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001364eval_to_string_skip(
1365 char_u *arg,
1366 char_u **nextcmd,
1367 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
1371
1372 if (skip)
1373 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 retval = NULL;
1376 else
1377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 retval = vim_strsave(get_tv_string(&tv));
1379 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 if (skip)
1382 --emsg_skip;
1383
1384 return retval;
1385}
1386
1387/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388 * Skip over an expression at "*pp".
1389 * Return FAIL for an error, OK otherwise.
1390 */
1391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393{
Bram Moolenaar33570922005-01-25 22:26:29 +00001394 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395
1396 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398}
1399
1400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001402 * When "convert" is TRUE convert a List into a sequence of lines and convert
1403 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Return pointer to allocated memory, or NULL for failure.
1405 */
1406 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001407eval_to_string(
1408 char_u *arg,
1409 char_u **nextcmd,
1410 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001414 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001415#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001416 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = NULL;
1421 else
1422 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001423 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 {
1425 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001426 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001427 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 if (tv.vval.v_list->lv_len > 0)
1430 ga_append(&ga, NL);
1431 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 ga_append(&ga, NUL);
1433 retval = (char_u *)ga.ga_data;
1434 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001435#ifdef FEAT_FLOAT
1436 else if (convert && tv.v_type == VAR_FLOAT)
1437 {
1438 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1439 retval = vim_strsave(numbuf);
1440 }
1441#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 else
1443 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446
1447 return retval;
1448}
1449
1450/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001451 * Call eval_to_string() without using current local variables and using
1452 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
1454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001455eval_to_string_safe(
1456 char_u *arg,
1457 char_u **nextcmd,
1458 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 char_u *retval;
1461 void *save_funccalp;
1462
1463 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001464 if (use_sandbox)
1465 ++sandbox;
1466 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001467 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001468 if (use_sandbox)
1469 --sandbox;
1470 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 restore_funccal(save_funccalp);
1472 return retval;
1473}
1474
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475/*
1476 * Top level evaluation function, returning a number.
1477 * Evaluates "expr" silently.
1478 * Returns -1 for an error.
1479 */
1480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001481eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
Bram Moolenaar33570922005-01-25 22:26:29 +00001483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001485 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487 ++emsg_off;
1488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 retval = -1;
1491 else
1492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001493 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 --emsg_off;
1497
1498 return retval;
1499}
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501/*
1502 * Prepare v: variable "idx" to be used.
1503 * Save the current typeval in "save_tv".
1504 * When not used yet add the variable to the v: hashtable.
1505 */
1506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508{
1509 *save_tv = vimvars[idx].vv_tv;
1510 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1511 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1512}
1513
1514/*
1515 * Restore v: variable "idx" to typeval "save_tv".
1516 * When no longer defined, remove the variable from the v: hashtable.
1517 */
1518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001520{
1521 hashitem_T *hi;
1522
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523 vimvars[idx].vv_tv = *save_tv;
1524 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1525 {
1526 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1527 if (HASHITEM_EMPTY(hi))
1528 EMSG2(_(e_intern2), "restore_vimvar()");
1529 else
1530 hash_remove(&vimvarht, hi);
1531 }
1532}
1533
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001534#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535/*
1536 * Evaluate an expression to a list with suggestions.
1537 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001538 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001539 */
1540 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001541eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542{
1543 typval_T save_val;
1544 typval_T rettv;
1545 list_T *list = NULL;
1546 char_u *p = skipwhite(expr);
1547
1548 /* Set "v:val" to the bad word. */
1549 prepare_vimvar(VV_VAL, &save_val);
1550 vimvars[VV_VAL].vv_type = VAR_STRING;
1551 vimvars[VV_VAL].vv_str = badword;
1552 if (p_verbose == 0)
1553 ++emsg_off;
1554
1555 if (eval1(&p, &rettv, TRUE) == OK)
1556 {
1557 if (rettv.v_type != VAR_LIST)
1558 clear_tv(&rettv);
1559 else
1560 list = rettv.vval.v_list;
1561 }
1562
1563 if (p_verbose == 0)
1564 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001565 restore_vimvar(VV_VAL, &save_val);
1566
1567 return list;
1568}
1569
1570/*
1571 * "list" is supposed to contain two items: a word and a number. Return the
1572 * word in "pp" and the number as the return value.
1573 * Return -1 if anything isn't right.
1574 * Used to get the good word and score from the eval_spell_expr() result.
1575 */
1576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001577get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001578{
1579 listitem_T *li;
1580
1581 li = list->lv_first;
1582 if (li == NULL)
1583 return -1;
1584 *pp = get_tv_string(&li->li_tv);
1585
1586 li = li->li_next;
1587 if (li == NULL)
1588 return -1;
1589 return get_tv_number(&li->li_tv);
1590}
1591#endif
1592
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001594 * Top level evaluation function.
1595 * Returns an allocated typval_T with the result.
1596 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597 */
1598 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600{
1601 typval_T *tv;
1602
1603 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 {
1606 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001607 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608 }
1609
1610 return tv;
1611}
1612
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 * Uses argv[argc] for the function arguments. Only Number and String
1617 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001620 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001621call_vim_function(
1622 char_u *func,
1623 int argc,
1624 char_u **argv,
1625 int safe, /* use the sandbox */
1626 int str_arg_only, /* all arguments are strings */
1627 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 long n;
1631 int len;
1632 int i;
1633 int doesrange;
1634 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001637 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 for (i = 0; i < argc; i++)
1642 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001643 /* Pass a NULL or empty argument as an empty string */
1644 if (argv[i] == NULL || *argv[i] == NUL)
1645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001646 argvars[i].v_type = VAR_STRING;
1647 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001648 continue;
1649 }
1650
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001651 if (str_arg_only)
1652 len = 0;
1653 else
1654 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001655 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (len != 0 && len == (int)STRLEN(argv[i]))
1657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001658 argvars[i].v_type = VAR_NUMBER;
1659 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_STRING;
1664 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 }
1667
1668 if (safe)
1669 {
1670 save_funccalp = save_funccal();
1671 ++sandbox;
1672 }
1673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1675 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (safe)
1679 {
1680 --sandbox;
1681 restore_funccal(save_funccalp);
1682 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 vim_free(argvars);
1684
1685 if (ret == FAIL)
1686 clear_tv(rettv);
1687
1688 return ret;
1689}
1690
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001691/*
1692 * Call vimL function "func" and return the result as a number.
1693 * Returns -1 when calling the function fails.
1694 * Uses argv[argc] for the function arguments.
1695 */
1696 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697call_func_retnr(
1698 char_u *func,
1699 int argc,
1700 char_u **argv,
1701 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001702{
1703 typval_T rettv;
1704 long retval;
1705
1706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1708 return -1;
1709
1710 retval = get_tv_number_chk(&rettv, NULL);
1711 clear_tv(&rettv);
1712 return retval;
1713}
1714
1715#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1716 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1717
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720 * Call vimL function "func" and return the result as a string.
1721 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
1723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retstr(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001732 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 return retval;
1741}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001742# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001744/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001745 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 */
1749 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750call_func_retlist(
1751 char_u *func,
1752 int argc,
1753 char_u **argv,
1754 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755{
1756 typval_T rettv;
1757
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001758 /* All arguments are passed as strings, no conversion to number. */
1759 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 return NULL;
1761
1762 if (rettv.v_type != VAR_LIST)
1763 {
1764 clear_tv(&rettv);
1765 return NULL;
1766 }
1767
1768 return rettv.vval.v_list;
1769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
1771
1772/*
1773 * Save the current function call pointer, and set it to NULL.
1774 * Used when executing autocommands and for ":source".
1775 */
1776 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001779 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 current_funccal = NULL;
1782 return (void *)fc;
1783}
1784
1785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788 funccall_T *fc = (funccall_T *)vfc;
1789
1790 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791}
1792
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793#if defined(FEAT_PROFILE) || defined(PROTO)
1794/*
1795 * Prepare profiling for entering a child or something else that is not
1796 * counted for the script/function itself.
1797 * Should always be called in pair with prof_child_exit().
1798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800prof_child_enter(
1801 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001802{
1803 funccall_T *fc = current_funccal;
1804
1805 if (fc != NULL && fc->func->uf_profiling)
1806 profile_start(&fc->prof_child);
1807 script_prof_save(tm);
1808}
1809
1810/*
1811 * Take care of time spent in a child.
1812 * Should always be called after prof_child_enter().
1813 */
1814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815prof_child_exit(
1816 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817{
1818 funccall_T *fc = current_funccal;
1819
1820 if (fc != NULL && fc->func->uf_profiling)
1821 {
1822 profile_end(&fc->prof_child);
1823 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1824 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1825 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1826 }
1827 script_prof_restore(tm);
1828}
1829#endif
1830
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#ifdef FEAT_FOLDING
1833/*
1834 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1835 * it in "*cp". Doesn't give error messages.
1836 */
1837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int retval;
1842 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001843 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1844 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 ++sandbox;
1849 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 retval = 0;
1853 else
1854 {
1855 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 if (tv.v_type == VAR_NUMBER)
1857 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001858 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 retval = 0;
1860 else
1861 {
1862 /* If the result is a string, check if there is a non-digit before
1863 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (!VIM_ISDIGIT(*s) && *s != '-')
1866 *cp = *s++;
1867 retval = atol((char *)s);
1868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001872 if (use_sandbox)
1873 --sandbox;
1874 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 return retval;
1877}
1878#endif
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 * ":let" list all variable values
1882 * ":let var1 var2" list variable values
1883 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 * ":let var += expr" assignment command.
1885 * ":let var -= expr" assignment command.
1886 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 int var_count = 0;
1897 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
Bram Moolenaardb552d602006-03-23 22:59:57 +00001902 argend = skip_var_list(arg, &var_count, &semicolon);
1903 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001905 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1906 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001907 expr = skipwhite(argend);
1908 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1909 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001911 /*
1912 * ":let" without "=": list variables
1913 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 if (*arg == '[')
1915 EMSG(_(e_invarg));
1916 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001918 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001922 list_glob_vars(&first);
1923 list_buf_vars(&first);
1924 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001925#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_script_vars(&first);
1929 list_func_vars(&first);
1930 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 eap->nextcmd = check_nextcmd(arg);
1933 }
1934 else
1935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 op[0] = '=';
1937 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001938 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1941 op[0] = *expr; /* +=, -= or .= */
1942 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 else
1945 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (eap->skip)
1951 {
1952 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 --emsg_skip;
1955 }
1956 else if (i != FAIL)
1957 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 }
1963}
1964
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965/*
1966 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1967 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 * When "nextchars" is not NULL it points to a string with characters that
1969 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1970 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 * Returns OK or FAIL;
1972 */
1973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001974ex_let_vars(
1975 char_u *arg_start,
1976 typval_T *tv,
1977 int copy, /* copy values from "tv", don't move */
1978 int semicolon, /* from skip_var_list() */
1979 int var_count, /* from skip_var_list() */
1980 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981{
1982 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 listitem_T *item;
1986 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987
1988 if (*arg != '[')
1989 {
1990 /*
1991 * ":let var = expr" or ":for var in list"
1992 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 return OK;
1996 }
1997
1998 /*
1999 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2000 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002001 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 {
2003 EMSG(_(e_listreq));
2004 return FAIL;
2005 }
2006
2007 i = list_len(l);
2008 if (semicolon == 0 && var_count < i)
2009 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002010 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 }
2013 if (var_count - semicolon > i)
2014 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002015 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 }
2018
2019 item = l->lv_first;
2020 while (*arg != ']')
2021 {
2022 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 item = item->li_next;
2025 if (arg == NULL)
2026 return FAIL;
2027
2028 arg = skipwhite(arg);
2029 if (*arg == ';')
2030 {
2031 /* Put the rest of the list (may be empty) in the var after ';'.
2032 * Create a new list for this. */
2033 l = list_alloc();
2034 if (l == NULL)
2035 return FAIL;
2036 while (item != NULL)
2037 {
2038 list_append_tv(l, &item->li_tv);
2039 item = item->li_next;
2040 }
2041
2042 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002043 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 ltv.vval.v_list = l;
2045 l->lv_refcount = 1;
2046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2048 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 clear_tv(&ltv);
2050 if (arg == NULL)
2051 return FAIL;
2052 break;
2053 }
2054 else if (*arg != ',' && *arg != ']')
2055 {
2056 EMSG2(_(e_intern2), "ex_let_vars()");
2057 return FAIL;
2058 }
2059 }
2060
2061 return OK;
2062}
2063
2064/*
2065 * Skip over assignable variable "var" or list of variables "[var, var]".
2066 * Used for ":let varvar = expr" and ":for varvar in expr".
2067 * For "[var, var]" increment "*var_count" for each variable.
2068 * for "[var, var; var]" set "semicolon".
2069 * Return NULL for an error.
2070 */
2071 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002072skip_var_list(
2073 char_u *arg,
2074 int *var_count,
2075 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076{
2077 char_u *p, *s;
2078
2079 if (*arg == '[')
2080 {
2081 /* "[var, var]": find the matching ']'. */
2082 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002083 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 {
2085 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2086 s = skip_var_one(p);
2087 if (s == p)
2088 {
2089 EMSG2(_(e_invarg2), p);
2090 return NULL;
2091 }
2092 ++*var_count;
2093
2094 p = skipwhite(s);
2095 if (*p == ']')
2096 break;
2097 else if (*p == ';')
2098 {
2099 if (*semicolon == 1)
2100 {
2101 EMSG(_("Double ; in list of variables"));
2102 return NULL;
2103 }
2104 *semicolon = 1;
2105 }
2106 else if (*p != ',')
2107 {
2108 EMSG2(_(e_invarg2), p);
2109 return NULL;
2110 }
2111 }
2112 return p + 1;
2113 }
2114 else
2115 return skip_var_one(arg);
2116}
2117
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002119 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002120 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002123skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002125 if (*arg == '@' && arg[1] != NUL)
2126 return arg + 2;
2127 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2128 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129}
2130
Bram Moolenaara7043832005-01-21 11:56:39 +00002131/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002132 * List variables for hashtab "ht" with prefix "prefix".
2133 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002136list_hashtable_vars(
2137 hashtab_T *ht,
2138 char_u *prefix,
2139 int empty,
2140 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar33570922005-01-25 22:26:29 +00002142 hashitem_T *hi;
2143 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 int todo;
2145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002146 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2148 {
2149 if (!HASHITEM_EMPTY(hi))
2150 {
2151 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002152 di = HI2DI(hi);
2153 if (empty || di->di_tv.v_type != VAR_STRING
2154 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002156 }
2157 }
2158}
2159
2160/*
2161 * List global variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002167}
2168
2169/*
2170 * List buffer variables.
2171 */
2172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 char_u numbuf[NUMBUFLEN];
2176
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179
2180 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2182 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002183}
2184
2185/*
2186 * List window variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002190{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002191 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193}
2194
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002195#ifdef FEAT_WINDOWS
2196/*
2197 * List tab page variables.
2198 */
2199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002200list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002202 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204}
2205#endif
2206
Bram Moolenaara7043832005-01-21 11:56:39 +00002207/*
2208 * List Vim variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214}
2215
2216/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217 * List script-local variables, if there is a script.
2218 */
2219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221{
2222 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2224 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225}
2226
2227/*
2228 * List function variables, if there is a function.
2229 */
2230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232{
2233 if (current_funccal != NULL)
2234 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002236}
2237
2238/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 * List variables in "arg".
2240 */
2241 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243{
2244 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 char_u *name_start;
2248 char_u *arg_subsc;
2249 char_u *tofree;
2250 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251
2252 while (!ends_excmd(*arg) && !got_int)
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002256 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2258 {
2259 emsg_severe = TRUE;
2260 EMSG(_(e_trailing));
2261 break;
2262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 /* get_name_len() takes care of expanding curly braces */
2267 name_start = name = arg;
2268 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2269 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 /* This is mainly to keep test 49 working: when expanding
2272 * curly braces fails overrule the exception error message. */
2273 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 emsg_severe = TRUE;
2276 EMSG2(_(e_invarg2), arg);
2277 break;
2278 }
2279 error = TRUE;
2280 }
2281 else
2282 {
2283 if (tofree != NULL)
2284 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002285 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 else
2288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 /* handle d.key, l[idx], f(expr) */
2290 arg_subsc = arg;
2291 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 case 'g': list_glob_vars(first); break;
2300 case 'b': list_buf_vars(first); break;
2301 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002302#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002303 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 'v': list_vim_vars(first); break;
2306 case 's': list_script_vars(first); break;
2307 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 default:
2309 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 }
2312 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 {
2314 char_u numbuf[NUMBUFLEN];
2315 char_u *tf;
2316 int c;
2317 char_u *s;
2318
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002319 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 c = *arg;
2321 *arg = NUL;
2322 list_one_var_a((char_u *)"",
2323 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002324 tv.v_type,
2325 s == NULL ? (char_u *)"" : s,
2326 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 *arg = c;
2328 vim_free(tf);
2329 }
2330 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
2333 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334
2335 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337
2338 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
2340
2341 return arg;
2342}
2343
2344/*
2345 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2346 * Returns a pointer to the char just after the var name.
2347 * Returns NULL if there is an error.
2348 */
2349 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002350ex_let_one(
2351 char_u *arg, /* points to variable name */
2352 typval_T *tv, /* value to assign to variable */
2353 int copy, /* copy value from "tv" */
2354 char_u *endchars, /* valid chars after variable name or NULL */
2355 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356{
2357 int c1;
2358 char_u *name;
2359 char_u *p;
2360 char_u *arg_end = NULL;
2361 int len;
2362 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364
2365 /*
2366 * ":let $VAR = expr": Set environment variable.
2367 */
2368 if (*arg == '$')
2369 {
2370 /* Find the end of the name. */
2371 ++arg;
2372 name = arg;
2373 len = get_env_len(&arg);
2374 if (len == 0)
2375 EMSG2(_(e_invarg2), name - 1);
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 if (op != NULL && (*op == '+' || *op == '-'))
2379 EMSG2(_(e_letwrong), op);
2380 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002381 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002383 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 {
2385 c1 = name[len];
2386 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 p = get_tv_string_chk(tv);
2388 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 {
2390 int mustfree = FALSE;
2391 char_u *s = vim_getenv(name, &mustfree);
2392
2393 if (s != NULL)
2394 {
2395 p = tofree = concat_str(s, p);
2396 if (mustfree)
2397 vim_free(s);
2398 }
2399 }
2400 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 if (STRICMP(name, "HOME") == 0)
2404 init_homedir();
2405 else if (didset_vim && STRICMP(name, "VIM") == 0)
2406 didset_vim = FALSE;
2407 else if (didset_vimruntime
2408 && STRICMP(name, "VIMRUNTIME") == 0)
2409 didset_vimruntime = FALSE;
2410 arg_end = arg;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 }
2415 }
2416 }
2417
2418 /*
2419 * ":let &option = expr": Set option value.
2420 * ":let &l:option = expr": Set local option value.
2421 * ":let &g:option = expr": Set global option value.
2422 */
2423 else if (*arg == '&')
2424 {
2425 /* Find the end of the name. */
2426 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 if (p == NULL || (endchars != NULL
2428 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 EMSG(_(e_letunexp));
2430 else
2431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 long n;
2433 int opt_type;
2434 long numval;
2435 char_u *stringval = NULL;
2436 char_u *s;
2437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 c1 = *p;
2439 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440
2441 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 s = get_tv_string_chk(tv); /* != NULL if number or string */
2443 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 {
2445 opt_type = get_option_value(arg, &numval,
2446 &stringval, opt_flags);
2447 if ((opt_type == 1 && *op == '.')
2448 || (opt_type == 0 && *op != '.'))
2449 EMSG2(_(e_letwrong), op);
2450 else
2451 {
2452 if (opt_type == 1) /* number */
2453 {
2454 if (*op == '+')
2455 n = numval + n;
2456 else
2457 n = numval - n;
2458 }
2459 else if (opt_type == 0 && stringval != NULL) /* string */
2460 {
2461 s = concat_str(stringval, s);
2462 vim_free(stringval);
2463 stringval = s;
2464 }
2465 }
2466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 if (s != NULL)
2468 {
2469 set_option_value(arg, n, s, opt_flags);
2470 arg_end = p;
2471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let @r = expr": Set register contents.
2479 */
2480 else if (*arg == '@')
2481 {
2482 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 if (op != NULL && (*op == '+' || *op == '-'))
2484 EMSG2(_(e_letwrong), op);
2485 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002486 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 EMSG(_(e_letunexp));
2488 else
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 char_u *s;
2492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 p = get_tv_string_chk(tv);
2494 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002496 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 if (s != NULL)
2498 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 vim_free(s);
2501 }
2502 }
2503 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 arg_end = arg + 1;
2507 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 }
2510 }
2511
2512 /*
2513 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002520 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 arg_end = p;
2529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
2533
2534 else
2535 EMSG2(_(e_invarg2), arg);
2536
2537 return arg_end;
2538}
2539
2540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2542 */
2543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002544check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545{
2546 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2547 {
2548 EMSG2(_(e_readonlyvar), arg);
2549 return TRUE;
2550 }
2551 return FALSE;
2552}
2553
2554/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Get an lval: variable, Dict item or List item that can be assigned a value
2556 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2557 * "name.key", "name.key[expr]" etc.
2558 * Indexing only works if "name" is an existing List or Dictionary.
2559 * "name" points to the start of the name.
2560 * If "rettv" is not NULL it points to the value to be assigned.
2561 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2562 * wrong; must end in space or cmd separator.
2563 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564 * flags:
2565 * GLV_QUIET: do not give error messages
2566 * GLV_NO_AUTOLOAD: do not use script autoloading
2567 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 */
2572 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002573get_lval(
2574 char_u *name,
2575 typval_T *rettv,
2576 lval_T *lp,
2577 int unlet,
2578 int skip,
2579 int flags, /* GLV_ values */
2580 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 char_u *expr_start, *expr_end;
2584 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 dictitem_T *v;
2586 typval_T var1;
2587 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002593 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597
2598 if (skip)
2599 {
2600 /* When skipping just find the end of the name. */
2601 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002602 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
2604
2605 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002606 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (expr_start != NULL)
2608 {
2609 /* Don't expand the name when we already know there is an error. */
2610 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2611 && *p != '[' && *p != '.')
2612 {
2613 EMSG(_(e_trailing));
2614 return NULL;
2615 }
2616
2617 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2618 if (lp->ll_exp_name == NULL)
2619 {
2620 /* Report an invalid expression in braces, unless the
2621 * expression evaluation has been cancelled due to an
2622 * aborting error, an interrupt, or an exception. */
2623 if (!aborting() && !quiet)
2624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 EMSG2(_(e_invarg2), name);
2627 return NULL;
2628 }
2629 }
2630 lp->ll_name = lp->ll_exp_name;
2631 }
2632 else
2633 lp->ll_name = name;
2634
2635 /* Without [idx] or .key we are done. */
2636 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2637 return p;
2638
2639 cc = *p;
2640 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002641 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (v == NULL && !quiet)
2643 EMSG2(_(e_undefvar), lp->ll_name);
2644 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 if (v == NULL)
2646 return NULL;
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /*
2649 * Loop until no more [idx] or .key is following.
2650 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002651 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2655 && !(lp->ll_tv->v_type == VAR_DICT
2656 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E689: Can only index a List or Dictionary"));
2660 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E708: [:] must come last"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 len = -1;
2670 if (*p == '.')
2671 {
2672 key = p + 1;
2673 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2674 ;
2675 if (len == 0)
2676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_(e_emptykey));
2679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
2681 p = key + len;
2682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 else
2684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p == ':')
2688 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 empty1 = FALSE;
2692 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var1) == NULL)
2695 {
2696 /* not a number or string */
2697 clear_tv(&var1);
2698 return NULL;
2699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Optionally get the second index [ :expr]. */
2703 if (*p == ':')
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (rettv != NULL && (rettv->v_type != VAR_LIST
2714 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 p = skipwhite(p + 1);
2723 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 if (get_tv_string_chk(&var2) == NULL)
2735 {
2736 /* not a number or string */
2737 if (!empty1)
2738 clear_tv(&var1);
2739 clear_tv(&var2);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (!empty1)
2753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758
2759 /* Skip to past ']'. */
2760 ++p;
2761 }
2762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 {
2765 if (len == -1)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (*key == NUL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
2772 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 lp->ll_list = NULL;
2778 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 /* When assigning to a scope dictionary check that a function and
2782 * variable name is valid (only variable name unless it is l: or
2783 * g: dictionary). Disallow overwriting a builtin function. */
2784 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 int prevval;
2787 int wrong;
2788
2789 if (len != -1)
2790 {
2791 prevval = key[len];
2792 key[len] = NUL;
2793 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002794 else
2795 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2797 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 || !valid_varname(key);
2800 if (len != -1)
2801 key[len] = prevval;
2802 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 return NULL;
2804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 /* Can't add "v:" variable. */
2809 if (lp->ll_dict == &vimvardict)
2810 {
2811 EMSG2(_(e_illvar), name);
2812 return NULL;
2813 }
2814
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 p = NULL;
2832 break;
2833 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 return NULL;
2837
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (len == -1)
2839 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842 else
2843 {
2844 /*
2845 * Get the number and item for the only or first index of the List.
2846 */
2847 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 else
2850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var1);
2853 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_list = lp->ll_tv->vval.v_list;
2856 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2857 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859 if (lp->ll_n1 < 0)
2860 {
2861 lp->ll_n1 = 0;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 }
2864 }
2865 if (lp->ll_li == NULL)
2866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 if (!quiet)
2870 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
2874 /*
2875 * May need to find the item or absolute index for the second
2876 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 * When no index given: "lp->ll_empty2" is TRUE.
2878 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002882 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2897 if (lp->ll_n1 < 0)
2898 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2899 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 {
2901 if (!quiet)
2902 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return p;
2912}
2913
2914/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919{
2920 vim_free(lp->ll_exp_name);
2921 vim_free(lp->ll_newkey);
2922}
2923
2924/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 */
2929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930set_var_lval(
2931 lval_T *lp,
2932 char_u *endp,
2933 typval_T *rettv,
2934 int copy,
2935 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936{
2937 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 listitem_T *ri;
2939 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940
2941 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 cc = *endp;
2946 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 if (op != NULL && *op != '=')
2948 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002949 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002952 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002953 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 if ((di == NULL
2957 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2958 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2959 FALSE)))
2960 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 set_var(lp->ll_name, &tv, FALSE);
2962 clear_tv(&tv);
2963 }
2964 }
2965 else
2966 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 else if (tv_check_lock(lp->ll_newkey == NULL
2971 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002972 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 else if (lp->ll_range)
2975 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 listitem_T *ll_li = lp->ll_li;
2977 int ll_n1 = lp->ll_n1;
2978
2979 /*
2980 * Check whether any of the list items is locked
2981 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002982 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002983 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002984 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 return;
2986 ri = ri->li_next;
2987 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2988 break;
2989 ll_li = ll_li->li_next;
2990 ++ll_n1;
2991 }
2992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /*
2994 * Assign the List values to the list items.
2995 */
2996 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 if (op != NULL && *op != '=')
2999 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3000 else
3001 {
3002 clear_tv(&lp->ll_li->li_tv);
3003 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 ri = ri->li_next;
3006 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3007 break;
3008 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003011 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 ri = NULL;
3014 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 lp->ll_li = lp->ll_li->li_next;
3018 ++lp->ll_n1;
3019 }
3020 if (ri != NULL)
3021 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 else if (lp->ll_empty2
3023 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 : lp->ll_n1 != lp->ll_n2)
3025 EMSG(_("E711: List value has not enough items"));
3026 }
3027 else
3028 {
3029 /*
3030 * Assign to a List or Dictionary item.
3031 */
3032 if (lp->ll_newkey != NULL)
3033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 if (op != NULL && *op != '=')
3035 {
3036 EMSG2(_(e_letwrong), op);
3037 return;
3038 }
3039
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 if (di == NULL)
3043 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3045 {
3046 vim_free(di);
3047 return;
3048 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 else if (op != NULL && *op != '=')
3052 {
3053 tv_op(lp->ll_tv, rettv, op);
3054 return;
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003058
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 /*
3060 * Assign the value to the variable or list item.
3061 */
3062 if (copy)
3063 copy_tv(rettv, lp->ll_tv);
3064 else
3065 {
3066 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003067 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 }
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071}
3072
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003073/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3075 * Returns OK or FAIL.
3076 */
3077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079{
3080 long n;
3081 char_u numbuf[NUMBUFLEN];
3082 char_u *s;
3083
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003084 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3085 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3086 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 {
3088 switch (tv1->v_type)
3089 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003090 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 case VAR_DICT:
3092 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003093 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003094 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003095 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 break;
3097
3098 case VAR_LIST:
3099 if (*op != '+' || tv2->v_type != VAR_LIST)
3100 break;
3101 /* List += List */
3102 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3103 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3104 return OK;
3105
3106 case VAR_NUMBER:
3107 case VAR_STRING:
3108 if (tv2->v_type == VAR_LIST)
3109 break;
3110 if (*op == '+' || *op == '-')
3111 {
3112 /* nr += nr or nr -= nr*/
3113 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114#ifdef FEAT_FLOAT
3115 if (tv2->v_type == VAR_FLOAT)
3116 {
3117 float_T f = n;
3118
3119 if (*op == '+')
3120 f += tv2->vval.v_float;
3121 else
3122 f -= tv2->vval.v_float;
3123 clear_tv(tv1);
3124 tv1->v_type = VAR_FLOAT;
3125 tv1->vval.v_float = f;
3126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128#endif
3129 {
3130 if (*op == '+')
3131 n += get_tv_number(tv2);
3132 else
3133 n -= get_tv_number(tv2);
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_NUMBER;
3136 tv1->vval.v_number = n;
3137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003138 }
3139 else
3140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 if (tv2->v_type == VAR_FLOAT)
3142 break;
3143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 /* str .= str */
3145 s = get_tv_string(tv1);
3146 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3147 clear_tv(tv1);
3148 tv1->v_type = VAR_STRING;
3149 tv1->vval.v_string = s;
3150 }
3151 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152
3153#ifdef FEAT_FLOAT
3154 case VAR_FLOAT:
3155 {
3156 float_T f;
3157
3158 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3159 && tv2->v_type != VAR_NUMBER
3160 && tv2->v_type != VAR_STRING))
3161 break;
3162 if (tv2->v_type == VAR_FLOAT)
3163 f = tv2->vval.v_float;
3164 else
3165 f = get_tv_number(tv2);
3166 if (*op == '+')
3167 tv1->vval.v_float += f;
3168 else
3169 tv1->vval.v_float -= f;
3170 }
3171 return OK;
3172#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003173 }
3174 }
3175
3176 EMSG2(_(e_letwrong), op);
3177 return FAIL;
3178}
3179
3180/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 * Add a watcher to a list.
3182 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003184list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185{
3186 lw->lw_next = l->lv_watch;
3187 l->lv_watch = lw;
3188}
3189
3190/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003191 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 * No warning when it isn't found...
3193 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003195list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198
3199 lwp = &l->lv_watch;
3200 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3201 {
3202 if (lw == lwrem)
3203 {
3204 *lwp = lw->lw_next;
3205 break;
3206 }
3207 lwp = &lw->lw_next;
3208 }
3209}
3210
3211/*
3212 * Just before removing an item from a list: advance watchers to the next
3213 * item.
3214 */
3215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217{
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3221 if (lw->lw_item == item)
3222 lw->lw_item = item->li_next;
3223}
3224
3225/*
3226 * Evaluate the expression used in a ":for var in expr" command.
3227 * "arg" points to "var".
3228 * Set "*errp" to TRUE for an error, FALSE otherwise;
3229 * Return a pointer that holds the info. Null when there is an error.
3230 */
3231 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232eval_for_line(
3233 char_u *arg,
3234 int *errp,
3235 char_u **nextcmdp,
3236 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 typval_T tv;
3241 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242
3243 *errp = TRUE; /* default: there is an error */
3244
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 if (fi == NULL)
3247 return NULL;
3248
3249 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3250 if (expr == NULL)
3251 return fi;
3252
3253 expr = skipwhite(expr);
3254 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3255 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003256 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 return fi;
3258 }
3259
3260 if (skip)
3261 ++emsg_skip;
3262 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3263 {
3264 *errp = FALSE;
3265 if (!skip)
3266 {
3267 l = tv.vval.v_list;
3268 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 clear_tv(&tv);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 else
3274 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003275 /* No need to increment the refcount, it's already set for the
3276 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 fi->fi_list = l;
3278 list_add_watch(l, &fi->fi_lw);
3279 fi->fi_lw.lw_item = l->lv_first;
3280 }
3281 }
3282 }
3283 if (skip)
3284 --emsg_skip;
3285
3286 return fi;
3287}
3288
3289/*
3290 * Use the first item in a ":for" list. Advance to the next.
3291 * Assign the values to the variable (list). "arg" points to the first one.
3292 * Return TRUE when a valid item was found, FALSE when at end of list or
3293 * something wrong.
3294 */
3295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003296next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003298 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301
3302 item = fi->fi_lw.lw_item;
3303 if (item == NULL)
3304 result = FALSE;
3305 else
3306 {
3307 fi->fi_lw.lw_item = item->li_next;
3308 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3309 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3310 }
3311 return result;
3312}
3313
3314/*
3315 * Free the structure used to store info used by ":for".
3316 */
3317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333set_context_for_expression(
3334 expand_T *xp,
3335 char_u *arg,
3336 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003446ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 char_u *arg = eap->arg;
3449 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003451 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 linenr_T lnum;
3455 int doesrange;
3456 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 if (eap->skip)
3460 {
3461 /* trans_function_name() doesn't work well when skipping, use eval0()
3462 * instead to skip to any following command, e.g. for:
3463 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003464 ++emsg_skip;
3465 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3466 clear_tv(&rettv);
3467 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003468 return;
3469 }
3470
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003471 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003472 if (fudi.fd_newkey != NULL)
3473 {
3474 /* Still need to give an error message for missing key. */
3475 EMSG2(_(e_dictkey), fudi.fd_newkey);
3476 vim_free(fudi.fd_newkey);
3477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 if (tofree == NULL)
3479 return;
3480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003481 /* Increase refcount on dictionary, it could get deleted when evaluating
3482 * the arguments. */
3483 if (fudi.fd_dict != NULL)
3484 ++fudi.fd_dict->dv_refcount;
3485
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003488 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar532c7802005-01-27 14:44:31 +00003490 /* Skip white space to allow ":call func ()". Not good, but required for
3491 * backward compatibility. */
3492 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 if (*startarg != '(')
3496 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003497 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 goto end;
3499 }
3500
3501 /*
3502 * When skipping, evaluate the function once, to find the end of the
3503 * arguments.
3504 * When the function takes a range, this is discovered after the first
3505 * call, and the loop is broken.
3506 */
3507 if (eap->skip)
3508 {
3509 ++emsg_skip;
3510 lnum = eap->line2; /* do it once, also with an invalid range */
3511 }
3512 else
3513 lnum = eap->line1;
3514 for ( ; lnum <= eap->line2; ++lnum)
3515 {
3516 if (!eap->skip && eap->addr_count > 0)
3517 {
3518 curwin->w_cursor.lnum = lnum;
3519 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003520#ifdef FEAT_VIRTUALEDIT
3521 curwin->w_cursor.coladd = 0;
3522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003525 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003526 eap->line1, eap->line2, &doesrange,
3527 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 failed = TRUE;
3530 break;
3531 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003532
3533 /* Handle a function returning a Funcref, Dictionary or List. */
3534 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3535 {
3536 failed = TRUE;
3537 break;
3538 }
3539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (doesrange || eap->skip)
3542 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (aborting())
3549 break;
3550 }
3551 if (eap->skip)
3552 --emsg_skip;
3553
3554 if (!failed)
3555 {
3556 /* Check for trailing illegal characters and a following command. */
3557 if (!ends_excmd(*arg))
3558 {
3559 emsg_severe = TRUE;
3560 EMSG(_(e_trailing));
3561 }
3562 else
3563 eap->nextcmd = check_nextcmd(arg);
3564 }
3565
3566end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003567 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
3571/*
3572 * ":unlet[!] var1 ... " command.
3573 */
3574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 ex_unletlock(eap, eap->arg, 0);
3578}
3579
3580/*
3581 * ":lockvar" and ":unlockvar" commands
3582 */
3583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 int deep = 2;
3588
3589 if (eap->forceit)
3590 deep = -1;
3591 else if (vim_isdigit(*arg))
3592 {
3593 deep = getdigits(&arg);
3594 arg = skipwhite(arg);
3595 }
3596
3597 ex_unletlock(eap, arg, deep);
3598}
3599
3600/*
3601 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3602 */
3603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003604ex_unletlock(
3605 exarg_T *eap,
3606 char_u *argstart,
3607 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608{
3609 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 do
3615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003617 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003618 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lv.ll_name == NULL)
3620 error = TRUE; /* error but continue parsing */
3621 if (name_end == NULL || (!vim_iswhite(*name_end)
3622 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (name_end != NULL)
3625 {
3626 emsg_severe = TRUE;
3627 EMSG(_(e_trailing));
3628 }
3629 if (!(eap->skip || error))
3630 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
3632 }
3633
3634 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 {
3636 if (eap->cmdidx == CMD_unlet)
3637 {
3638 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3639 error = TRUE;
3640 }
3641 else
3642 {
3643 if (do_lock_var(&lv, name_end, deep,
3644 eap->cmdidx == CMD_lockvar) == FAIL)
3645 error = TRUE;
3646 }
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 if (!eap->skip)
3650 clear_lval(&lv);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 arg = skipwhite(name_end);
3653 } while (!ends_excmd(*arg));
3654
3655 eap->nextcmd = check_nextcmd(arg);
3656}
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659do_unlet_var(
3660 lval_T *lp,
3661 char_u *name_end,
3662 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 int ret = OK;
3665 int cc;
3666
3667 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 cc = *name_end;
3670 *name_end = NUL;
3671
3672 /* Normal name or expanded name. */
3673 if (check_changedtick(lp->ll_name))
3674 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003679 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 else if (lp->ll_range)
3685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003688 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689
3690 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3691 {
3692 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003694 return FAIL;
3695 ll_li = li;
3696 ++ll_n1;
3697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698
3699 /* Delete a range of List items. */
3700 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 li = lp->ll_li->li_next;
3703 listitem_remove(lp->ll_list, lp->ll_li);
3704 lp->ll_li = li;
3705 ++lp->ll_n1;
3706 }
3707 }
3708 else
3709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a List item. */
3712 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003715 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 }
3717
3718 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719}
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721/*
3722 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003726do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaar33570922005-01-25 22:26:29 +00003728 hashtab_T *ht;
3729 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003730 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003731 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003732 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 ht = find_var_ht(name, &varname);
3735 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003737 if (ht == &globvarht)
3738 d = &globvardict;
3739 else if (current_funccal != NULL
3740 && ht == &current_funccal->l_vars.dv_hashtab)
3741 d = &current_funccal->l_vars;
3742 else if (ht == &compat_hashtab)
3743 d = &vimvardict;
3744 else
3745 {
3746 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3747 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3748 }
3749 if (d == NULL)
3750 {
3751 EMSG2(_(e_intern2), "do_unlet()");
3752 return FAIL;
3753 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 hi = hash_find(ht, varname);
3755 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003757 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003758 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003759 || var_check_ro(di->di_flags, name, FALSE)
3760 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003761 return FAIL;
3762
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003763 delete_var(ht, hi);
3764 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767 if (forceit)
3768 return OK;
3769 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return FAIL;
3771}
3772
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773/*
3774 * Lock or unlock variable indicated by "lp".
3775 * "deep" is the levels to go (-1 for unlimited);
3776 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3777 */
3778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779do_lock_var(
3780 lval_T *lp,
3781 char_u *name_end,
3782 int deep,
3783 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784{
3785 int ret = OK;
3786 int cc;
3787 dictitem_T *di;
3788
3789 if (deep == 0) /* nothing to do */
3790 return OK;
3791
3792 if (lp->ll_tv == NULL)
3793 {
3794 cc = *name_end;
3795 *name_end = NUL;
3796
3797 /* Normal name or expanded name. */
3798 if (check_changedtick(lp->ll_name))
3799 ret = FAIL;
3800 else
3801 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003802 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003803 if (di == NULL)
3804 ret = FAIL;
3805 else
3806 {
3807 if (lock)
3808 di->di_flags |= DI_FLAGS_LOCK;
3809 else
3810 di->di_flags &= ~DI_FLAGS_LOCK;
3811 item_lock(&di->di_tv, deep, lock);
3812 }
3813 }
3814 *name_end = cc;
3815 }
3816 else if (lp->ll_range)
3817 {
3818 listitem_T *li = lp->ll_li;
3819
3820 /* (un)lock a range of List items. */
3821 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3822 {
3823 item_lock(&li->li_tv, deep, lock);
3824 li = li->li_next;
3825 ++lp->ll_n1;
3826 }
3827 }
3828 else if (lp->ll_list != NULL)
3829 /* (un)lock a List item. */
3830 item_lock(&lp->ll_li->li_tv, deep, lock);
3831 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003832 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 item_lock(&lp->ll_di->di_tv, deep, lock);
3834
3835 return ret;
3836}
3837
3838/*
3839 * Lock or unlock an item. "deep" is nr of levels to go.
3840 */
3841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003842item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843{
3844 static int recurse = 0;
3845 list_T *l;
3846 listitem_T *li;
3847 dict_T *d;
3848 hashitem_T *hi;
3849 int todo;
3850
3851 if (recurse >= DICT_MAXNEST)
3852 {
3853 EMSG(_("E743: variable nested too deep for (un)lock"));
3854 return;
3855 }
3856 if (deep == 0)
3857 return;
3858 ++recurse;
3859
3860 /* lock/unlock the item itself */
3861 if (lock)
3862 tv->v_lock |= VAR_LOCKED;
3863 else
3864 tv->v_lock &= ~VAR_LOCKED;
3865
3866 switch (tv->v_type)
3867 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003868 case VAR_UNKNOWN:
3869 case VAR_NUMBER:
3870 case VAR_STRING:
3871 case VAR_FUNC:
3872 case VAR_FLOAT:
3873 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003875 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 break;
3877
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003878 case VAR_LIST:
3879 if ((l = tv->vval.v_list) != NULL)
3880 {
3881 if (lock)
3882 l->lv_lock |= VAR_LOCKED;
3883 else
3884 l->lv_lock &= ~VAR_LOCKED;
3885 if (deep < 0 || deep > 1)
3886 /* recursive: lock/unlock the items the List contains */
3887 for (li = l->lv_first; li != NULL; li = li->li_next)
3888 item_lock(&li->li_tv, deep - 1, lock);
3889 }
3890 break;
3891 case VAR_DICT:
3892 if ((d = tv->vval.v_dict) != NULL)
3893 {
3894 if (lock)
3895 d->dv_lock |= VAR_LOCKED;
3896 else
3897 d->dv_lock &= ~VAR_LOCKED;
3898 if (deep < 0 || deep > 1)
3899 {
3900 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003901 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003902 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3903 {
3904 if (!HASHITEM_EMPTY(hi))
3905 {
3906 --todo;
3907 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3908 }
3909 }
3910 }
3911 }
3912 }
3913 --recurse;
3914}
3915
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003917 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3918 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919 */
3920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003921tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003922{
3923 return (tv->v_lock & VAR_LOCKED)
3924 || (tv->v_type == VAR_LIST
3925 && tv->vval.v_list != NULL
3926 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3927 || (tv->v_type == VAR_DICT
3928 && tv->vval.v_dict != NULL
3929 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3930}
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3933/*
3934 * Delete all "menutrans_" variables.
3935 */
3936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003937del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003943 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 {
3946 if (!HASHITEM_EMPTY(hi))
3947 {
3948 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3950 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 }
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954}
3955#endif
3956
3957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3958
3959/*
3960 * Local string buffer for the next two functions to store a variable name
3961 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3962 * get_user_var_name().
3963 */
3964
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003965static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967static char_u *varnamebuf = NULL;
3968static int varnamebuflen = 0;
3969
3970/*
3971 * Function to concatenate a prefix and a variable name.
3972 */
3973 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003974cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 int len;
3977
3978 len = (int)STRLEN(name) + 3;
3979 if (len > varnamebuflen)
3980 {
3981 vim_free(varnamebuf);
3982 len += 10; /* some additional space */
3983 varnamebuf = alloc(len);
3984 if (varnamebuf == NULL)
3985 {
3986 varnamebuflen = 0;
3987 return NULL;
3988 }
3989 varnamebuflen = len;
3990 }
3991 *varnamebuf = prefix;
3992 varnamebuf[1] = ':';
3993 STRCPY(varnamebuf + 2, name);
3994 return varnamebuf;
3995}
3996
3997/*
3998 * Function given to ExpandGeneric() to obtain the list of user defined
3999 * (global/buffer/window/built-in) variable names.
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004004 static long_u gdone;
4005 static long_u bdone;
4006 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007#ifdef FEAT_WINDOWS
4008 static long_u tdone;
4009#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004010 static int vidx;
4011 static hashitem_T *hi;
4012 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017#ifdef FEAT_WINDOWS
4018 tdone = 0;
4019#endif
4020 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004021
4022 /* Global variables */
4023 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 else
4028 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 while (HASHITEM_EMPTY(hi))
4030 ++hi;
4031 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4032 return cat_prefix_varname('g', hi->hi_key);
4033 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035
4036 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004037 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 else
4043 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004044 while (HASHITEM_EMPTY(hi))
4045 ++hi;
4046 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return (char_u *)"b:changedtick";
4052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
4054 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004055 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004058 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004060 else
4061 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004062 while (HASHITEM_EMPTY(hi))
4063 ++hi;
4064 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067#ifdef FEAT_WINDOWS
4068 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004069 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070 if (tdone < ht->ht_used)
4071 {
4072 if (tdone++ == 0)
4073 hi = ht->ht_array;
4074 else
4075 ++hi;
4076 while (HASHITEM_EMPTY(hi))
4077 ++hi;
4078 return cat_prefix_varname('t', hi->hi_key);
4079 }
4080#endif
4081
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 /* v: variables */
4083 if (vidx < VV_LEN)
4084 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 vim_free(varnamebuf);
4087 varnamebuf = NULL;
4088 varnamebuflen = 0;
4089 return NULL;
4090}
4091
4092#endif /* FEAT_CMDL_COMPL */
4093
4094/*
4095 * types for expressions.
4096 */
4097typedef enum
4098{
4099 TYPE_UNKNOWN = 0
4100 , TYPE_EQUAL /* == */
4101 , TYPE_NEQUAL /* != */
4102 , TYPE_GREATER /* > */
4103 , TYPE_GEQUAL /* >= */
4104 , TYPE_SMALLER /* < */
4105 , TYPE_SEQUAL /* <= */
4106 , TYPE_MATCH /* =~ */
4107 , TYPE_NOMATCH /* !~ */
4108} exptype_T;
4109
4110/*
4111 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4114 */
4115
4116/*
4117 * Handle zero level expression.
4118 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004120 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * Return OK or FAIL.
4122 */
4123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004124eval0(
4125 char_u *arg,
4126 typval_T *rettv,
4127 char_u **nextcmd,
4128 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int ret;
4131 char_u *p;
4132
4133 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (ret == FAIL || !ends_excmd(*p))
4136 {
4137 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /*
4140 * Report the invalid expression unless the expression evaluation has
4141 * been cancelled due to an aborting error, an interrupt, or an
4142 * exception.
4143 */
4144 if (!aborting())
4145 EMSG2(_(e_invexpr2), arg);
4146 ret = FAIL;
4147 }
4148 if (nextcmd != NULL)
4149 *nextcmd = check_nextcmd(p);
4150
4151 return ret;
4152}
4153
4154/*
4155 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004156 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 *
4158 * "arg" must point to the first non-white of the expression.
4159 * "arg" is advanced to the next non-white after the recognized expression.
4160 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004161 * Note: "rettv.v_lock" is not set.
4162 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * Return OK or FAIL.
4164 */
4165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004166eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 if ((*arg)[0] == '?')
4178 {
4179 result = FALSE;
4180 if (evaluate)
4181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 int error = FALSE;
4183
4184 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190
4191 /*
4192 * Get the second variable.
4193 */
4194 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197
4198 /*
4199 * Check for the ":".
4200 */
4201 if ((*arg)[0] != ':')
4202 {
4203 EMSG(_("E109: Missing ':' after '?'"));
4204 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207 }
4208
4209 /*
4210 * Get the third variable.
4211 */
4212 *arg = skipwhite(*arg + 1);
4213 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4214 {
4215 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223 return OK;
4224}
4225
4226/*
4227 * Handle first level expression:
4228 * expr2 || expr2 || expr2 logical OR
4229 *
4230 * "arg" must point to the first non-white of the expression.
4231 * "arg" is advanced to the next non-white after the recognized expression.
4232 *
4233 * Return OK or FAIL.
4234 */
4235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004236eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaar33570922005-01-25 22:26:29 +00004238 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 long result;
4240 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 /*
4244 * Get the first variable.
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
4248
4249 /*
4250 * Repeat until there is no following "||".
4251 */
4252 first = TRUE;
4253 result = FALSE;
4254 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4255 {
4256 if (evaluate && first)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 first = FALSE;
4264 }
4265
4266 /*
4267 * Get the second variable.
4268 */
4269 *arg = skipwhite(*arg + 2);
4270 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4271 return FAIL;
4272
4273 /*
4274 * Compute the result.
4275 */
4276 if (evaluate && !result)
4277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (error)
4282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 if (evaluate)
4285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 rettv->v_type = VAR_NUMBER;
4287 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 }
4290
4291 return OK;
4292}
4293
4294/*
4295 * Handle second level expression:
4296 * expr3 && expr3 && expr3 logical AND
4297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004304eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 long result;
4308 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 /*
4318 * Repeat until there is no following "&&".
4319 */
4320 first = TRUE;
4321 result = TRUE;
4322 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4323 {
4324 if (evaluate && first)
4325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 first = FALSE;
4332 }
4333
4334 /*
4335 * Get the second variable.
4336 */
4337 *arg = skipwhite(*arg + 2);
4338 if (eval4(arg, &var2, evaluate && result) == FAIL)
4339 return FAIL;
4340
4341 /*
4342 * Compute the result.
4343 */
4344 if (evaluate && result)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (error)
4350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 if (evaluate)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle third level expression:
4364 * var1 == var2
4365 * var1 =~ var2
4366 * var1 != var2
4367 * var1 !~ var2
4368 * var1 > var2
4369 * var1 >= var2
4370 * var1 < var2
4371 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 * var1 is var2
4373 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 char_u *p;
4385 int i;
4386 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 int len = 2;
4389 long n1, n2;
4390 char_u *s1, *s2;
4391 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4392 regmatch_T regmatch;
4393 int ic;
4394 char_u *save_cpo;
4395
4396 /*
4397 * Get the first variable.
4398 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
4401
4402 p = *arg;
4403 switch (p[0])
4404 {
4405 case '=': if (p[1] == '=')
4406 type = TYPE_EQUAL;
4407 else if (p[1] == '~')
4408 type = TYPE_MATCH;
4409 break;
4410 case '!': if (p[1] == '=')
4411 type = TYPE_NEQUAL;
4412 else if (p[1] == '~')
4413 type = TYPE_NOMATCH;
4414 break;
4415 case '>': if (p[1] != '=')
4416 {
4417 type = TYPE_GREATER;
4418 len = 1;
4419 }
4420 else
4421 type = TYPE_GEQUAL;
4422 break;
4423 case '<': if (p[1] != '=')
4424 {
4425 type = TYPE_SMALLER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_SEQUAL;
4430 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 case 'i': if (p[1] == 's')
4432 {
4433 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4434 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004435 i = p[len];
4436 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004437 {
4438 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4439 type_is = TRUE;
4440 }
4441 }
4442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444
4445 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 */
4448 if (type != TYPE_UNKNOWN)
4449 {
4450 /* extra question mark appended: ignore case */
4451 if (p[len] == '?')
4452 {
4453 ic = TRUE;
4454 ++len;
4455 }
4456 /* extra '#' appended: match case */
4457 else if (p[len] == '#')
4458 {
4459 ic = FALSE;
4460 ++len;
4461 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004462 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 else
4464 ic = p_ic;
4465
4466 /*
4467 * Get the second variable.
4468 */
4469 *arg = skipwhite(p + len);
4470 if (eval5(arg, &var2, evaluate) == FAIL)
4471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 return FAIL;
4474 }
4475
4476 if (evaluate)
4477 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 if (type_is && rettv->v_type != var2.v_type)
4479 {
4480 /* For "is" a different type always means FALSE, for "notis"
4481 * it means TRUE. */
4482 n1 = (type == TYPE_NEQUAL);
4483 }
4484 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4485 {
4486 if (type_is)
4487 {
4488 n1 = (rettv->v_type == var2.v_type
4489 && rettv->vval.v_list == var2.vval.v_list);
4490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 else if (rettv->v_type != var2.v_type
4494 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4495 {
4496 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004497 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004499 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 return FAIL;
4503 }
4504 else
4505 {
4506 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004507 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4508 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 if (type == TYPE_NEQUAL)
4510 n1 = !n1;
4511 }
4512 }
4513
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004514 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4515 {
4516 if (type_is)
4517 {
4518 n1 = (rettv->v_type == var2.v_type
4519 && rettv->vval.v_dict == var2.vval.v_dict);
4520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 else if (rettv->v_type != var2.v_type
4524 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4525 {
4526 if (rettv->v_type != var2.v_type)
4527 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4528 else
4529 EMSG(_("E736: Invalid operation for Dictionary"));
4530 clear_tv(rettv);
4531 clear_tv(&var2);
4532 return FAIL;
4533 }
4534 else
4535 {
4536 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004537 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4538 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004539 if (type == TYPE_NEQUAL)
4540 n1 = !n1;
4541 }
4542 }
4543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4545 {
4546 if (rettv->v_type != var2.v_type
4547 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4548 {
4549 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004550 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 clear_tv(rettv);
4554 clear_tv(&var2);
4555 return FAIL;
4556 }
4557 else
4558 {
4559 /* Compare two Funcrefs for being equal or unequal. */
4560 if (rettv->vval.v_string == NULL
4561 || var2.vval.v_string == NULL)
4562 n1 = FALSE;
4563 else
4564 n1 = STRCMP(rettv->vval.v_string,
4565 var2.vval.v_string) == 0;
4566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 }
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571#ifdef FEAT_FLOAT
4572 /*
4573 * If one of the two variables is a float, compare as a float.
4574 * When using "=~" or "!~", always compare as string.
4575 */
4576 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4577 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4578 {
4579 float_T f1, f2;
4580
4581 if (rettv->v_type == VAR_FLOAT)
4582 f1 = rettv->vval.v_float;
4583 else
4584 f1 = get_tv_number(rettv);
4585 if (var2.v_type == VAR_FLOAT)
4586 f2 = var2.vval.v_float;
4587 else
4588 f2 = get_tv_number(&var2);
4589 n1 = FALSE;
4590 switch (type)
4591 {
4592 case TYPE_EQUAL: n1 = (f1 == f2); break;
4593 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4594 case TYPE_GREATER: n1 = (f1 > f2); break;
4595 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4596 case TYPE_SMALLER: n1 = (f1 < f2); break;
4597 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4598 case TYPE_UNKNOWN:
4599 case TYPE_MATCH:
4600 case TYPE_NOMATCH: break; /* avoid gcc warning */
4601 }
4602 }
4603#endif
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /*
4606 * If one of the two variables is a number, compare as a number.
4607 * When using "=~" or "!~", always compare as string.
4608 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 n1 = get_tv_number(rettv);
4613 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 switch (type)
4615 {
4616 case TYPE_EQUAL: n1 = (n1 == n2); break;
4617 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4618 case TYPE_GREATER: n1 = (n1 > n2); break;
4619 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4620 case TYPE_SMALLER: n1 = (n1 < n2); break;
4621 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4622 case TYPE_UNKNOWN:
4623 case TYPE_MATCH:
4624 case TYPE_NOMATCH: break; /* avoid gcc warning */
4625 }
4626 }
4627 else
4628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 s1 = get_tv_string_buf(rettv, buf1);
4630 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4632 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4633 else
4634 i = 0;
4635 n1 = FALSE;
4636 switch (type)
4637 {
4638 case TYPE_EQUAL: n1 = (i == 0); break;
4639 case TYPE_NEQUAL: n1 = (i != 0); break;
4640 case TYPE_GREATER: n1 = (i > 0); break;
4641 case TYPE_GEQUAL: n1 = (i >= 0); break;
4642 case TYPE_SMALLER: n1 = (i < 0); break;
4643 case TYPE_SEQUAL: n1 = (i <= 0); break;
4644
4645 case TYPE_MATCH:
4646 case TYPE_NOMATCH:
4647 /* avoid 'l' flag in 'cpoptions' */
4648 save_cpo = p_cpo;
4649 p_cpo = (char_u *)"";
4650 regmatch.regprog = vim_regcomp(s2,
4651 RE_MAGIC + RE_STRING);
4652 regmatch.rm_ic = ic;
4653 if (regmatch.regprog != NULL)
4654 {
4655 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004656 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (type == TYPE_NOMATCH)
4658 n1 = !n1;
4659 }
4660 p_cpo = save_cpo;
4661 break;
4662
4663 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4664 }
4665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
4667 clear_tv(&var2);
4668 rettv->v_type = VAR_NUMBER;
4669 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671 }
4672
4673 return OK;
4674}
4675
4676/*
4677 * Handle fourth level expression:
4678 * + number addition
4679 * - number subtraction
4680 * . string concatenation
4681 *
4682 * "arg" must point to the first non-white of the expression.
4683 * "arg" is advanced to the next non-white after the recognized expression.
4684 *
4685 * Return OK or FAIL.
4686 */
4687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004688eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaar33570922005-01-25 22:26:29 +00004690 typval_T var2;
4691 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 int op;
4693 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694#ifdef FEAT_FLOAT
4695 float_T f1 = 0, f2 = 0;
4696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char_u *s1, *s2;
4698 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4699 char_u *p;
4700
4701 /*
4702 * Get the first variable.
4703 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004704 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706
4707 /*
4708 * Repeat computing, until no '+', '-' or '.' is following.
4709 */
4710 for (;;)
4711 {
4712 op = **arg;
4713 if (op != '+' && op != '-' && op != '.')
4714 break;
4715
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 if ((op != '+' || rettv->v_type != VAR_LIST)
4717#ifdef FEAT_FLOAT
4718 && (op == '.' || rettv->v_type != VAR_FLOAT)
4719#endif
4720 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
4722 /* For "list + ...", an illegal use of the first operand as
4723 * a number cannot be determined before evaluating the 2nd
4724 * operand: if this is also a list, all is ok.
4725 * For "something . ...", "something - ..." or "non-list + ...",
4726 * we know that the first operand needs to be a string or number
4727 * without evaluating the 2nd operand. So check before to avoid
4728 * side effects after an error. */
4729 if (evaluate && get_tv_string_chk(rettv) == NULL)
4730 {
4731 clear_tv(rettv);
4732 return FAIL;
4733 }
4734 }
4735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /*
4737 * Get the second variable.
4738 */
4739 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004740 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744 }
4745
4746 if (evaluate)
4747 {
4748 /*
4749 * Compute the result.
4750 */
4751 if (op == '.')
4752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4754 s2 = get_tv_string_buf_chk(&var2, buf2);
4755 if (s2 == NULL) /* type error ? */
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004761 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 clear_tv(rettv);
4763 rettv->v_type = VAR_STRING;
4764 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004766 else if (op == '+' && rettv->v_type == VAR_LIST
4767 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004768 {
4769 /* concatenate Lists */
4770 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4771 &var3) == FAIL)
4772 {
4773 clear_tv(rettv);
4774 clear_tv(&var2);
4775 return FAIL;
4776 }
4777 clear_tv(rettv);
4778 *rettv = var3;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
4781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 int error = FALSE;
4783
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784#ifdef FEAT_FLOAT
4785 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 f1 = rettv->vval.v_float;
4788 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 else
4791#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 n1 = get_tv_number_chk(rettv, &error);
4794 if (error)
4795 {
4796 /* This can only happen for "list + non-list". For
4797 * "non-list + ..." or "something - ...", we returned
4798 * before evaluating the 2nd operand. */
4799 clear_tv(rettv);
4800 return FAIL;
4801 }
4802#ifdef FEAT_FLOAT
4803 if (var2.v_type == VAR_FLOAT)
4804 f1 = n1;
4805#endif
4806 }
4807#ifdef FEAT_FLOAT
4808 if (var2.v_type == VAR_FLOAT)
4809 {
4810 f2 = var2.vval.v_float;
4811 n2 = 0;
4812 }
4813 else
4814#endif
4815 {
4816 n2 = get_tv_number_chk(&var2, &error);
4817 if (error)
4818 {
4819 clear_tv(rettv);
4820 clear_tv(&var2);
4821 return FAIL;
4822 }
4823#ifdef FEAT_FLOAT
4824 if (rettv->v_type == VAR_FLOAT)
4825 f2 = n2;
4826#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829
4830#ifdef FEAT_FLOAT
4831 /* If there is a float on either side the result is a float. */
4832 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4833 {
4834 if (op == '+')
4835 f1 = f1 + f2;
4836 else
4837 f1 = f1 - f2;
4838 rettv->v_type = VAR_FLOAT;
4839 rettv->vval.v_float = f1;
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842#endif
4843 {
4844 if (op == '+')
4845 n1 = n1 + n2;
4846 else
4847 n1 = n1 - n2;
4848 rettv->v_type = VAR_NUMBER;
4849 rettv->vval.v_number = n1;
4850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855 return OK;
4856}
4857
4858/*
4859 * Handle fifth level expression:
4860 * * number multiplication
4861 * / number division
4862 * % number modulo
4863 *
4864 * "arg" must point to the first non-white of the expression.
4865 * "arg" is advanced to the next non-white after the recognized expression.
4866 *
4867 * Return OK or FAIL.
4868 */
4869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004870eval6(
4871 char_u **arg,
4872 typval_T *rettv,
4873 int evaluate,
4874 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int op;
4878 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879#ifdef FEAT_FLOAT
4880 int use_float = FALSE;
4881 float_T f1 = 0, f2;
4882#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004883 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /*
4886 * Get the first variable.
4887 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
4890
4891 /*
4892 * Repeat computing, until no '*', '/' or '%' is following.
4893 */
4894 for (;;)
4895 {
4896 op = **arg;
4897 if (op != '*' && op != '/' && op != '%')
4898 break;
4899
4900 if (evaluate)
4901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902#ifdef FEAT_FLOAT
4903 if (rettv->v_type == VAR_FLOAT)
4904 {
4905 f1 = rettv->vval.v_float;
4906 use_float = TRUE;
4907 n1 = 0;
4908 }
4909 else
4910#endif
4911 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 if (error)
4914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 else
4917 n1 = 0;
4918
4919 /*
4920 * Get the second variable.
4921 */
4922 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004923 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return FAIL;
4925
4926 if (evaluate)
4927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#ifdef FEAT_FLOAT
4929 if (var2.v_type == VAR_FLOAT)
4930 {
4931 if (!use_float)
4932 {
4933 f1 = n1;
4934 use_float = TRUE;
4935 }
4936 f2 = var2.vval.v_float;
4937 n2 = 0;
4938 }
4939 else
4940#endif
4941 {
4942 n2 = get_tv_number_chk(&var2, &error);
4943 clear_tv(&var2);
4944 if (error)
4945 return FAIL;
4946#ifdef FEAT_FLOAT
4947 if (use_float)
4948 f2 = n2;
4949#endif
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 /*
4953 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#ifdef FEAT_FLOAT
4957 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959 if (op == '*')
4960 f1 = f1 * f2;
4961 else if (op == '/')
4962 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004963# ifdef VMS
4964 /* VMS crashes on divide by zero, work around it */
4965 if (f2 == 0.0)
4966 {
4967 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004968 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 }
4974 else
4975 f1 = f1 / f2;
4976# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 /* We rely on the floating point library to handle divide
4978 * by zero to result in "inf" and not a crash. */
4979 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004984 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 return FAIL;
4986 }
4987 rettv->v_type = VAR_FLOAT;
4988 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 if (op == '*')
4994 n1 = n1 * n2;
4995 else if (op == '/')
4996 {
4997 if (n2 == 0) /* give an error message? */
4998 {
4999 if (n1 == 0)
5000 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5001 else if (n1 < 0)
5002 n1 = -0x7fffffffL;
5003 else
5004 n1 = 0x7fffffffL;
5005 }
5006 else
5007 n1 = n1 / n2;
5008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
5011 if (n2 == 0) /* give an error message? */
5012 n1 = 0;
5013 else
5014 n1 = n1 % n2;
5015 }
5016 rettv->v_type = VAR_NUMBER;
5017 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 }
5021
5022 return OK;
5023}
5024
5025/*
5026 * Handle sixth level expression:
5027 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005028 * "string" string constant
5029 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 * &option-name option value
5031 * @r register contents
5032 * identifier variable value
5033 * function() function call
5034 * $VAR environment variable
5035 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005036 * [expr, expr] List
5037 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 *
5039 * Also handle:
5040 * ! in front logical NOT
5041 * - in front unary minus
5042 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 * trailing [] subscript in String or List
5044 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *
5046 * "arg" must point to the first non-white of the expression.
5047 * "arg" is advanced to the next non-white after the recognized expression.
5048 *
5049 * Return OK or FAIL.
5050 */
5051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005052eval7(
5053 char_u **arg,
5054 typval_T *rettv,
5055 int evaluate,
5056 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 long n;
5059 int len;
5060 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *start_leader, *end_leader;
5062 int ret = OK;
5063 char_u *alias;
5064
5065 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 /*
5072 * Skip '!' and '-' characters. They are handled later.
5073 */
5074 start_leader = *arg;
5075 while (**arg == '!' || **arg == '-' || **arg == '+')
5076 *arg = skipwhite(*arg + 1);
5077 end_leader = *arg;
5078
5079 switch (**arg)
5080 {
5081 /*
5082 * Number constant.
5083 */
5084 case '0':
5085 case '1':
5086 case '2':
5087 case '3':
5088 case '4':
5089 case '5':
5090 case '6':
5091 case '7':
5092 case '8':
5093 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 {
5095#ifdef FEAT_FLOAT
5096 char_u *p = skipdigits(*arg + 1);
5097 int get_float = FALSE;
5098
5099 /* We accept a float when the format matches
5100 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005101 * strict to avoid backwards compatibility problems.
5102 * Don't look for a float after the "." operator, so that
5103 * ":let vers = 1.2.3" doesn't fail. */
5104 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 get_float = TRUE;
5107 p = skipdigits(p + 2);
5108 if (*p == 'e' || *p == 'E')
5109 {
5110 ++p;
5111 if (*p == '-' || *p == '+')
5112 ++p;
5113 if (!vim_isdigit(*p))
5114 get_float = FALSE;
5115 else
5116 p = skipdigits(p + 1);
5117 }
5118 if (ASCII_ISALPHA(*p) || *p == '.')
5119 get_float = FALSE;
5120 }
5121 if (get_float)
5122 {
5123 float_T f;
5124
5125 *arg += string2float(*arg, &f);
5126 if (evaluate)
5127 {
5128 rettv->v_type = VAR_FLOAT;
5129 rettv->vval.v_float = f;
5130 }
5131 }
5132 else
5133#endif
5134 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005135 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005136 *arg += len;
5137 if (evaluate)
5138 {
5139 rettv->v_type = VAR_NUMBER;
5140 rettv->vval.v_number = n;
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 /*
5147 * String constant: "string".
5148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151
5152 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 break;
5157
5158 /*
5159 * List: [expr, expr]
5160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 * Dictionary: {key: val, key: val}
5166 */
5167 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5168 break;
5169
5170 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005171 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
5177 * Environment variable: $VAR.
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
5183 * Register contents: @r.
5184 */
5185 case '@': ++*arg;
5186 if (evaluate)
5187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005189 rettv->vval.v_string = get_reg_contents(**arg,
5190 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 if (**arg != NUL)
5193 ++*arg;
5194 break;
5195
5196 /*
5197 * nested expression: (expression).
5198 */
5199 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (**arg == ')')
5202 ++*arg;
5203 else if (ret == OK)
5204 {
5205 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 ret = FAIL;
5208 }
5209 break;
5210
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214
5215 if (ret == NOTDONE)
5216 {
5217 /*
5218 * Must be a variable or function name.
5219 * Can also be a curly-braces kind of name: {expr}.
5220 */
5221 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 if (alias != NULL)
5224 s = alias;
5225
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 ret = FAIL;
5228 else
5229 {
5230 if (**arg == '(') /* recursive! */
5231 {
5232 /* If "s" is the name of a variable of type VAR_FUNC
5233 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005234 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235
5236 /* Invoke the function. */
5237 ret = get_func_tv(s, len, rettv, arg,
5238 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005240
5241 /* If evaluate is FALSE rettv->v_type was not set in
5242 * get_func_tv, but it's needed in handle_subscript() to parse
5243 * what follows. So set it here. */
5244 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5245 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005246 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005247 rettv->v_type = VAR_FUNC;
5248 }
5249
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 /* Stop the expression evaluation when immediately
5251 * aborting on error, or when an interrupt occurred or
5252 * an exception was thrown but not caught. */
5253 if (aborting())
5254 {
5255 if (ret == OK)
5256 clear_tv(rettv);
5257 ret = FAIL;
5258 }
5259 }
5260 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005261 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005262 else
5263 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005265 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 *arg = skipwhite(*arg);
5269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005270 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5271 * expr(expr). */
5272 if (ret == OK)
5273 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 /*
5276 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5277 */
5278 if (ret == OK && evaluate && end_leader > start_leader)
5279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281 int val = 0;
5282#ifdef FEAT_FLOAT
5283 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 if (rettv->v_type == VAR_FLOAT)
5286 f = rettv->vval.v_float;
5287 else
5288#endif
5289 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 clear_tv(rettv);
5293 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 else
5296 {
5297 while (end_leader > start_leader)
5298 {
5299 --end_leader;
5300 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005301 {
5302#ifdef FEAT_FLOAT
5303 if (rettv->v_type == VAR_FLOAT)
5304 f = !f;
5305 else
5306#endif
5307 val = !val;
5308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005310 {
5311#ifdef FEAT_FLOAT
5312 if (rettv->v_type == VAR_FLOAT)
5313 f = -f;
5314 else
5315#endif
5316 val = -val;
5317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 {
5322 clear_tv(rettv);
5323 rettv->vval.v_float = f;
5324 }
5325 else
5326#endif
5327 {
5328 clear_tv(rettv);
5329 rettv->v_type = VAR_NUMBER;
5330 rettv->vval.v_number = val;
5331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334
5335 return ret;
5336}
5337
5338/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005339 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5340 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5342 */
5343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005344eval_index(
5345 char_u **arg,
5346 typval_T *rettv,
5347 int evaluate,
5348 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349{
5350 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 long len = -1;
5354 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357
Bram Moolenaara03f2332016-02-06 18:09:59 +01005358 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 case VAR_FUNC:
5361 if (verbose)
5362 EMSG(_("E695: Cannot index a Funcref"));
5363 return FAIL;
5364 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 if (verbose)
5367 EMSG(_(e_float_as_string));
5368 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005371 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005372 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 if (verbose)
5374 EMSG(_("E909: Cannot index a special variable"));
5375 return FAIL;
5376 case VAR_UNKNOWN:
5377 if (evaluate)
5378 return FAIL;
5379 /* FALLTHROUGH */
5380
5381 case VAR_STRING:
5382 case VAR_NUMBER:
5383 case VAR_LIST:
5384 case VAR_DICT:
5385 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005388 init_tv(&var1);
5389 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 /*
5393 * dict.name
5394 */
5395 key = *arg + 1;
5396 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5397 ;
5398 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 }
5402 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 /*
5405 * something[idx]
5406 *
5407 * Get the (first) variable from inside the [].
5408 */
5409 *arg = skipwhite(*arg + 1);
5410 if (**arg == ':')
5411 empty1 = TRUE;
5412 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5413 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005414 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5415 {
5416 /* not a number or string */
5417 clear_tv(&var1);
5418 return FAIL;
5419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420
5421 /*
5422 * Get the second variable from inside the [:].
5423 */
5424 if (**arg == ':')
5425 {
5426 range = TRUE;
5427 *arg = skipwhite(*arg + 1);
5428 if (**arg == ']')
5429 empty2 = TRUE;
5430 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005432 if (!empty1)
5433 clear_tv(&var1);
5434 return FAIL;
5435 }
5436 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5437 {
5438 /* not a number or string */
5439 if (!empty1)
5440 clear_tv(&var1);
5441 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 return FAIL;
5443 }
5444 }
5445
5446 /* Check for the ']'. */
5447 if (**arg != ']')
5448 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005449 if (verbose)
5450 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 clear_tv(&var1);
5452 if (range)
5453 clear_tv(&var2);
5454 return FAIL;
5455 }
5456 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458
5459 if (evaluate)
5460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 n1 = 0;
5462 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 n1 = get_tv_number(&var1);
5465 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 }
5467 if (range)
5468 {
5469 if (empty2)
5470 n2 = -1;
5471 else
5472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 n2 = get_tv_number(&var2);
5474 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 }
5476 }
5477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005481 case VAR_FUNC:
5482 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_SPECIAL:
5484 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005485 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486 break; /* not evaluating, skipping over subscript */
5487
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 case VAR_NUMBER:
5489 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 len = (long)STRLEN(s);
5492 if (range)
5493 {
5494 /* The resulting variable is a substring. If the indexes
5495 * are out of range the result is empty. */
5496 if (n1 < 0)
5497 {
5498 n1 = len + n1;
5499 if (n1 < 0)
5500 n1 = 0;
5501 }
5502 if (n2 < 0)
5503 n2 = len + n2;
5504 else if (n2 >= len)
5505 n2 = len;
5506 if (n1 >= len || n2 < 0 || n1 > n2)
5507 s = NULL;
5508 else
5509 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5510 }
5511 else
5512 {
5513 /* The resulting variable is a string of a single
5514 * character. If the index is too big or negative the
5515 * result is empty. */
5516 if (n1 >= len || n1 < 0)
5517 s = NULL;
5518 else
5519 s = vim_strnsave(s + n1, 1);
5520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 clear_tv(rettv);
5522 rettv->v_type = VAR_STRING;
5523 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 break;
5525
5526 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 if (n1 < 0)
5529 n1 = len + n1;
5530 if (!empty1 && (n1 < 0 || n1 >= len))
5531 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005532 /* For a range we allow invalid values and return an empty
5533 * list. A list index out of range is an error. */
5534 if (!range)
5535 {
5536 if (verbose)
5537 EMSGN(_(e_listidx), n1);
5538 return FAIL;
5539 }
5540 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 }
5542 if (range)
5543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 list_T *l;
5545 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546
5547 if (n2 < 0)
5548 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005549 else if (n2 >= len)
5550 n2 = len - 1;
5551 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 l = list_alloc();
5554 if (l == NULL)
5555 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 n1 <= n2; ++n1)
5558 {
5559 if (list_append_tv(l, &item->li_tv) == FAIL)
5560 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005561 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 return FAIL;
5563 }
5564 item = item->li_next;
5565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 clear_tv(rettv);
5567 rettv->v_type = VAR_LIST;
5568 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005569 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 }
5571 else
5572 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005573 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 clear_tv(rettv);
5575 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 }
5577 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
5579 case VAR_DICT:
5580 if (range)
5581 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005582 if (verbose)
5583 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 if (len == -1)
5585 clear_tv(&var1);
5586 return FAIL;
5587 }
5588 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590
5591 if (len == -1)
5592 {
5593 key = get_tv_string(&var1);
5594 if (*key == NUL)
5595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005596 if (verbose)
5597 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 clear_tv(&var1);
5599 return FAIL;
5600 }
5601 }
5602
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005603 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005606 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 if (len == -1)
5608 clear_tv(&var1);
5609 if (item == NULL)
5610 return FAIL;
5611
5612 copy_tv(&item->di_tv, &var1);
5613 clear_tv(rettv);
5614 *rettv = var1;
5615 }
5616 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 }
5618 }
5619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 return OK;
5621}
5622
5623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 * Get an option value.
5625 * "arg" points to the '&' or '+' before the option name.
5626 * "arg" is advanced to character after the option name.
5627 * Return OK or FAIL.
5628 */
5629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005630get_option_tv(
5631 char_u **arg,
5632 typval_T *rettv, /* when NULL, only check if option exists */
5633 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 char_u *option_end;
5636 long numval;
5637 char_u *stringval;
5638 int opt_type;
5639 int c;
5640 int working = (**arg == '+'); /* has("+option") */
5641 int ret = OK;
5642 int opt_flags;
5643
5644 /*
5645 * Isolate the option name and find its value.
5646 */
5647 option_end = find_option_end(arg, &opt_flags);
5648 if (option_end == NULL)
5649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 EMSG2(_("E112: Option name missing: %s"), *arg);
5652 return FAIL;
5653 }
5654
5655 if (!evaluate)
5656 {
5657 *arg = option_end;
5658 return OK;
5659 }
5660
5661 c = *option_end;
5662 *option_end = NUL;
5663 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 if (opt_type == -3) /* invalid name */
5667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 EMSG2(_("E113: Unknown option: %s"), *arg);
5670 ret = FAIL;
5671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 if (opt_type == -2) /* hidden string option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == -1) /* hidden number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else if (opt_type == 1) /* number option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_NUMBER;
5687 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else /* string option */
5690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 rettv->v_type = VAR_STRING;
5692 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 }
5695 else if (working && (opt_type == -2 || opt_type == -1))
5696 ret = FAIL;
5697
5698 *option_end = c; /* put back for error messages */
5699 *arg = option_end;
5700
5701 return ret;
5702}
5703
5704/*
5705 * Allocate a variable for a string constant.
5706 * Return OK or FAIL.
5707 */
5708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005709get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 char_u *p;
5712 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 int extra = 0;
5714
5715 /*
5716 * Find the end of the string, skipping backslashed characters.
5717 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 if (*p == '\\' && p[1] != NUL)
5721 {
5722 ++p;
5723 /* A "\<x>" form occupies at least 4 characters, and produces up
5724 * to 6 characters: reserve space for 2 extra */
5725 if (*p == '<')
5726 extra += 2;
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729
5730 if (*p != '"')
5731 {
5732 EMSG2(_("E114: Missing quote: %s"), *arg);
5733 return FAIL;
5734 }
5735
5736 /* If only parsing, set *arg and return here */
5737 if (!evaluate)
5738 {
5739 *arg = p + 1;
5740 return OK;
5741 }
5742
5743 /*
5744 * Copy the string into allocated memory, handling backslashed
5745 * characters.
5746 */
5747 name = alloc((unsigned)(p - *arg + extra));
5748 if (name == NULL)
5749 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 if (*p == '\\')
5756 {
5757 switch (*++p)
5758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 case 'b': *name++ = BS; ++p; break;
5760 case 'e': *name++ = ESC; ++p; break;
5761 case 'f': *name++ = FF; ++p; break;
5762 case 'n': *name++ = NL; ++p; break;
5763 case 'r': *name++ = CAR; ++p; break;
5764 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 case 'X': /* hex: "\x1", "\x12" */
5767 case 'x':
5768 case 'u': /* Unicode: "\u0023" */
5769 case 'U':
5770 if (vim_isxdigit(p[1]))
5771 {
5772 int n, nr;
5773 int c = toupper(*p);
5774
5775 if (c == 'X')
5776 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else
5780 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 nr = 0;
5782 while (--n >= 0 && vim_isxdigit(p[1]))
5783 {
5784 ++p;
5785 nr = (nr << 4) + hex2nr(*p);
5786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788#ifdef FEAT_MBYTE
5789 /* For "\u" store the number according to
5790 * 'encoding'. */
5791 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 else
5794#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798
5799 /* octal: "\1", "\12", "\123" */
5800 case '0':
5801 case '1':
5802 case '2':
5803 case '3':
5804 case '4':
5805 case '5':
5806 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 case '7': *name = *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 *name = (*name << 3) + *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
5812 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816
5817 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (extra != 0)
5820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 /* FALLTHROUGH */
5825
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828 }
5829 }
5830 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 *arg = p + 1;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 return OK;
5838}
5839
5840/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 * Return OK or FAIL.
5843 */
5844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 int reduce = 0;
5850
5851 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 */
5854 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++reduce;
5861 ++p;
5862 }
5863 }
5864
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 return FAIL;
5869 }
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 if (!evaluate)
5873 {
5874 *arg = p + 1;
5875 return OK;
5876 }
5877
5878 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 */
5881 str = alloc((unsigned)((p - *arg) - reduce));
5882 if (str == NULL)
5883 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 break;
5893 ++p;
5894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 *arg = p + 1;
5899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 return OK;
5901}
5902
5903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * Allocate a variable for a List and fill it from "*arg".
5905 * Return OK or FAIL.
5906 */
5907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005908get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909{
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 list_T *l = NULL;
5911 typval_T tv;
5912 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913
5914 if (evaluate)
5915 {
5916 l = list_alloc();
5917 if (l == NULL)
5918 return FAIL;
5919 }
5920
5921 *arg = skipwhite(*arg + 1);
5922 while (**arg != ']' && **arg != NUL)
5923 {
5924 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5925 goto failret;
5926 if (evaluate)
5927 {
5928 item = listitem_alloc();
5929 if (item != NULL)
5930 {
5931 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005932 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 list_append(l, item);
5934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005935 else
5936 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 }
5938
5939 if (**arg == ']')
5940 break;
5941 if (**arg != ',')
5942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 goto failret;
5945 }
5946 *arg = skipwhite(*arg + 1);
5947 }
5948
5949 if (**arg != ']')
5950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952failret:
5953 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005954 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 return FAIL;
5956 }
5957
5958 *arg = skipwhite(*arg + 1);
5959 if (evaluate)
5960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 rettv->v_type = VAR_LIST;
5962 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 ++l->lv_refcount;
5964 }
5965
5966 return OK;
5967}
5968
5969/*
5970 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005971 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005973 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005974list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005976 list_T *l;
5977
5978 l = (list_T *)alloc_clear(sizeof(list_T));
5979 if (l != NULL)
5980 {
5981 /* Prepend the list to the list of lists for garbage collection. */
5982 if (first_list != NULL)
5983 first_list->lv_used_prev = l;
5984 l->lv_used_prev = NULL;
5985 l->lv_used_next = first_list;
5986 first_list = l;
5987 }
5988 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989}
5990
5991/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992 * Allocate an empty list for a return value.
5993 * Returns OK or FAIL.
5994 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005997{
5998 list_T *l = list_alloc();
5999
6000 if (l == NULL)
6001 return FAIL;
6002
6003 rettv->vval.v_list = l;
6004 rettv->v_type = VAR_LIST;
6005 ++l->lv_refcount;
6006 return OK;
6007}
6008
6009/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 * Unreference a list: decrement the reference count and free it when it
6011 * becomes zero.
6012 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006016 if (l != NULL && --l->lv_refcount <= 0)
6017 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018}
6019
6020/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006021 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 * Ignores the reference count.
6023 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025list_free(
6026 list_T *l,
6027 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006031 /* Remove the list from the list of lists for garbage collection. */
6032 if (l->lv_used_prev == NULL)
6033 first_list = l->lv_used_next;
6034 else
6035 l->lv_used_prev->lv_used_next = l->lv_used_next;
6036 if (l->lv_used_next != NULL)
6037 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6038
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 /* Remove the item before deleting it. */
6042 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006043 if (recurse || (item->li_tv.v_type != VAR_LIST
6044 && item->li_tv.v_type != VAR_DICT))
6045 clear_tv(&item->li_tv);
6046 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 }
6048 vim_free(l);
6049}
6050
6051/*
6052 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006053 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006055 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006056listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059}
6060
6061/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006062 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006065listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 vim_free(item);
6069}
6070
6071/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072 * Remove a list item from a List and free it. Also clears the value.
6073 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006077 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078 listitem_free(item);
6079}
6080
6081/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 * Get the number of items in a list.
6083 */
6084 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 if (l == NULL)
6088 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090}
6091
6092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 * Return TRUE when two lists have exactly the same values.
6094 */
6095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096list_equal(
6097 list_T *l1,
6098 list_T *l2,
6099 int ic, /* ignore case for strings */
6100 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006104 if (l1 == NULL || l2 == NULL)
6105 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 if (l1 == l2)
6107 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108 if (list_len(l1) != list_len(l2))
6109 return FALSE;
6110
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 for (item1 = l1->lv_first, item2 = l2->lv_first;
6112 item1 != NULL && item2 != NULL;
6113 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 return FALSE;
6116 return item1 == NULL && item2 == NULL;
6117}
6118
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119/*
6120 * Return the dictitem that an entry in a hashtable points to.
6121 */
6122 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006123dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124{
6125 return HI2DI(hi);
6126}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006129 * Return TRUE when two dictionaries have exactly the same key/values.
6130 */
6131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006132dict_equal(
6133 dict_T *d1,
6134 dict_T *d2,
6135 int ic, /* ignore case for strings */
6136 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137{
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 hashitem_T *hi;
6139 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006142 if (d1 == NULL || d2 == NULL)
6143 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006144 if (d1 == d2)
6145 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 if (dict_len(d1) != dict_len(d2))
6147 return FALSE;
6148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006149 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 if (!HASHITEM_EMPTY(hi))
6153 {
6154 item2 = dict_find(d2, hi->hi_key, -1);
6155 if (item2 == NULL)
6156 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006157 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 return FALSE;
6159 --todo;
6160 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 }
6162 return TRUE;
6163}
6164
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165static int tv_equal_recurse_limit;
6166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 */
6172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006173tv_equal(
6174 typval_T *tv1,
6175 typval_T *tv2,
6176 int ic, /* ignore case */
6177 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178{
6179 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006180 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006182 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006184 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006185 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 * recursiveness to a limit. We guess they are equal then.
6189 * A fixed limit has the problem of still taking an awful long time.
6190 * Reduce the limit every time running into it. That should work fine for
6191 * deeply linked structures that are not recursively linked and catch
6192 * recursiveness quickly. */
6193 if (!recursive)
6194 tv_equal_recurse_limit = 1000;
6195 if (recursive_cnt >= tv_equal_recurse_limit)
6196 {
6197 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200
6201 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204 ++recursive_cnt;
6205 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6206 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006207 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208
6209 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006210 ++recursive_cnt;
6211 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6212 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006213 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 case VAR_FUNC:
6216 return (tv1->vval.v_string != NULL
6217 && tv2->vval.v_string != NULL
6218 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6219
6220 case VAR_NUMBER:
6221 return tv1->vval.v_number == tv2->vval.v_number;
6222
6223 case VAR_STRING:
6224 s1 = get_tv_string_buf(tv1, buf1);
6225 s2 = get_tv_string_buf(tv2, buf2);
6226 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006227
6228 case VAR_SPECIAL:
6229 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006230
6231 case VAR_FLOAT:
6232#ifdef FEAT_FLOAT
6233 return tv1->vval.v_float == tv2->vval.v_float;
6234#endif
6235 case VAR_JOB:
6236#ifdef FEAT_JOB
6237 return tv1->vval.v_job == tv2->vval.v_job;
6238#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006239 case VAR_CHANNEL:
6240#ifdef FEAT_CHANNEL
6241 return tv1->vval.v_channel == tv2->vval.v_channel;
6242#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006243 case VAR_UNKNOWN:
6244 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246
Bram Moolenaara03f2332016-02-06 18:09:59 +01006247 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6248 * does not equal anything, not even itself. */
6249 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259{
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 long idx;
6262
6263 if (l == NULL)
6264 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265
6266 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268 n = l->lv_len + n;
6269
6270 /* Check for index out of range. */
6271 if (n < 0 || n >= l->lv_len)
6272 return NULL;
6273
6274 /* When there is a cached index may start search from there. */
6275 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006277 if (n < l->lv_idx / 2)
6278 {
6279 /* closest to the start of the list */
6280 item = l->lv_first;
6281 idx = 0;
6282 }
6283 else if (n > (l->lv_idx + l->lv_len) / 2)
6284 {
6285 /* closest to the end of the list */
6286 item = l->lv_last;
6287 idx = l->lv_len - 1;
6288 }
6289 else
6290 {
6291 /* closest to the cached index */
6292 item = l->lv_idx_item;
6293 idx = l->lv_idx;
6294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 }
6296 else
6297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006298 if (n < l->lv_len / 2)
6299 {
6300 /* closest to the start of the list */
6301 item = l->lv_first;
6302 idx = 0;
6303 }
6304 else
6305 {
6306 /* closest to the end of the list */
6307 item = l->lv_last;
6308 idx = l->lv_len - 1;
6309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311
6312 while (n > idx)
6313 {
6314 /* search forward */
6315 item = item->li_next;
6316 ++idx;
6317 }
6318 while (n < idx)
6319 {
6320 /* search backward */
6321 item = item->li_prev;
6322 --idx;
6323 }
6324
6325 /* cache the used index */
6326 l->lv_idx = idx;
6327 l->lv_idx_item = item;
6328
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 return item;
6330}
6331
6332/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006333 * Get list item "l[idx]" as a number.
6334 */
6335 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006336list_find_nr(
6337 list_T *l,
6338 long idx,
6339 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006340{
6341 listitem_T *li;
6342
6343 li = list_find(l, idx);
6344 if (li == NULL)
6345 {
6346 if (errorp != NULL)
6347 *errorp = TRUE;
6348 return -1L;
6349 }
6350 return get_tv_number_chk(&li->li_tv, errorp);
6351}
6352
6353/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006354 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6355 */
6356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006357list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006358{
6359 listitem_T *li;
6360
6361 li = list_find(l, idx - 1);
6362 if (li == NULL)
6363 {
6364 EMSGN(_(e_listidx), idx);
6365 return NULL;
6366 }
6367 return get_tv_string(&li->li_tv);
6368}
6369
6370/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371 * Locate "item" list "l" and return its index.
6372 * Returns -1 when "item" is not in the list.
6373 */
6374 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376{
6377 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379
6380 if (l == NULL)
6381 return -1;
6382 idx = 0;
6383 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6384 ++idx;
6385 if (li == NULL)
6386 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006387 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006388}
6389
6390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 * Append item "item" to the end of list "l".
6392 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006394list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395{
6396 if (l->lv_last == NULL)
6397 {
6398 /* empty list */
6399 l->lv_first = item;
6400 l->lv_last = item;
6401 item->li_prev = NULL;
6402 }
6403 else
6404 {
6405 l->lv_last->li_next = item;
6406 item->li_prev = l->lv_last;
6407 l->lv_last = item;
6408 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006409 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 item->li_next = NULL;
6411}
6412
6413/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 copy_tv(tv, &li->li_tv);
6425 list_append(l, li);
6426 return OK;
6427}
6428
6429/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006430 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431 * Return FAIL when out of memory.
6432 */
6433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435{
6436 listitem_T *li = listitem_alloc();
6437
6438 if (li == NULL)
6439 return FAIL;
6440 li->li_tv.v_type = VAR_DICT;
6441 li->li_tv.v_lock = 0;
6442 li->li_tv.vval.v_dict = dict;
6443 list_append(list, li);
6444 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 return OK;
6446}
6447
6448/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006449 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Returns FAIL when out of memory.
6452 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006455{
6456 listitem_T *li = listitem_alloc();
6457
6458 if (li == NULL)
6459 return FAIL;
6460 list_append(l, li);
6461 li->li_tv.v_type = VAR_STRING;
6462 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006463 if (str == NULL)
6464 li->li_tv.vval.v_string = NULL;
6465 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006466 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 return FAIL;
6468 return OK;
6469}
6470
6471/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472 * Append "n" to list "l".
6473 * Returns FAIL when out of memory.
6474 */
6475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006477{
6478 listitem_T *li;
6479
6480 li = listitem_alloc();
6481 if (li == NULL)
6482 return FAIL;
6483 li->li_tv.v_type = VAR_NUMBER;
6484 li->li_tv.v_lock = 0;
6485 li->li_tv.vval.v_number = n;
6486 list_append(l, li);
6487 return OK;
6488}
6489
6490/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492 * If "item" is NULL append at the end.
6493 * Return FAIL when out of memory.
6494 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497{
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499
6500 if (ni == NULL)
6501 return FAIL;
6502 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006503 list_insert(l, ni, item);
6504 return OK;
6505}
6506
6507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006509{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (item == NULL)
6511 /* Append new item at end of list. */
6512 list_append(l, ni);
6513 else
6514 {
6515 /* Insert new item before existing item. */
6516 ni->li_prev = item->li_prev;
6517 ni->li_next = item;
6518 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 ++l->lv_idx;
6522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 l->lv_idx_item = NULL;
6527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531}
6532
6533/*
6534 * Extend "l1" with "l2".
6535 * If "bef" is NULL append at the end, otherwise insert before this item.
6536 * Returns FAIL when out of memory.
6537 */
6538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006539list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540{
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006542 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 /* We also quit the loop when we have inserted the original item count of
6545 * the list, avoid a hang when we extend a list with itself. */
6546 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6548 return FAIL;
6549 return OK;
6550}
6551
6552/*
6553 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6554 * Return FAIL when out of memory.
6555 */
6556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558{
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006561 if (l1 == NULL || l2 == NULL)
6562 return FAIL;
6563
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (l == NULL)
6567 return FAIL;
6568 tv->v_type = VAR_LIST;
6569 tv->vval.v_list = l;
6570
6571 /* append all items from the second list */
6572 return list_extend(l, l2, NULL);
6573}
6574
6575/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * Returns NULL when out of memory.
6580 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006582list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583{
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *copy;
6585 listitem_T *item;
6586 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587
6588 if (orig == NULL)
6589 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 copy = list_alloc();
6592 if (copy != NULL)
6593 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 if (copyID != 0)
6595 {
6596 /* Do this before adding the items, because one of the items may
6597 * refer back to this list. */
6598 orig->lv_copyID = copyID;
6599 orig->lv_copylist = copy;
6600 }
6601 for (item = orig->lv_first; item != NULL && !got_int;
6602 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 {
6604 ni = listitem_alloc();
6605 if (ni == NULL)
6606 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 {
6609 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6610 {
6611 vim_free(ni);
6612 break;
6613 }
6614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006616 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 list_append(copy, ni);
6618 }
6619 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 if (item != NULL)
6621 {
6622 list_unref(copy);
6623 copy = NULL;
6624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 }
6626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 return copy;
6628}
6629
6630/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006632 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006633 * This used to be called list_remove, but that conflicts with a Sun header
6634 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006637vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638{
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641 /* notify watchers */
6642 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006644 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645 list_fix_watch(l, ip);
6646 if (ip == item2)
6647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649
6650 if (item2->li_next == NULL)
6651 l->lv_last = item->li_prev;
6652 else
6653 item2->li_next->li_prev = item->li_prev;
6654 if (item->li_prev == NULL)
6655 l->lv_first = item2->li_next;
6656 else
6657 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006658 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659}
6660
6661/*
6662 * Return an allocated string with the string representation of a list.
6663 * May return NULL.
6664 */
6665 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667{
6668 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669
6670 if (tv->vval.v_list == NULL)
6671 return NULL;
6672 ga_init2(&ga, (int)sizeof(char), 80);
6673 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 {
6676 vim_free(ga.ga_data);
6677 return NULL;
6678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 ga_append(&ga, ']');
6680 ga_append(&ga, NUL);
6681 return (char_u *)ga.ga_data;
6682}
6683
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006684typedef struct join_S {
6685 char_u *s;
6686 char_u *tofree;
6687} join_T;
6688
6689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006690list_join_inner(
6691 garray_T *gap, /* to store the result in */
6692 list_T *l,
6693 char_u *sep,
6694 int echo_style,
6695 int copyID,
6696 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697{
6698 int i;
6699 join_T *p;
6700 int len;
6701 int sumlen = 0;
6702 int first = TRUE;
6703 char_u *tofree;
6704 char_u numbuf[NUMBUFLEN];
6705 listitem_T *item;
6706 char_u *s;
6707
6708 /* Stringify each item in the list. */
6709 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6710 {
6711 if (echo_style)
6712 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6713 else
6714 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6715 if (s == NULL)
6716 return FAIL;
6717
6718 len = (int)STRLEN(s);
6719 sumlen += len;
6720
Bram Moolenaarcde88542015-08-11 19:14:00 +02006721 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006722 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6723 if (tofree != NULL || s != numbuf)
6724 {
6725 p->s = s;
6726 p->tofree = tofree;
6727 }
6728 else
6729 {
6730 p->s = vim_strnsave(s, len);
6731 p->tofree = p->s;
6732 }
6733
6734 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006735 if (did_echo_string_emsg) /* recursion error, bail out */
6736 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737 }
6738
6739 /* Allocate result buffer with its total size, avoid re-allocation and
6740 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6741 if (join_gap->ga_len >= 2)
6742 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6743 if (ga_grow(gap, sumlen + 2) == FAIL)
6744 return FAIL;
6745
6746 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6747 {
6748 if (first)
6749 first = FALSE;
6750 else
6751 ga_concat(gap, sep);
6752 p = ((join_T *)join_gap->ga_data) + i;
6753
6754 if (p->s != NULL)
6755 ga_concat(gap, p->s);
6756 line_breakcheck();
6757 }
6758
6759 return OK;
6760}
6761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006764 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006765 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768list_join(
6769 garray_T *gap,
6770 list_T *l,
6771 char_u *sep,
6772 int echo_style,
6773 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006775 garray_T join_ga;
6776 int retval;
6777 join_T *p;
6778 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779
Bram Moolenaard39a7512015-04-16 22:51:22 +02006780 if (l->lv_len < 1)
6781 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6783 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6784
6785 /* Dispose each item in join_ga. */
6786 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006788 p = (join_T *)join_ga.ga_data;
6789 for (i = 0; i < join_ga.ga_len; ++i)
6790 {
6791 vim_free(p->tofree);
6792 ++p;
6793 }
6794 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796
6797 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798}
6799
6800/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006801 * Return the next (unique) copy ID.
6802 * Used for serializing nested structures.
6803 */
6804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006806{
6807 current_copyID += COPYID_INC;
6808 return current_copyID;
6809}
6810
6811/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 * Garbage collection for lists and dictionaries.
6813 *
6814 * We use reference counts to be able to free most items right away when they
6815 * are no longer used. But for composite items it's possible that it becomes
6816 * unused while the reference count is > 0: When there is a recursive
6817 * reference. Example:
6818 * :let l = [1, 2, 3]
6819 * :let d = {9: l}
6820 * :let l[1] = d
6821 *
6822 * Since this is quite unusual we handle this with garbage collection: every
6823 * once in a while find out which lists and dicts are not referenced from any
6824 * variable.
6825 *
6826 * Here is a good reference text about garbage collection (refers to Python
6827 * but it applies to all reference-counting mechanisms):
6828 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830
6831/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 * Do garbage collection for lists and dicts.
6833 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006836garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 buf_T *buf;
6841 win_T *wp;
6842 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006843 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006844 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006845 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006846#ifdef FEAT_WINDOWS
6847 tabpage_T *tp;
6848#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006850 /* Only do this once. */
6851 want_garbage_collect = FALSE;
6852 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006853 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006854
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 /* We advance by two because we add one for items referenced through
6856 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006857 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 /*
6860 * 1. Go through all accessible variables and mark all lists and dicts
6861 * with copyID.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863
6864 /* Don't free variables in the previous_funccal list unless they are only
6865 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006866 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6868 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006869 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6870 NULL);
6871 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6872 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 }
6874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /* script-local variables */
6876 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* buffer-local variables */
6880 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883
6884 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006885 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6887 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006888#ifdef FEAT_AUTOCMD
6889 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006892#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#ifdef FEAT_WINDOWS
6895 /* tabpage-local variables */
6896 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899#endif
6900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* function-local variables */
6905 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6906 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6908 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 }
6910
Bram Moolenaard812df62008-11-09 12:46:09 +00006911 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006913
Bram Moolenaar1dced572012-04-05 16:54:08 +02006914#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#endif
6917
Bram Moolenaardb913952012-06-29 12:54:53 +02006918#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#endif
6921
6922#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006924#endif
6925
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006926#ifdef FEAT_CHANNEL
6927 abort = abort || set_ref_in_channel(copyID);
6928#endif
6929
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006931 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 /*
6933 * 2. Free lists and dictionaries that are not referenced.
6934 */
6935 did_free = free_unref_items(copyID);
6936
6937 /*
6938 * 3. Check if any funccal can be freed now.
6939 */
6940 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (can_free_funccal(*pfc, copyID))
6943 {
6944 fc = *pfc;
6945 *pfc = fc->caller;
6946 free_funccal(fc, TRUE);
6947 did_free = TRUE;
6948 did_free_funccal = TRUE;
6949 }
6950 else
6951 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (did_free_funccal)
6954 /* When a funccal was freed some more items might be garbage
6955 * collected, so run again. */
6956 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 else if (p_verbose > 0)
6959 {
6960 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6961 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962
6963 return did_free;
6964}
6965
6966/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006967 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 */
6969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006972 dict_T *dd, *dd_next;
6973 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 int did_free = FALSE;
6975
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 */
6979 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 {
6981 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 /* Free the Dictionary and ordinary items it contains, but don't
6985 * recurse into Lists and Dictionaries, they will be in the list
6986 * of dicts or list of lists. */
6987 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 dd = dd_next;
6991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992
6993 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 * Go through the list of lists and free items without the copyID.
6995 * But don't free a list that has a watcher (used in a for loop), these
6996 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 */
6998 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006999 {
7000 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7002 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 /* Free the List and ordinary items it contains, but don't recurse
7005 * into Lists and Dictionaries, they will be in the list of dicts
7006 * or list of lists. */
7007 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007010 ll = ll_next;
7011 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007012
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 return did_free;
7014}
7015
7016/*
7017 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 * "list_stack" is used to add lists to be marked. Can be NULL.
7019 *
7020 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024{
7025 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 hashtab_T *cur_ht;
7029 ht_stack_T *ht_stack = NULL;
7030 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 cur_ht = ht;
7033 for (;;)
7034 {
7035 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 /* Mark each item in the hashtab. If the item contains a hashtab
7038 * it is added to ht_stack, if it contains a list it is added to
7039 * list_stack. */
7040 todo = (int)cur_ht->ht_used;
7041 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7042 if (!HASHITEM_EMPTY(hi))
7043 {
7044 --todo;
7045 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7046 &ht_stack, list_stack);
7047 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049
7050 if (ht_stack == NULL)
7051 break;
7052
7053 /* take an item from the stack */
7054 cur_ht = ht_stack->ht;
7055 tempitem = ht_stack;
7056 ht_stack = ht_stack->prev;
7057 free(tempitem);
7058 }
7059
7060 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061}
7062
7063/*
7064 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7066 *
7067 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 listitem_T *li;
7073 int abort = FALSE;
7074 list_T *cur_l;
7075 list_stack_T *list_stack = NULL;
7076 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 cur_l = l;
7079 for (;;)
7080 {
7081 if (!abort)
7082 /* Mark each item in the list. If the item contains a hashtab
7083 * it is added to ht_stack, if it contains a list it is added to
7084 * list_stack. */
7085 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7086 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7087 ht_stack, &list_stack);
7088 if (list_stack == NULL)
7089 break;
7090
7091 /* take an item from the stack */
7092 cur_l = list_stack->list;
7093 tempitem = list_stack;
7094 list_stack = list_stack->prev;
7095 free(tempitem);
7096 }
7097
7098 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099}
7100
7101/*
7102 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 * "list_stack" is used to add lists to be marked. Can be NULL.
7104 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7105 *
7106 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109set_ref_in_item(
7110 typval_T *tv,
7111 int copyID,
7112 ht_stack_T **ht_stack,
7113 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114{
7115 dict_T *dd;
7116 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007118
Bram Moolenaara03f2332016-02-06 18:09:59 +01007119 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 dd = tv->vval.v_dict;
7122 if (dd != NULL && dd->dv_copyID != copyID)
7123 {
7124 /* Didn't see this dict yet. */
7125 dd->dv_copyID = copyID;
7126 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007127 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007128 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7129 }
7130 else
7131 {
7132 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7133 if (newitem == NULL)
7134 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 else
7136 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 newitem->ht = &dd->dv_hashtab;
7138 newitem->prev = *ht_stack;
7139 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007142 }
7143 }
7144 else if (tv->v_type == VAR_LIST)
7145 {
7146 ll = tv->vval.v_list;
7147 if (ll != NULL && ll->lv_copyID != copyID)
7148 {
7149 /* Didn't see this list yet. */
7150 ll->lv_copyID = copyID;
7151 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007153 abort = set_ref_in_list(ll, copyID, ht_stack);
7154 }
7155 else
7156 {
7157 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007158 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007159 if (newitem == NULL)
7160 abort = TRUE;
7161 else
7162 {
7163 newitem->list = ll;
7164 newitem->prev = *list_stack;
7165 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171}
7172
7173/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 * Allocate an empty header for a dictionary.
7175 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007176 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007177dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178{
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007183 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007184 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 if (first_dict != NULL)
7186 first_dict->dv_used_prev = d;
7187 d->dv_used_next = first_dict;
7188 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007189 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007193 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198}
7199
7200/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007201 * Allocate an empty dict for a return value.
7202 * Returns OK or FAIL.
7203 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007206{
7207 dict_T *d = dict_alloc();
7208
7209 if (d == NULL)
7210 return FAIL;
7211
7212 rettv->vval.v_dict = d;
7213 rettv->v_type = VAR_DICT;
7214 ++d->dv_refcount;
7215 return OK;
7216}
7217
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Unreference a Dictionary: decrement the reference count and free it when it
7221 * becomes zero.
7222 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007226 if (d != NULL && --d->dv_refcount <= 0)
7227 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228}
7229
7230/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007231 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Ignores the reference count.
7233 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007235dict_free(
7236 dict_T *d,
7237 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007241 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243 /* Remove the dict from the list of dicts for garbage collection. */
7244 if (d->dv_used_prev == NULL)
7245 first_dict = d->dv_used_next;
7246 else
7247 d->dv_used_prev->dv_used_next = d->dv_used_next;
7248 if (d->dv_used_next != NULL)
7249 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7250
7251 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007252 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007253 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (!HASHITEM_EMPTY(hi))
7257 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007258 /* Remove the item before deleting it, just in case there is
7259 * something recursive causing trouble. */
7260 di = HI2DI(hi);
7261 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262 if (recurse || (di->di_tv.v_type != VAR_LIST
7263 && di->di_tv.v_type != VAR_DICT))
7264 clear_tv(&di->di_tv);
7265 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 --todo;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 vim_free(d);
7271}
7272
7273/*
7274 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 * The "key" is copied to the new item.
7276 * Note that the value of the item "di_tv" still needs to be initialized!
7277 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007279 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007284 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007288 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291}
7292
7293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 * Make a copy of a Dictionary item.
7295 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007297dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298{
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7302 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 if (di != NULL)
7304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007306 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307 copy_tv(&org->di_tv, &di->di_tv);
7308 }
7309 return di;
7310}
7311
7312/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 * Remove item "item" from Dictionary "dict" and free it.
7314 */
7315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317{
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 if (HASHITEM_EMPTY(hi))
7322 EMSG2(_(e_intern2), "dictitem_remove()");
7323 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 dictitem_free(item);
7326}
7327
7328/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 * Free a dict item. Also clears the value.
7330 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007332dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007335 if (item->di_flags & DI_FLAGS_ALLOC)
7336 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337}
7338
7339/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7341 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007342 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Returns NULL when out of memory.
7344 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347{
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 dict_T *copy;
7349 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352
7353 if (orig == NULL)
7354 return NULL;
7355
7356 copy = dict_alloc();
7357 if (copy != NULL)
7358 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007359 if (copyID != 0)
7360 {
7361 orig->dv_copyID = copyID;
7362 orig->dv_copydict = copy;
7363 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007364 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 --todo;
7370
7371 di = dictitem_alloc(hi->hi_key);
7372 if (di == NULL)
7373 break;
7374 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 {
7376 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7377 copyID) == FAIL)
7378 {
7379 vim_free(di);
7380 break;
7381 }
7382 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 else
7384 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7385 if (dict_add(copy, di) == FAIL)
7386 {
7387 dictitem_free(di);
7388 break;
7389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (todo > 0)
7395 {
7396 dict_unref(copy);
7397 copy = NULL;
7398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
7400
7401 return copy;
7402}
7403
7404/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007406 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410{
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412}
7413
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007415 * Add a number or string entry to dictionary "d".
7416 * When "str" is NULL use number "nr", otherwise use "str".
7417 * Returns FAIL when out of memory and when key already exists.
7418 */
7419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007420dict_add_nr_str(
7421 dict_T *d,
7422 char *key,
7423 long nr,
7424 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007425{
7426 dictitem_T *item;
7427
7428 item = dictitem_alloc((char_u *)key);
7429 if (item == NULL)
7430 return FAIL;
7431 item->di_tv.v_lock = 0;
7432 if (str == NULL)
7433 {
7434 item->di_tv.v_type = VAR_NUMBER;
7435 item->di_tv.vval.v_number = nr;
7436 }
7437 else
7438 {
7439 item->di_tv.v_type = VAR_STRING;
7440 item->di_tv.vval.v_string = vim_strsave(str);
7441 }
7442 if (dict_add(d, item) == FAIL)
7443 {
7444 dictitem_free(item);
7445 return FAIL;
7446 }
7447 return OK;
7448}
7449
7450/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007451 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007452 * Returns FAIL when out of memory and when key already exists.
7453 */
7454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007456{
7457 dictitem_T *item;
7458
7459 item = dictitem_alloc((char_u *)key);
7460 if (item == NULL)
7461 return FAIL;
7462 item->di_tv.v_lock = 0;
7463 item->di_tv.v_type = VAR_LIST;
7464 item->di_tv.vval.v_list = list;
7465 if (dict_add(d, item) == FAIL)
7466 {
7467 dictitem_free(item);
7468 return FAIL;
7469 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007470 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 return OK;
7472}
7473
7474/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 * Get the number of items in a Dictionary.
7476 */
7477 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 if (d == NULL)
7481 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007482 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483}
7484
7485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 * Find item "key[len]" in Dictionary "d".
7487 * If "len" is negative use strlen(key).
7488 * Returns NULL when not found.
7489 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007490 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007491dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493#define AKEYLEN 200
7494 char_u buf[AKEYLEN];
7495 char_u *akey;
7496 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007499 if (len < 0)
7500 akey = key;
7501 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tofree = akey = vim_strnsave(key, len);
7504 if (akey == NULL)
7505 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007506 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 else
7508 {
7509 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007510 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 akey = buf;
7512 }
7513
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 vim_free(tofree);
7516 if (HASHITEM_EMPTY(hi))
7517 return NULL;
7518 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519}
7520
7521/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007522 * Get a string item from a dictionary.
7523 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007524 * Returns NULL if the entry doesn't exist or out of memory.
7525 */
7526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528{
7529 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531
7532 di = dict_find(d, key, -1);
7533 if (di == NULL)
7534 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007535 s = get_tv_string(&di->di_tv);
7536 if (save && s != NULL)
7537 s = vim_strsave(s);
7538 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539}
7540
7541/*
7542 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007543 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544 */
7545 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547{
7548 dictitem_T *di;
7549
7550 di = dict_find(d, key, -1);
7551 if (di == NULL)
7552 return 0;
7553 return get_tv_number(&di->di_tv);
7554}
7555
7556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 * Return an allocated string with the string representation of a Dictionary.
7558 * May return NULL.
7559 */
7560 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562{
7563 garray_T ga;
7564 int first = TRUE;
7565 char_u *tofree;
7566 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 return NULL;
7574 ga_init2(&ga, (int)sizeof(char), 80);
7575 ga_append(&ga, '{');
7576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007578 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 --todo;
7583
7584 if (first)
7585 first = FALSE;
7586 else
7587 ga_concat(&ga, (char_u *)", ");
7588
7589 tofree = string_quote(hi->hi_key, FALSE);
7590 if (tofree != NULL)
7591 {
7592 ga_concat(&ga, tofree);
7593 vim_free(tofree);
7594 }
7595 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 if (s != NULL)
7598 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007600 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 line_breakcheck();
7603
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 if (todo > 0)
7607 {
7608 vim_free(ga.ga_data);
7609 return NULL;
7610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611
7612 ga_append(&ga, '}');
7613 ga_append(&ga, NUL);
7614 return (char_u *)ga.ga_data;
7615}
7616
7617/*
7618 * Allocate a variable for a Dictionary and fill it from "*arg".
7619 * Return OK or FAIL. Returns NOTDONE for {expr}.
7620 */
7621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623{
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 dict_T *d = NULL;
7625 typval_T tvkey;
7626 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007627 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631
7632 /*
7633 * First check if it's not a curly-braces thing: {expr}.
7634 * Must do this without evaluating, otherwise a function may be called
7635 * twice. Unfortunately this means we need to call eval1() twice for the
7636 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 if (*start != '}')
7640 {
7641 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7642 return FAIL;
7643 if (*start == '}')
7644 return NOTDONE;
7645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646
7647 if (evaluate)
7648 {
7649 d = dict_alloc();
7650 if (d == NULL)
7651 return FAIL;
7652 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653 tvkey.v_type = VAR_UNKNOWN;
7654 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655
7656 *arg = skipwhite(*arg + 1);
7657 while (**arg != '}' && **arg != NUL)
7658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 if (**arg != ':')
7662 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007663 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 goto failret;
7666 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007667 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 key = get_tv_string_buf_chk(&tvkey, buf);
7670 if (key == NULL || *key == NUL)
7671 {
7672 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7673 if (key != NULL)
7674 EMSG(_(e_emptykey));
7675 clear_tv(&tvkey);
7676 goto failret;
7677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679
7680 *arg = skipwhite(*arg + 1);
7681 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7682 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007683 if (evaluate)
7684 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 goto failret;
7686 }
7687 if (evaluate)
7688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 if (item != NULL)
7691 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007692 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 clear_tv(&tv);
7695 goto failret;
7696 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 item = dictitem_alloc(key);
7698 clear_tv(&tvkey);
7699 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007702 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 if (dict_add(d, item) == FAIL)
7704 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 }
7706 }
7707
7708 if (**arg == '}')
7709 break;
7710 if (**arg != ',')
7711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007712 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 goto failret;
7714 }
7715 *arg = skipwhite(*arg + 1);
7716 }
7717
7718 if (**arg != '}')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721failret:
7722 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007723 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 return FAIL;
7725 }
7726
7727 *arg = skipwhite(*arg + 1);
7728 if (evaluate)
7729 {
7730 rettv->v_type = VAR_DICT;
7731 rettv->vval.v_dict = d;
7732 ++d->dv_refcount;
7733 }
7734
7735 return OK;
7736}
7737
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007738#if defined(FEAT_CHANNEL) || defined(PROTO)
7739/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007740 * Decrement the reference count on "channel" and maybe free it when it goes
7741 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007742 * Returns TRUE when the channel was freed.
7743 */
7744 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007745channel_unref(channel_T *channel)
7746{
7747 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007748 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007749 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007750 return TRUE;
7751 }
7752 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007753}
7754#endif
7755
Bram Moolenaar65edff82016-02-21 16:40:11 +01007756#if defined(FEAT_JOB) || defined(PROTO)
7757static job_T *first_job = NULL;
7758
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 static void
7760job_free(job_T *job)
7761{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007762# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007763 if (job->jv_channel != NULL)
7764 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007765 /* The link from the channel to the job doesn't count as a reference,
7766 * thus don't decrement the refcount of the job. The reference from
7767 * the job to the channel does count the refrence, decrement it and
7768 * NULL the reference. We don't set ch_job_killed, unreferencing the
7769 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 job->jv_channel->ch_job = NULL;
7771 channel_unref(job->jv_channel);
7772 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007773# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007774 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007775
7776 if (job->jv_next != NULL)
7777 job->jv_next->jv_prev = job->jv_prev;
7778 if (job->jv_prev == NULL)
7779 first_job = job->jv_next;
7780 else
7781 job->jv_prev->jv_next = job->jv_next;
7782
7783 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007784 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 vim_free(job);
7786}
7787
7788 static void
7789job_unref(job_T *job)
7790{
7791 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007792 {
7793 /* Do not free the job when it has not ended yet and there is a
7794 * "stoponexit" flag or an exit callback. */
7795 if (job->jv_status != JOB_STARTED
7796 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7797 job_free(job);
7798 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007799}
7800
7801/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007802 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007803 */
7804 static job_T *
7805job_alloc(void)
7806{
7807 job_T *job;
7808
7809 job = (job_T *)alloc_clear(sizeof(job_T));
7810 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007811 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007813 job->jv_stoponexit = vim_strsave((char_u *)"term");
7814
7815 if (first_job != NULL)
7816 {
7817 first_job->jv_prev = job;
7818 job->jv_next = first_job;
7819 }
7820 first_job = job;
7821 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007822 return job;
7823}
7824
Bram Moolenaar65edff82016-02-21 16:40:11 +01007825 static void
7826job_set_options(job_T *job, jobopt_T *opt)
7827{
7828 if (opt->jo_set & JO_STOPONEXIT)
7829 {
7830 vim_free(job->jv_stoponexit);
7831 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7832 job->jv_stoponexit = NULL;
7833 else
7834 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7835 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007836 if (opt->jo_set & JO_EXIT_CB)
7837 {
7838 vim_free(job->jv_exit_cb);
7839 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7840 job->jv_exit_cb = NULL;
7841 else
7842 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7843 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007844}
7845
7846/*
7847 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7848 */
7849 void
7850job_stop_on_exit()
7851{
7852 job_T *job;
7853
7854 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007855 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007856 mch_stop_job(job, job->jv_stoponexit);
7857}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007858#endif
7859
Bram Moolenaar17a13432016-01-24 14:22:10 +01007860 static char *
7861get_var_special_name(int nr)
7862{
7863 switch (nr)
7864 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007865 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007866 case VVAL_TRUE: return "v:true";
7867 case VVAL_NONE: return "v:none";
7868 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007869 }
7870 EMSG2(_(e_intern2), "get_var_special_name()");
7871 return "42";
7872}
7873
Bram Moolenaar8c711452005-01-14 21:53:12 +00007874/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 * Return a string with the string representation of a variable.
7876 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007877 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007880 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 */
7882 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883echo_string(
7884 typval_T *tv,
7885 char_u **tofree,
7886 char_u *numbuf,
7887 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 static int recurse = 0;
7890 char_u *r = NULL;
7891
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007894 if (!did_echo_string_emsg)
7895 {
7896 /* Only give this message once for a recursive call to avoid
7897 * flooding the user with errors. And stop iterating over lists
7898 * and dicts. */
7899 did_echo_string_emsg = TRUE;
7900 EMSG(_("E724: variable nested too deep for displaying"));
7901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007903 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 }
7905 ++recurse;
7906
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 switch (tv->v_type)
7908 {
7909 case VAR_FUNC:
7910 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007911 r = tv->vval.v_string;
7912 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007913
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007914 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007915 if (tv->vval.v_list == NULL)
7916 {
7917 *tofree = NULL;
7918 r = NULL;
7919 }
7920 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7921 {
7922 *tofree = NULL;
7923 r = (char_u *)"[...]";
7924 }
7925 else
7926 {
7927 tv->vval.v_list->lv_copyID = copyID;
7928 *tofree = list2string(tv, copyID);
7929 r = *tofree;
7930 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007931 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007932
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007934 if (tv->vval.v_dict == NULL)
7935 {
7936 *tofree = NULL;
7937 r = NULL;
7938 }
7939 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7940 {
7941 *tofree = NULL;
7942 r = (char_u *)"{...}";
7943 }
7944 else
7945 {
7946 tv->vval.v_dict->dv_copyID = copyID;
7947 *tofree = dict2string(tv, copyID);
7948 r = *tofree;
7949 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007950 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 case VAR_STRING:
7953 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007954 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007955 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007956 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007957 *tofree = NULL;
7958 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007959 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007960
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007961 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007962#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963 *tofree = NULL;
7964 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7965 r = numbuf;
7966 break;
7967#endif
7968
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007969 case VAR_SPECIAL:
7970 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007971 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007972 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007974
Bram Moolenaar8502c702014-06-17 12:51:16 +02007975 if (--recurse == 0)
7976 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007977 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978}
7979
7980/*
7981 * Return a string with the string representation of a variable.
7982 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7983 * "numbuf" is used for a number.
7984 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007985 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 */
7987 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007988tv2string(
7989 typval_T *tv,
7990 char_u **tofree,
7991 char_u *numbuf,
7992 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007993{
7994 switch (tv->v_type)
7995 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996 case VAR_FUNC:
7997 *tofree = string_quote(tv->vval.v_string, TRUE);
7998 return *tofree;
7999 case VAR_STRING:
8000 *tofree = string_quote(tv->vval.v_string, FALSE);
8001 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008002#ifdef FEAT_FLOAT
8003 case VAR_FLOAT:
8004 *tofree = NULL;
8005 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8006 return numbuf;
8007#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008010 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008011 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008012 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008013 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008014 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008016 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008017 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018}
8019
8020/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 * Return string "str" in ' quotes, doubling ' characters.
8022 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008024 */
8025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008026string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008027{
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029 char_u *p, *r, *s;
8030
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 len = (function ? 13 : 3);
8032 if (str != NULL)
8033 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008034 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 for (p = str; *p != NUL; mb_ptr_adv(p))
8036 if (*p == '\'')
8037 ++len;
8038 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039 s = r = alloc(len);
8040 if (r != NULL)
8041 {
8042 if (function)
8043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008045 r += 10;
8046 }
8047 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 if (str != NULL)
8050 for (p = str; *p != NUL; )
8051 {
8052 if (*p == '\'')
8053 *r++ = '\'';
8054 MB_COPY_CHAR(p, r);
8055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008057 if (function)
8058 *r++ = ')';
8059 *r++ = NUL;
8060 }
8061 return s;
8062}
8063
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008064#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065/*
8066 * Convert the string "text" to a floating point number.
8067 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8068 * this always uses a decimal point.
8069 * Returns the length of the text that was consumed.
8070 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008072string2float(
8073 char_u *text,
8074 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075{
8076 char *s = (char *)text;
8077 float_T f;
8078
8079 f = strtod(s, &s);
8080 *value = f;
8081 return (int)((char_u *)s - text);
8082}
8083#endif
8084
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 * Get the value of an environment variable.
8087 * "arg" is pointing to the '$'. It is advanced to after the name.
8088 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008089 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 */
8091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008092get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 char_u *string = NULL;
8095 int len;
8096 int cc;
8097 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008098 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100 ++*arg;
8101 name = *arg;
8102 len = get_env_len(arg);
8103 if (evaluate)
8104 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008105 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008106 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008107
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008108 cc = name[len];
8109 name[len] = NUL;
8110 /* first try vim_getenv(), fast for normal environment vars */
8111 string = vim_getenv(name, &mustfree);
8112 if (string != NULL && *string != NUL)
8113 {
8114 if (!mustfree)
8115 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008117 else
8118 {
8119 if (mustfree)
8120 vim_free(string);
8121
8122 /* next try expanding things like $VIM and ${HOME} */
8123 string = expand_env_save(name - 1);
8124 if (string != NULL && *string == '$')
8125 {
8126 vim_free(string);
8127 string = NULL;
8128 }
8129 }
8130 name[len] = cc;
8131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 rettv->v_type = VAR_STRING;
8133 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135
8136 return OK;
8137}
8138
8139/*
8140 * Array with names and number of arguments of all internal functions
8141 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8142 */
8143static struct fst
8144{
8145 char *f_name; /* function name */
8146 char f_min_argc; /* minimal number of arguments */
8147 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008148 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008149 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150} functions[] =
8151{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152#ifdef FEAT_FLOAT
8153 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008154 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008156 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008157 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008158 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"append", 2, 2, f_append},
8160 {"argc", 0, 0, f_argc},
8161 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008162 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008163 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008164#ifdef FEAT_FLOAT
8165 {"asin", 1, 1, f_asin}, /* WJMc */
8166#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008167 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008168 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008169 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008170 {"assert_false", 1, 2, f_assert_false},
8171 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008172#ifdef FEAT_FLOAT
8173 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008174 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008177 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"bufexists", 1, 1, f_bufexists},
8179 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8180 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8181 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8182 {"buflisted", 1, 1, f_buflisted},
8183 {"bufloaded", 1, 1, f_bufloaded},
8184 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008185 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"bufwinnr", 1, 1, f_bufwinnr},
8187 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008188 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008189 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191#ifdef FEAT_FLOAT
8192 {"ceil", 1, 1, f_ceil},
8193#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008194#ifdef FEAT_CHANNEL
8195 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008196 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8197 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008198# ifdef FEAT_JOB
8199 {"ch_getjob", 1, 1, f_ch_getjob},
8200# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008201 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008202 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008203 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008204 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008205 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008206 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8207 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008208 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008209 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008210#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008211 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008212 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008214 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008216#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008217 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008218 {"complete_add", 1, 1, f_complete_add},
8219 {"complete_check", 0, 0, f_complete_check},
8220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008223#ifdef FEAT_FLOAT
8224 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008225 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008226#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008229 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008230 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008231 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008233 {"diff_filler", 1, 1, f_diff_filler},
8234 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008235 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008236 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"eventhandler", 0, 0, f_eventhandler},
8240 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008241 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008243#ifdef FEAT_FLOAT
8244 {"exp", 1, 1, f_exp},
8245#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008246 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008247 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008248 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8250 {"filereadable", 1, 1, f_filereadable},
8251 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008253 {"finddir", 1, 3, f_finddir},
8254 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255#ifdef FEAT_FLOAT
8256 {"float2nr", 1, 1, f_float2nr},
8257 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008258 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008260 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"fnamemodify", 2, 2, f_fnamemodify},
8262 {"foldclosed", 1, 1, f_foldclosed},
8263 {"foldclosedend", 1, 1, f_foldclosedend},
8264 {"foldlevel", 1, 1, f_foldlevel},
8265 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008266 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008269 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008270 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008271 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008272 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"getchar", 0, 1, f_getchar},
8274 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008275 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {"getcmdline", 0, 0, f_getcmdline},
8277 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008278 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008279 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008280 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008281 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008282 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008283 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"getfsize", 1, 1, f_getfsize},
8285 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008286 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008287 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008288 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008289 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008290 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008291 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008292 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008293 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008295 {"gettabvar", 2, 3, f_gettabvar},
8296 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"getwinposx", 0, 0, f_getwinposx},
8298 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008299 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008300 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008301 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008302 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008305 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008306 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8308 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8309 {"histadd", 2, 2, f_histadd},
8310 {"histdel", 1, 2, f_histdel},
8311 {"histget", 1, 2, f_histget},
8312 {"histnr", 1, 1, f_histnr},
8313 {"hlID", 1, 1, f_hlID},
8314 {"hlexists", 1, 1, f_hlexists},
8315 {"hostname", 0, 0, f_hostname},
8316 {"iconv", 3, 3, f_iconv},
8317 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008318 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008319 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008321 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"inputrestore", 0, 0, f_inputrestore},
8323 {"inputsave", 0, 0, f_inputsave},
8324 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008326 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008328 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008329#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8330 {"isnan", 1, 1, f_isnan},
8331#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008332 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008333#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008334# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008335 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008336# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008337 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008338 {"job_start", 1, 2, f_job_start},
8339 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008340 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008341#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008343 {"js_decode", 1, 1, f_js_decode},
8344 {"js_encode", 1, 1, f_js_encode},
8345 {"json_decode", 1, 1, f_json_decode},
8346 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"libcall", 3, 3, f_libcall},
8351 {"libcallnr", 3, 3, f_libcallnr},
8352 {"line", 1, 1, f_line},
8353 {"line2byte", 1, 1, f_line2byte},
8354 {"lispindent", 1, 1, f_lispindent},
8355 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008356#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008357 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008358 {"log10", 1, 1, f_log10},
8359#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008360#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008361 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008362#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008363 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008364 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008365 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008366 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008367 {"matchadd", 2, 5, f_matchadd},
8368 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008369 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008370 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008371 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008372 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008373 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008374 {"max", 1, 1, f_max},
8375 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008376#ifdef vim_mkdir
8377 {"mkdir", 1, 3, f_mkdir},
8378#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008379 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008380#ifdef FEAT_MZSCHEME
8381 {"mzeval", 1, 1, f_mzeval},
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008384 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008385 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008386 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008387#ifdef FEAT_PERL
8388 {"perleval", 1, 1, f_perleval},
8389#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008390#ifdef FEAT_FLOAT
8391 {"pow", 2, 2, f_pow},
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008394 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008395 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008396#ifdef FEAT_PYTHON3
8397 {"py3eval", 1, 1, f_py3eval},
8398#endif
8399#ifdef FEAT_PYTHON
8400 {"pyeval", 1, 1, f_pyeval},
8401#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008402 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008403 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008404 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008405 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008406 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"remote_expr", 2, 3, f_remote_expr},
8408 {"remote_foreground", 1, 1, f_remote_foreground},
8409 {"remote_peek", 1, 2, f_remote_peek},
8410 {"remote_read", 1, 1, f_remote_read},
8411 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008412 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008414 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008416 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008417#ifdef FEAT_FLOAT
8418 {"round", 1, 1, f_round},
8419#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008420 {"screenattr", 2, 2, f_screenattr},
8421 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008422 {"screencol", 0, 0, f_screencol},
8423 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008424 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008425 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008426 {"searchpair", 3, 7, f_searchpair},
8427 {"searchpairpos", 3, 7, f_searchpairpos},
8428 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"server2client", 2, 2, f_server2client},
8430 {"serverlist", 0, 0, f_serverlist},
8431 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008432 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"setcmdpos", 1, 1, f_setcmdpos},
8434 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008435 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008436 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008437 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008438 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008440 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008441 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008443#ifdef FEAT_CRYPT
8444 {"sha256", 1, 1, f_sha256},
8445#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008446 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008447 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#ifdef FEAT_FLOAT
8450 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008451 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008453 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008454 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008455 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008456 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008457 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008458#ifdef FEAT_FLOAT
8459 {"sqrt", 1, 1, f_sqrt},
8460 {"str2float", 1, 1, f_str2float},
8461#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008462 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008463 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008464 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465#ifdef HAVE_STRFTIME
8466 {"strftime", 1, 2, f_strftime},
8467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008468 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008469 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"strlen", 1, 1, f_strlen},
8471 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008472 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008474 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008475 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"substitute", 4, 4, f_substitute},
8477 {"synID", 3, 3, f_synID},
8478 {"synIDattr", 2, 3, f_synIDattr},
8479 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008480 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008481 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008482 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008483 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008484 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008485 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008486 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008487 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008488 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008489#ifdef FEAT_FLOAT
8490 {"tan", 1, 1, f_tan},
8491 {"tanh", 1, 1, f_tanh},
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008494 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {"tolower", 1, 1, f_tolower},
8496 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008497 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008498#ifdef FEAT_FLOAT
8499 {"trunc", 1, 1, f_trunc},
8500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008502 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008503 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008504 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008505 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"virtcol", 1, 1, f_virtcol},
8507 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008508 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"winbufnr", 1, 1, f_winbufnr},
8510 {"wincol", 0, 0, f_wincol},
8511 {"winheight", 1, 1, f_winheight},
8512 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008513 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008515 {"winrestview", 1, 1, f_winrestview},
8516 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008518 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008519 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008520 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521};
8522
8523#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8524
8525/*
8526 * Function given to ExpandGeneric() to obtain the list of internal
8527 * or user defined function names.
8528 */
8529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 static int intidx = -1;
8533 char_u *name;
8534
8535 if (idx == 0)
8536 intidx = -1;
8537 if (intidx < 0)
8538 {
8539 name = get_user_func_name(xp, idx);
8540 if (name != NULL)
8541 return name;
8542 }
8543 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8544 {
8545 STRCPY(IObuff, functions[intidx].f_name);
8546 STRCAT(IObuff, "(");
8547 if (functions[intidx].f_max_argc == 0)
8548 STRCAT(IObuff, ")");
8549 return IObuff;
8550 }
8551
8552 return NULL;
8553}
8554
8555/*
8556 * Function given to ExpandGeneric() to obtain the list of internal or
8557 * user defined variable or function names.
8558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008560get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 static int intidx = -1;
8563 char_u *name;
8564
8565 if (idx == 0)
8566 intidx = -1;
8567 if (intidx < 0)
8568 {
8569 name = get_function_name(xp, idx);
8570 if (name != NULL)
8571 return name;
8572 }
8573 return get_user_var_name(xp, ++intidx);
8574}
8575
8576#endif /* FEAT_CMDL_COMPL */
8577
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008578#if defined(EBCDIC) || defined(PROTO)
8579/*
8580 * Compare struct fst by function name.
8581 */
8582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008583compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008584{
8585 struct fst *p1 = (struct fst *)s1;
8586 struct fst *p2 = (struct fst *)s2;
8587
8588 return STRCMP(p1->f_name, p2->f_name);
8589}
8590
8591/*
8592 * Sort the function table by function name.
8593 * The sorting of the table above is ASCII dependant.
8594 * On machines using EBCDIC we have to sort it.
8595 */
8596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008597sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008598{
8599 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8600
8601 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8602}
8603#endif
8604
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606/*
8607 * Find internal function in table above.
8608 * Return index, or -1 if not found
8609 */
8610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611find_internal_func(
8612 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
8614 int first = 0;
8615 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8616 int cmp;
8617 int x;
8618
8619 /*
8620 * Find the function name in the table. Binary search.
8621 */
8622 while (first <= last)
8623 {
8624 x = first + ((unsigned)(last - first) >> 1);
8625 cmp = STRCMP(name, functions[x].f_name);
8626 if (cmp < 0)
8627 last = x - 1;
8628 else if (cmp > 0)
8629 first = x + 1;
8630 else
8631 return x;
8632 }
8633 return -1;
8634}
8635
8636/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008637 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8638 * name it contains, otherwise return "name".
8639 */
8640 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008641deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008642{
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 int cc;
8645
8646 cc = name[*lenp];
8647 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008648 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008649 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008650 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008652 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008653 {
8654 *lenp = 0;
8655 return (char_u *)""; /* just in case */
8656 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008657 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 }
8660
8661 return name;
8662}
8663
8664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 * Allocate a variable for the result of a function.
8666 * Return OK or FAIL.
8667 */
8668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008669get_func_tv(
8670 char_u *name, /* name of the function */
8671 int len, /* length of "name" */
8672 typval_T *rettv,
8673 char_u **arg, /* argument, pointing to the '(' */
8674 linenr_T firstline, /* first line of range */
8675 linenr_T lastline, /* last line of range */
8676 int *doesrange, /* return: function handled range */
8677 int evaluate,
8678 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 char_u *argp;
8681 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008682 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 int argcount = 0; /* number of arguments found */
8684
8685 /*
8686 * Get the arguments.
8687 */
8688 argp = *arg;
8689 while (argcount < MAX_FUNC_ARGS)
8690 {
8691 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8692 if (*argp == ')' || *argp == ',' || *argp == NUL)
8693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8695 {
8696 ret = FAIL;
8697 break;
8698 }
8699 ++argcount;
8700 if (*argp != ',')
8701 break;
8702 }
8703 if (*argp == ')')
8704 ++argp;
8705 else
8706 ret = FAIL;
8707
8708 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008710 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 {
8713 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008714 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008716 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 *arg = skipwhite(argp);
8723 return ret;
8724}
8725
8726
8727/*
8728 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008729 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008730 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008733call_func(
8734 char_u *funcname, /* name of the function */
8735 int len, /* length of "name" */
8736 typval_T *rettv, /* return value goes here */
8737 int argcount, /* number of "argvars" */
8738 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008739 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008740 linenr_T firstline, /* first line of range */
8741 linenr_T lastline, /* last line of range */
8742 int *doesrange, /* return: function handled range */
8743 int evaluate,
8744 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747#define ERROR_UNKNOWN 0
8748#define ERROR_TOOMANY 1
8749#define ERROR_TOOFEW 2
8750#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751#define ERROR_DICT 4
8752#define ERROR_NONE 5
8753#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 int error = ERROR_NONE;
8755 int i;
8756 int llen;
8757 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758#define FLEN_FIXED 40
8759 char_u fname_buf[FLEN_FIXED + 1];
8760 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008761 char_u *name;
8762
8763 /* Make a copy of the name, if it comes from a funcref variable it could
8764 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008765 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008766 if (name == NULL)
8767 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 /*
8770 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8771 * Change <SNR>123_name() to K_SNR 123_name().
8772 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 llen = eval_fname_script(name);
8775 if (llen > 0)
8776 {
8777 fname_buf[0] = K_SPECIAL;
8778 fname_buf[1] = KS_EXTRA;
8779 fname_buf[2] = (int)KE_SNR;
8780 i = 3;
8781 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8782 {
8783 if (current_SID <= 0)
8784 error = ERROR_SCRIPT;
8785 else
8786 {
8787 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8788 i = (int)STRLEN(fname_buf);
8789 }
8790 }
8791 if (i + STRLEN(name + llen) < FLEN_FIXED)
8792 {
8793 STRCPY(fname_buf + i, name + llen);
8794 fname = fname_buf;
8795 }
8796 else
8797 {
8798 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8799 if (fname == NULL)
8800 error = ERROR_OTHER;
8801 else
8802 {
8803 mch_memmove(fname, fname_buf, (size_t)i);
8804 STRCPY(fname + i, name + llen);
8805 }
8806 }
8807 }
8808 else
8809 fname = name;
8810
8811 *doesrange = FALSE;
8812
8813
8814 /* execute the function if no errors detected and executing */
8815 if (evaluate && error == ERROR_NONE)
8816 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008817 char_u *rfname = fname;
8818
8819 /* Ignore "g:" before a function name. */
8820 if (fname[0] == 'g' && fname[1] == ':')
8821 rfname = fname + 2;
8822
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008823 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8824 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 error = ERROR_UNKNOWN;
8826
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008827 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
8829 /*
8830 * User defined function.
8831 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008832 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835 /* Trigger FuncUndefined event, may load the function. */
8836 if (fp == NULL
8837 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008838 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008839 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008842 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 }
8844#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008845 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008846 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008847 {
8848 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 }
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (fp != NULL)
8853 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008854 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008856 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008858 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008861 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else
8863 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008864 int did_save_redo = FALSE;
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 /*
8867 * Call the user function.
8868 * Save and restore search patterns, script variables and
8869 * redo buffer.
8870 */
8871 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008872#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008873 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008874#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008875 {
8876 saveRedobuff();
8877 did_save_redo = TRUE;
8878 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008879 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008882 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8883 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8884 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008885 /* Function was unreferenced while being used, free it
8886 * now. */
8887 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008888 if (did_save_redo)
8889 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 restore_search_patterns();
8891 error = ERROR_NONE;
8892 }
8893 }
8894 }
8895 else
8896 {
8897 /*
8898 * Find the function name in the table, call its implementation.
8899 */
8900 i = find_internal_func(fname);
8901 if (i >= 0)
8902 {
8903 if (argcount < functions[i].f_min_argc)
8904 error = ERROR_TOOFEW;
8905 else if (argcount > functions[i].f_max_argc)
8906 error = ERROR_TOOMANY;
8907 else
8908 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008909 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 error = ERROR_NONE;
8912 }
8913 }
8914 }
8915 /*
8916 * The function call (or "FuncUndefined" autocommand sequence) might
8917 * have been aborted by an error, an interrupt, or an explicitly thrown
8918 * exception that has not been caught so far. This situation can be
8919 * tested for by calling aborting(). For an error in an internal
8920 * function or for the "E132" error in call_user_func(), however, the
8921 * throw point at which the "force_abort" flag (temporarily reset by
8922 * emsg()) is normally updated has not been reached yet. We need to
8923 * update that flag first to make aborting() reliable.
8924 */
8925 update_force_abort();
8926 }
8927 if (error == ERROR_NONE)
8928 ret = OK;
8929
8930 /*
8931 * Report an error unless the argument evaluation or function call has been
8932 * cancelled due to an aborting error, an interrupt, or an exception.
8933 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 if (!aborting())
8935 {
8936 switch (error)
8937 {
8938 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008939 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 break;
8941 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008942 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008943 break;
8944 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008945 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008946 name);
8947 break;
8948 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008949 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008950 name);
8951 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008953 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008954 name);
8955 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008956 }
8957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (fname != name && fname != fname_buf)
8960 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008961 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962
8963 return ret;
8964}
8965
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008966/*
8967 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008968 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969 */
8970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008971emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008972{
8973 char_u *p;
8974
8975 if (*name == K_SPECIAL)
8976 p = concat_str((char_u *)"<SNR>", name + 3);
8977 else
8978 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008979 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008980 if (p != name)
8981 vim_free(p);
8982}
8983
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008984/*
8985 * Return TRUE for a non-zero Number and a non-empty String.
8986 */
8987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008989{
8990 return ((argvars[0].v_type == VAR_NUMBER
8991 && argvars[0].vval.v_number != 0)
8992 || (argvars[0].v_type == VAR_STRING
8993 && argvars[0].vval.v_string != NULL
8994 && *argvars[0].vval.v_string != NUL));
8995}
8996
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997/*********************************************
8998 * Implementation of the built-in functions
8999 */
9000
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009001#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009002static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009003
9004/*
9005 * Get the float value of "argvars[0]" into "f".
9006 * Returns FAIL when the argument is not a Number or Float.
9007 */
9008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009009get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009010{
9011 if (argvars[0].v_type == VAR_FLOAT)
9012 {
9013 *f = argvars[0].vval.v_float;
9014 return OK;
9015 }
9016 if (argvars[0].v_type == VAR_NUMBER)
9017 {
9018 *f = (float_T)argvars[0].vval.v_number;
9019 return OK;
9020 }
9021 EMSG(_("E808: Number or Float required"));
9022 return FAIL;
9023}
9024
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009025/*
9026 * "abs(expr)" function
9027 */
9028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009029f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009030{
9031 if (argvars[0].v_type == VAR_FLOAT)
9032 {
9033 rettv->v_type = VAR_FLOAT;
9034 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9035 }
9036 else
9037 {
9038 varnumber_T n;
9039 int error = FALSE;
9040
9041 n = get_tv_number_chk(&argvars[0], &error);
9042 if (error)
9043 rettv->vval.v_number = -1;
9044 else if (n > 0)
9045 rettv->vval.v_number = n;
9046 else
9047 rettv->vval.v_number = -n;
9048 }
9049}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009050
9051/*
9052 * "acos()" function
9053 */
9054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009056{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009057 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009058
9059 rettv->v_type = VAR_FLOAT;
9060 if (get_float_arg(argvars, &f) == OK)
9061 rettv->vval.v_float = acos(f);
9062 else
9063 rettv->vval.v_float = 0.0;
9064}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009065#endif
9066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009068 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 */
9070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009071f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
Bram Moolenaar33570922005-01-25 22:26:29 +00009073 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009076 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009078 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009079 && !tv_check_lock(l->lv_lock,
9080 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009081 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009083 }
9084 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009085 EMSG(_(e_listreq));
9086}
9087
9088/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009089 * "alloc_fail(id, countdown, repeat)" function
9090 */
9091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009092f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009093{
9094 if (argvars[0].v_type != VAR_NUMBER
9095 || argvars[0].vval.v_number <= 0
9096 || argvars[1].v_type != VAR_NUMBER
9097 || argvars[1].vval.v_number < 0
9098 || argvars[2].v_type != VAR_NUMBER)
9099 EMSG(_(e_invarg));
9100 else
9101 {
9102 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009103 if (alloc_fail_id >= aid_last)
9104 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009105 alloc_fail_countdown = argvars[1].vval.v_number;
9106 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009107 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009108 }
9109}
9110
9111/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009112 * "and(expr, expr)" function
9113 */
9114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009115f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009116{
9117 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9118 & get_tv_number_chk(&argvars[1], NULL);
9119}
9120
9121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009122 * "append(lnum, string/list)" function
9123 */
9124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009125f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009126{
9127 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 list_T *l = NULL;
9130 listitem_T *li = NULL;
9131 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009132 long added = 0;
9133
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009134 /* When coming here from Insert mode, sync undo, so that this can be
9135 * undone separately from what was previously inserted. */
9136 if (u_sync_once == 2)
9137 {
9138 u_sync_once = 1; /* notify that u_sync() was called */
9139 u_sync(TRUE);
9140 }
9141
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142 lnum = get_tv_lnum(argvars);
9143 if (lnum >= 0
9144 && lnum <= curbuf->b_ml.ml_line_count
9145 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009146 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 l = argvars[1].vval.v_list;
9150 if (l == NULL)
9151 return;
9152 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009154 for (;;)
9155 {
9156 if (l == NULL)
9157 tv = &argvars[1]; /* append a string */
9158 else if (li == NULL)
9159 break; /* end of list */
9160 else
9161 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 line = get_tv_string_chk(tv);
9163 if (line == NULL) /* type error */
9164 {
9165 rettv->vval.v_number = 1; /* Failed */
9166 break;
9167 }
9168 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009169 ++added;
9170 if (l == NULL)
9171 break;
9172 li = li->li_next;
9173 }
9174
9175 appended_lines_mark(lnum, added);
9176 if (curwin->w_cursor.lnum > lnum)
9177 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 else
9180 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181}
9182
9183/*
9184 * "argc()" function
9185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
9193 * "argidx()" function
9194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009202 * "arglistid()" function
9203 */
9204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009205f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009206{
9207 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009208
9209 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009210 wp = find_tabwin(&argvars[0], &argvars[1]);
9211 if (wp != NULL)
9212 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009213}
9214
9215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 * "argv(nr)" function
9217 */
9218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009219f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 int idx;
9222
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009223 if (argvars[0].v_type != VAR_UNKNOWN)
9224 {
9225 idx = get_tv_number_chk(&argvars[0], NULL);
9226 if (idx >= 0 && idx < ARGCOUNT)
9227 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9228 else
9229 rettv->vval.v_string = NULL;
9230 rettv->v_type = VAR_STRING;
9231 }
9232 else if (rettv_list_alloc(rettv) == OK)
9233 for (idx = 0; idx < ARGCOUNT; ++idx)
9234 list_append_string(rettv->vval.v_list,
9235 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236}
9237
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009238static void prepare_assert_error(garray_T*gap);
9239static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9240static void assert_error(garray_T *gap);
9241static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009242
9243/*
9244 * Prepare "gap" for an assert error and add the sourcing position.
9245 */
9246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009247prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009248{
9249 char buf[NUMBUFLEN];
9250
9251 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009252 if (sourcing_name != NULL)
9253 {
9254 ga_concat(gap, sourcing_name);
9255 if (sourcing_lnum > 0)
9256 ga_concat(gap, (char_u *)" ");
9257 }
9258 if (sourcing_lnum > 0)
9259 {
9260 sprintf(buf, "line %ld", (long)sourcing_lnum);
9261 ga_concat(gap, (char_u *)buf);
9262 }
9263 if (sourcing_name != NULL || sourcing_lnum > 0)
9264 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265}
9266
9267/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009268 * Append "str" to "gap", escaping unprintable characters.
9269 * Changes NL to \n, CR to \r, etc.
9270 */
9271 static void
9272ga_concat_esc(garray_T *gap, char_u *str)
9273{
9274 char_u *p;
9275 char_u buf[NUMBUFLEN];
9276
9277 for (p = str; *p != NUL; ++p)
9278 switch (*p)
9279 {
9280 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9281 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9282 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9283 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9284 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9285 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9286 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9287 default:
9288 if (*p < ' ')
9289 {
9290 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9291 ga_concat(gap, buf);
9292 }
9293 else
9294 ga_append(gap, *p);
9295 break;
9296 }
9297}
9298
9299/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300 * Fill "gap" with information about an assert error.
9301 */
9302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009303fill_assert_error(
9304 garray_T *gap,
9305 typval_T *opt_msg_tv,
9306 char_u *exp_str,
9307 typval_T *exp_tv,
9308 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009309{
9310 char_u numbuf[NUMBUFLEN];
9311 char_u *tofree;
9312
9313 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9314 {
9315 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9316 vim_free(tofree);
9317 }
9318 else
9319 {
9320 ga_concat(gap, (char_u *)"Expected ");
9321 if (exp_str == NULL)
9322 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009323 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324 vim_free(tofree);
9325 }
9326 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009327 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009328 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009329 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009330 vim_free(tofree);
9331 }
9332}
Bram Moolenaar43345542015-11-29 17:35:35 +01009333
9334/*
9335 * Add an assert error to v:errors.
9336 */
9337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009338assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009339{
9340 struct vimvar *vp = &vimvars[VV_ERRORS];
9341
9342 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9343 /* Make sure v:errors is a list. */
9344 set_vim_var_list(VV_ERRORS, list_alloc());
9345 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9346}
9347
Bram Moolenaar43345542015-11-29 17:35:35 +01009348/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009350 */
9351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009352f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009353{
9354 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009355
9356 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9357 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009358 prepare_assert_error(&ga);
9359 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9360 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009361 ga_clear(&ga);
9362 }
9363}
9364
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009365/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009366 * "assert_exception(string[, msg])" function
9367 */
9368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009369f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009370{
9371 garray_T ga;
9372 char *error;
9373
9374 error = (char *)get_tv_string_chk(&argvars[0]);
9375 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9376 {
9377 prepare_assert_error(&ga);
9378 ga_concat(&ga, (char_u *)"v:exception is not set");
9379 assert_error(&ga);
9380 ga_clear(&ga);
9381 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009382 else if (error != NULL
9383 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009384 {
9385 prepare_assert_error(&ga);
9386 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9387 &vimvars[VV_EXCEPTION].vv_tv);
9388 assert_error(&ga);
9389 ga_clear(&ga);
9390 }
9391}
9392
9393/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009394 * "assert_fails(cmd [, error])" function
9395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009398{
9399 char_u *cmd = get_tv_string_chk(&argvars[0]);
9400 garray_T ga;
9401
9402 called_emsg = FALSE;
9403 suppress_errthrow = TRUE;
9404 emsg_silent = TRUE;
9405 do_cmdline_cmd(cmd);
9406 if (!called_emsg)
9407 {
9408 prepare_assert_error(&ga);
9409 ga_concat(&ga, (char_u *)"command did not fail: ");
9410 ga_concat(&ga, cmd);
9411 assert_error(&ga);
9412 ga_clear(&ga);
9413 }
9414 else if (argvars[1].v_type != VAR_UNKNOWN)
9415 {
9416 char_u buf[NUMBUFLEN];
9417 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9418
9419 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9420 {
9421 prepare_assert_error(&ga);
9422 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9423 &vimvars[VV_ERRMSG].vv_tv);
9424 assert_error(&ga);
9425 ga_clear(&ga);
9426 }
9427 }
9428
9429 called_emsg = FALSE;
9430 suppress_errthrow = FALSE;
9431 emsg_silent = FALSE;
9432 emsg_on_display = FALSE;
9433 set_vim_var_string(VV_ERRMSG, NULL, 0);
9434}
9435
9436/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009437 * Common for assert_true() and assert_false().
9438 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009440assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009441{
9442 int error = FALSE;
9443 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009444
Bram Moolenaar37127922016-02-06 20:29:28 +01009445 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009446 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009447 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009448 if (argvars[0].v_type != VAR_NUMBER
9449 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9450 || error)
9451 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009452 prepare_assert_error(&ga);
9453 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009454 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 NULL, &argvars[0]);
9456 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009457 ga_clear(&ga);
9458 }
9459}
9460
9461/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009466{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009468}
9469
9470/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009471 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009472 */
9473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009474f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009475{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009476 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009477}
9478
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009479#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009480/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009481 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009484f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009486 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009487
9488 rettv->v_type = VAR_FLOAT;
9489 if (get_float_arg(argvars, &f) == OK)
9490 rettv->vval.v_float = asin(f);
9491 else
9492 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493}
9494
9495/*
9496 * "atan()" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009500{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009501 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = atan(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009509
9510/*
9511 * "atan2()" function
9512 */
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009515{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009516 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009517
9518 rettv->v_type = VAR_FLOAT;
9519 if (get_float_arg(argvars, &fx) == OK
9520 && get_float_arg(&argvars[1], &fy) == OK)
9521 rettv->vval.v_float = atan2(fx, fy);
9522 else
9523 rettv->vval.v_float = 0.0;
9524}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009525#endif
9526
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527/*
9528 * "browse(save, title, initdir, default)" function
9529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009531f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
9533#ifdef FEAT_BROWSE
9534 int save;
9535 char_u *title;
9536 char_u *initdir;
9537 char_u *defname;
9538 char_u buf[NUMBUFLEN];
9539 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 save = get_tv_number_chk(&argvars[0], &error);
9543 title = get_tv_string_chk(&argvars[1]);
9544 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9545 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 if (error || title == NULL || initdir == NULL || defname == NULL)
9548 rettv->vval.v_string = NULL;
9549 else
9550 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009551 do_browse(save ? BROWSE_SAVE : 0,
9552 title, defname, NULL, initdir, NULL, curbuf);
9553#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009555#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557}
9558
9559/*
9560 * "browsedir(title, initdir)" function
9561 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009563f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564{
9565#ifdef FEAT_BROWSE
9566 char_u *title;
9567 char_u *initdir;
9568 char_u buf[NUMBUFLEN];
9569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 title = get_tv_string_chk(&argvars[0]);
9571 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 if (title == NULL || initdir == NULL)
9574 rettv->vval.v_string = NULL;
9575 else
9576 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009577 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582}
9583
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009584static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009585
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586/*
9587 * Find a buffer by number or exact name.
9588 */
9589 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
9592 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009594 if (avar->v_type == VAR_NUMBER)
9595 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009596 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009598 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009599 if (buf == NULL)
9600 {
9601 /* No full path name match, try a match with a URL or a "nofile"
9602 * buffer, these don't use the full path. */
9603 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9604 if (buf->b_fname != NULL
9605 && (path_with_url(buf->b_fname)
9606#ifdef FEAT_QUICKFIX
9607 || bt_nofile(buf)
9608#endif
9609 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009610 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009611 break;
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 }
9614 return buf;
9615}
9616
9617/*
9618 * "bufexists(expr)" function
9619 */
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
9626/*
9627 * "buflisted(expr)" function
9628 */
9629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
9632 buf_T *buf;
9633
9634 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636}
9637
9638/*
9639 * "bufloaded(expr)" function
9640 */
9641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009642f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
9644 buf_T *buf;
9645
9646 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009647 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648}
9649
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009650static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652/*
9653 * Get buffer by number or pattern.
9654 */
9655 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009656get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 int save_magic;
9660 char_u *save_cpo;
9661 buf_T *buf;
9662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009663 if (tv->v_type == VAR_NUMBER)
9664 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009665 if (tv->v_type != VAR_STRING)
9666 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 if (name == NULL || *name == NUL)
9668 return curbuf;
9669 if (name[0] == '$' && name[1] == NUL)
9670 return lastbuf;
9671
9672 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9673 save_magic = p_magic;
9674 p_magic = TRUE;
9675 save_cpo = p_cpo;
9676 p_cpo = (char_u *)"";
9677
9678 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009679 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680
9681 p_magic = save_magic;
9682 p_cpo = save_cpo;
9683
9684 /* If not found, try expanding the name, like done for bufexists(). */
9685 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687
9688 return buf;
9689}
9690
9691/*
9692 * "bufname(expr)" function
9693 */
9694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009695f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 buf_T *buf;
9698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009701 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 --emsg_off;
9708}
9709
9710/*
9711 * "bufnr(expr)" function
9712 */
9713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009717 int error = FALSE;
9718 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009722 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009723 --emsg_off;
9724
9725 /* If the buffer isn't found and the second argument is not zero create a
9726 * new buffer. */
9727 if (buf == NULL
9728 && argvars[1].v_type != VAR_UNKNOWN
9729 && get_tv_number_chk(&argvars[1], &error) != 0
9730 && !error
9731 && (name = get_tv_string_chk(&argvars[0])) != NULL
9732 && !error)
9733 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9734
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739}
9740
9741/*
9742 * "bufwinnr(nr)" function
9743 */
9744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009745f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747#ifdef FEAT_WINDOWS
9748 win_T *wp;
9749 int winnr = 0;
9750#endif
9751 buf_T *buf;
9752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009755 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#ifdef FEAT_WINDOWS
9757 for (wp = firstwin; wp; wp = wp->w_next)
9758 {
9759 ++winnr;
9760 if (wp->w_buffer == buf)
9761 break;
9762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#endif
9767 --emsg_off;
9768}
9769
9770/*
9771 * "byte2line(byte)" function
9772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009774f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
9776#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#else
9779 long boff = 0;
9780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 (linenr_T)0, &boff);
9787#endif
9788}
9789
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009791byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009792{
9793#ifdef FEAT_MBYTE
9794 char_u *t;
9795#endif
9796 char_u *str;
9797 long idx;
9798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 str = get_tv_string_chk(&argvars[0]);
9800 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803 return;
9804
9805#ifdef FEAT_MBYTE
9806 t = str;
9807 for ( ; idx > 0; idx--)
9808 {
9809 if (*t == NUL) /* EOL reached */
9810 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009811 if (enc_utf8 && comp)
9812 t += utf_ptr2len(t);
9813 else
9814 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009815 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009816 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009818 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820#endif
9821}
9822
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009823/*
9824 * "byteidx()" function
9825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009828{
9829 byteidx(argvars, rettv, FALSE);
9830}
9831
9832/*
9833 * "byteidxcomp()" function
9834 */
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009837{
9838 byteidx(argvars, rettv, TRUE);
9839}
9840
Bram Moolenaardb913952012-06-29 12:54:53 +02009841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842func_call(
9843 char_u *name,
9844 typval_T *args,
9845 dict_T *selfdict,
9846 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009847{
9848 listitem_T *item;
9849 typval_T argv[MAX_FUNC_ARGS + 1];
9850 int argc = 0;
9851 int dummy;
9852 int r = 0;
9853
9854 for (item = args->vval.v_list->lv_first; item != NULL;
9855 item = item->li_next)
9856 {
9857 if (argc == MAX_FUNC_ARGS)
9858 {
9859 EMSG(_("E699: Too many arguments"));
9860 break;
9861 }
9862 /* Make a copy of each argument. This is needed to be able to set
9863 * v_lock to VAR_FIXED in the copy without changing the original list.
9864 */
9865 copy_tv(&item->li_tv, &argv[argc++]);
9866 }
9867
9868 if (item == NULL)
9869 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9870 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9871 &dummy, TRUE, selfdict);
9872
9873 /* Free the arguments. */
9874 while (argc > 0)
9875 clear_tv(&argv[--argc]);
9876
9877 return r;
9878}
9879
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009880/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009881 * "call(func, arglist)" function
9882 */
9883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885{
9886 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009887 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009888
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009889 if (argvars[1].v_type != VAR_LIST)
9890 {
9891 EMSG(_(e_listreq));
9892 return;
9893 }
9894 if (argvars[1].vval.v_list == NULL)
9895 return;
9896
9897 if (argvars[0].v_type == VAR_FUNC)
9898 func = argvars[0].vval.v_string;
9899 else
9900 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 if (*func == NUL)
9902 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009903
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 if (argvars[2].v_type != VAR_UNKNOWN)
9905 {
9906 if (argvars[2].v_type != VAR_DICT)
9907 {
9908 EMSG(_(e_dictreq));
9909 return;
9910 }
9911 selfdict = argvars[2].vval.v_dict;
9912 }
9913
Bram Moolenaardb913952012-06-29 12:54:53 +02009914 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915}
9916
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009917#ifdef FEAT_FLOAT
9918/*
9919 * "ceil({float})" function
9920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009923{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009924 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009925
9926 rettv->v_type = VAR_FLOAT;
9927 if (get_float_arg(argvars, &f) == OK)
9928 rettv->vval.v_float = ceil(f);
9929 else
9930 rettv->vval.v_float = 0.0;
9931}
9932#endif
9933
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009934#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009935/*
9936 * Get a callback from "arg". It can be a Funcref or a function name.
9937 * When "arg" is zero return an empty string.
9938 * Return NULL for an invalid argument.
9939 */
9940 static char_u *
9941get_callback(typval_T *arg)
9942{
9943 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9944 return arg->vval.v_string;
9945 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9946 return (char_u *)"";
9947 EMSG(_("E999: Invalid callback argument"));
9948 return NULL;
9949}
9950
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009951 static int
9952handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9953{
9954 char_u *val = get_tv_string(item);
9955
9956 opt->jo_set |= jo;
9957 if (STRCMP(val, "nl") == 0)
9958 *modep = MODE_NL;
9959 else if (STRCMP(val, "raw") == 0)
9960 *modep = MODE_RAW;
9961 else if (STRCMP(val, "js") == 0)
9962 *modep = MODE_JS;
9963 else if (STRCMP(val, "json") == 0)
9964 *modep = MODE_JSON;
9965 else
9966 {
9967 EMSG2(_(e_invarg2), val);
9968 return FAIL;
9969 }
9970 return OK;
9971}
9972
Bram Moolenaar187db502016-02-27 14:44:26 +01009973 static int
9974handle_io(typval_T *item, int part, jobopt_T *opt)
9975{
9976 char_u *val = get_tv_string(item);
9977
9978 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9979 if (STRCMP(val, "null") == 0)
9980 opt->jo_io[part] = JIO_NULL;
9981 else if (STRCMP(val, "pipe") == 0)
9982 opt->jo_io[part] = JIO_PIPE;
9983 else if (STRCMP(val, "file") == 0)
9984 opt->jo_io[part] = JIO_FILE;
9985 else if (STRCMP(val, "buffer") == 0)
9986 opt->jo_io[part] = JIO_BUFFER;
9987 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9988 opt->jo_io[part] = JIO_OUT;
9989 else
9990 {
9991 EMSG2(_(e_invarg2), val);
9992 return FAIL;
9993 }
9994 return OK;
9995}
9996
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009997 static void
9998clear_job_options(jobopt_T *opt)
9999{
10000 vim_memset(opt, 0, sizeof(jobopt_T));
10001}
10002
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010003/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010004 * Get the PART_ number from the first character of an option name.
10005 */
10006 static int
10007part_from_char(int c)
10008{
10009 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10010}
10011
10012/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010013 * Get the option entries from the dict in "tv", parse them and put the result
10014 * in "opt".
10015 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010016 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010017 */
10018 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010019get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010020{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010021 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010022 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010023 dict_T *dict;
10024 int todo;
10025 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010026 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010027
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010028 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010029 if (tv->v_type == VAR_UNKNOWN)
10030 return OK;
10031 if (tv->v_type != VAR_DICT)
10032 {
10033 EMSG(_(e_invarg));
10034 return FAIL;
10035 }
10036 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010037 if (dict == NULL)
10038 return OK;
10039
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010040 todo = (int)dict->dv_hashtab.ht_used;
10041 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10042 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010043 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010044 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010045
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010046 if (STRCMP(hi->hi_key, "mode") == 0)
10047 {
10048 if (!(supported & JO_MODE))
10049 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010050 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010051 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010052 }
10053 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10054 {
10055 if (!(supported & JO_IN_MODE))
10056 break;
10057 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10058 == FAIL)
10059 return FAIL;
10060 }
10061 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10062 {
10063 if (!(supported & JO_OUT_MODE))
10064 break;
10065 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10066 == FAIL)
10067 return FAIL;
10068 }
10069 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10070 {
10071 if (!(supported & JO_ERR_MODE))
10072 break;
10073 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10074 == FAIL)
10075 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010076 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010077 else if (STRCMP(hi->hi_key, "in-io") == 0
10078 || STRCMP(hi->hi_key, "out-io") == 0
10079 || STRCMP(hi->hi_key, "err-io") == 0)
10080 {
10081 if (!(supported & JO_OUT_IO))
10082 break;
10083 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10084 return FAIL;
10085 }
10086 else if (STRCMP(hi->hi_key, "in-name") == 0
10087 || STRCMP(hi->hi_key, "out-name") == 0
10088 || STRCMP(hi->hi_key, "err-name") == 0)
10089 {
10090 part = part_from_char(*hi->hi_key);
10091
10092 if (!(supported & JO_OUT_IO))
10093 break;
10094 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10095 opt->jo_io_name[part] =
10096 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10097 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010098 else if (STRCMP(hi->hi_key, "callback") == 0)
10099 {
10100 if (!(supported & JO_CALLBACK))
10101 break;
10102 opt->jo_set |= JO_CALLBACK;
10103 opt->jo_callback = get_callback(item);
10104 if (opt->jo_callback == NULL)
10105 {
10106 EMSG2(_(e_invarg2), "callback");
10107 return FAIL;
10108 }
10109 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010110 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10111 {
10112 if (!(supported & JO_OUT_CALLBACK))
10113 break;
10114 opt->jo_set |= JO_OUT_CALLBACK;
10115 opt->jo_out_cb = get_callback(item);
10116 if (opt->jo_out_cb == NULL)
10117 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010118 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010119 return FAIL;
10120 }
10121 }
10122 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10123 {
10124 if (!(supported & JO_ERR_CALLBACK))
10125 break;
10126 opt->jo_set |= JO_ERR_CALLBACK;
10127 opt->jo_err_cb = get_callback(item);
10128 if (opt->jo_err_cb == NULL)
10129 {
10130 EMSG2(_(e_invarg2), "err-cb");
10131 return FAIL;
10132 }
10133 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010134 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10135 {
10136 if (!(supported & JO_CLOSE_CALLBACK))
10137 break;
10138 opt->jo_set |= JO_CLOSE_CALLBACK;
10139 opt->jo_close_cb = get_callback(item);
10140 if (opt->jo_close_cb == NULL)
10141 {
10142 EMSG2(_(e_invarg2), "close-cb");
10143 return FAIL;
10144 }
10145 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010146 else if (STRCMP(hi->hi_key, "waittime") == 0)
10147 {
10148 if (!(supported & JO_WAITTIME))
10149 break;
10150 opt->jo_set |= JO_WAITTIME;
10151 opt->jo_waittime = get_tv_number(item);
10152 }
10153 else if (STRCMP(hi->hi_key, "timeout") == 0)
10154 {
10155 if (!(supported & JO_TIMEOUT))
10156 break;
10157 opt->jo_set |= JO_TIMEOUT;
10158 opt->jo_timeout = get_tv_number(item);
10159 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010160 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10161 {
10162 if (!(supported & JO_OUT_TIMEOUT))
10163 break;
10164 opt->jo_set |= JO_OUT_TIMEOUT;
10165 opt->jo_out_timeout = get_tv_number(item);
10166 }
10167 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10168 {
10169 if (!(supported & JO_ERR_TIMEOUT))
10170 break;
10171 opt->jo_set |= JO_ERR_TIMEOUT;
10172 opt->jo_err_timeout = get_tv_number(item);
10173 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010174 else if (STRCMP(hi->hi_key, "part") == 0)
10175 {
10176 if (!(supported & JO_PART))
10177 break;
10178 opt->jo_set |= JO_PART;
10179 val = get_tv_string(item);
10180 if (STRCMP(val, "err") == 0)
10181 opt->jo_part = PART_ERR;
10182 else
10183 {
10184 EMSG2(_(e_invarg2), val);
10185 return FAIL;
10186 }
10187 }
10188 else if (STRCMP(hi->hi_key, "id") == 0)
10189 {
10190 if (!(supported & JO_ID))
10191 break;
10192 opt->jo_set |= JO_ID;
10193 opt->jo_id = get_tv_number(item);
10194 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010195 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10196 {
10197 if (!(supported & JO_STOPONEXIT))
10198 break;
10199 opt->jo_set |= JO_STOPONEXIT;
10200 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10201 opt->jo_soe_buf);
10202 if (opt->jo_stoponexit == NULL)
10203 {
10204 EMSG2(_(e_invarg2), "stoponexit");
10205 return FAIL;
10206 }
10207 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010208 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10209 {
10210 if (!(supported & JO_EXIT_CB))
10211 break;
10212 opt->jo_set |= JO_EXIT_CB;
10213 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010214 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010215 {
10216 EMSG2(_(e_invarg2), "exit-cb");
10217 return FAIL;
10218 }
10219 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010220 else
10221 break;
10222 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010223 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010224 if (todo > 0)
10225 {
10226 EMSG2(_(e_invarg2), hi->hi_key);
10227 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010228 }
10229
Bram Moolenaar187db502016-02-27 14:44:26 +010010230 for (part = PART_OUT; part <= PART_IN; ++part)
10231 if (opt->jo_io[part] == JIO_BUFFER && opt->jo_io_name[part] == NULL)
10232 {
10233 EMSG(_("E915: Missing name for buffer"));
10234 return FAIL;
10235 }
10236
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010237 return OK;
10238}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010239#endif
10240
10241#ifdef FEAT_CHANNEL
10242/*
10243 * Get the channel from the argument.
10244 * Returns NULL if the handle is invalid.
10245 */
10246 static channel_T *
10247get_channel_arg(typval_T *tv)
10248{
10249 channel_T *channel;
10250
10251 if (tv->v_type != VAR_CHANNEL)
10252 {
10253 EMSG2(_(e_invarg2), get_tv_string(tv));
10254 return NULL;
10255 }
10256 channel = tv->vval.v_channel;
10257
10258 if (channel == NULL || !channel_is_open(channel))
10259 {
10260 EMSG(_("E906: not an open channel"));
10261 return NULL;
10262 }
10263 return channel;
10264}
10265
10266/*
10267 * "ch_close()" function
10268 */
10269 static void
10270f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10271{
10272 channel_T *channel = get_channel_arg(&argvars[0]);
10273
10274 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010275 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010276 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010277 channel_clear(channel);
10278 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010279}
10280
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010281# ifdef FEAT_JOB
10282/*
10283 * "ch_getjob()" function
10284 */
10285 static void
10286f_ch_getjob(typval_T *argvars, typval_T *rettv)
10287{
10288 channel_T *channel = get_channel_arg(&argvars[0]);
10289
10290 if (channel != NULL)
10291 {
10292 rettv->v_type = VAR_JOB;
10293 rettv->vval.v_job = channel->ch_job;
10294 if (channel->ch_job != NULL)
10295 ++channel->ch_job->jv_refcount;
10296 }
10297}
10298# endif
10299
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010300/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010301 * "ch_log()" function
10302 */
10303 static void
10304f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10305{
10306 char_u *msg = get_tv_string(&argvars[0]);
10307 channel_T *channel = NULL;
10308
10309 if (argvars[1].v_type != VAR_UNKNOWN)
10310 channel = get_channel_arg(&argvars[1]);
10311
10312 ch_log(channel, (char *)msg);
10313}
10314
10315/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010316 * "ch_logfile()" function
10317 */
10318 static void
10319f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10320{
10321 char_u *fname;
10322 char_u *opt = (char_u *)"";
10323 char_u buf[NUMBUFLEN];
10324 FILE *file = NULL;
10325
10326 fname = get_tv_string(&argvars[0]);
10327 if (argvars[1].v_type == VAR_STRING)
10328 opt = get_tv_string_buf(&argvars[1], buf);
10329 if (*fname != NUL)
10330 {
10331 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10332 if (file == NULL)
10333 {
10334 EMSG2(_(e_notopen), fname);
10335 return;
10336 }
10337 }
10338 ch_logfile(file);
10339}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010340
10341/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010342 * "ch_open()" function
10343 */
10344 static void
10345f_ch_open(typval_T *argvars, typval_T *rettv)
10346{
10347 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010348 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010349 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010350 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010351 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010352 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010353
10354 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010355 rettv->v_type = VAR_CHANNEL;
10356 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010357
10358 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010359 if (argvars[1].v_type != VAR_UNKNOWN
10360 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010361 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010362 EMSG(_(e_invarg));
10363 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010364 }
10365
10366 /* parse address */
10367 p = vim_strchr(address, ':');
10368 if (p == NULL)
10369 {
10370 EMSG2(_(e_invarg2), address);
10371 return;
10372 }
10373 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010374 port = strtol((char *)p, &rest, 10);
10375 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010376 {
10377 p[-1] = ':';
10378 EMSG2(_(e_invarg2), address);
10379 return;
10380 }
10381
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010382 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010383 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010384 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010385 opt.jo_timeout = 2000;
10386 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010387 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010388 return;
10389 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010390 {
10391 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010392 return;
10393 }
10394
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010395 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010396 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010397 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010398 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010399 opt.jo_set = JO_ALL;
10400 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010401 }
10402}
10403
10404/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010405 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010406 */
10407 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010408common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010409{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010410 channel_T *channel;
10411 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010412 jobopt_T opt;
10413 int mode;
10414 int timeout;
10415 int id = -1;
10416 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010417
10418 /* return an empty string by default */
10419 rettv->v_type = VAR_STRING;
10420 rettv->vval.v_string = NULL;
10421
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010422 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010423 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10424 == FAIL)
10425 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010426
Bram Moolenaar77073442016-02-13 23:23:53 +010010427 channel = get_channel_arg(&argvars[0]);
10428 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010429 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010430 if (opt.jo_set & JO_PART)
10431 part = opt.jo_part;
10432 else
10433 part = channel_part_read(channel);
10434 mode = channel_get_mode(channel, part);
10435 timeout = channel_get_timeout(channel, part);
10436 if (opt.jo_set & JO_TIMEOUT)
10437 timeout = opt.jo_timeout;
10438
10439 if (raw || mode == MODE_RAW || mode == MODE_NL)
10440 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10441 else
10442 {
10443 if (opt.jo_set & JO_ID)
10444 id = opt.jo_id;
10445 channel_read_json_block(channel, part, timeout, id, &listtv);
10446 if (listtv != NULL)
10447 *rettv = *listtv;
10448 else
10449 {
10450 rettv->v_type = VAR_SPECIAL;
10451 rettv->vval.v_number = VVAL_NONE;
10452 }
10453 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010454 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010455}
10456
10457/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010458 * "ch_read()" function
10459 */
10460 static void
10461f_ch_read(typval_T *argvars, typval_T *rettv)
10462{
10463 common_channel_read(argvars, rettv, FALSE);
10464}
10465
10466/*
10467 * "ch_readraw()" function
10468 */
10469 static void
10470f_ch_readraw(typval_T *argvars, typval_T *rettv)
10471{
10472 common_channel_read(argvars, rettv, TRUE);
10473}
10474
10475/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010476 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010477 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010478 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010479 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010480 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010481 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010482send_common(
10483 typval_T *argvars,
10484 char_u *text,
10485 int id,
10486 int eval,
10487 char *fun,
10488 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010489{
Bram Moolenaar77073442016-02-13 23:23:53 +010010490 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010491 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010492 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010493
Bram Moolenaar77073442016-02-13 23:23:53 +010010494 channel = get_channel_arg(&argvars[0]);
10495 if (channel == NULL)
10496 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010497 part_send = channel_part_send(channel);
10498 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010499
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010500 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010501 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10502 return NULL;
10503
Bram Moolenaara07fec92016-02-05 21:04:08 +010010504 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010505 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10506 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010507 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010508 {
10509 if (eval)
10510 {
10511 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10512 return NULL;
10513 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010514 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010515 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010516
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010517 if (channel_send(channel, part_send, text, fun) == OK
10518 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010519 return channel;
10520 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010521}
10522
10523/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010524 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010525 */
10526 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010527ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010528{
10529 char_u *text;
10530 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010531 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010532 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010533 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010534 int part_send;
10535 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010536 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010537
10538 /* return an empty string by default */
10539 rettv->v_type = VAR_STRING;
10540 rettv->vval.v_string = NULL;
10541
Bram Moolenaar77073442016-02-13 23:23:53 +010010542 channel = get_channel_arg(&argvars[0]);
10543 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010544 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010545 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010546
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010547 ch_mode = channel_get_mode(channel, part_send);
10548 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010549 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010550 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010551 return;
10552 }
10553
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010554 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010555 text = json_encode_nr_expr(id, &argvars[1],
10556 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010557 if (text == NULL)
10558 return;
10559
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010560 channel = send_common(argvars, text, id, eval,
10561 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010562 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010563 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010565 /* TODO: timeout from options */
10566 timeout = channel_get_timeout(channel, part_read);
10567 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10568 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010569 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010570 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010571
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010572 /* Move the item from the list and then change the type to
10573 * avoid the value being freed. */
10574 *rettv = list->lv_last->li_tv;
10575 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010576 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010577 }
10578 }
10579}
10580
10581/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010582 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010583 */
10584 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010585f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10586{
10587 ch_expr_common(argvars, rettv, TRUE);
10588}
10589
10590/*
10591 * "ch_sendexpr()" function
10592 */
10593 static void
10594f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10595{
10596 ch_expr_common(argvars, rettv, FALSE);
10597}
10598
10599/*
10600 * common for "ch_evalraw()" and "ch_sendraw()"
10601 */
10602 static void
10603ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010604{
10605 char_u buf[NUMBUFLEN];
10606 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010607 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010608 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010609 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010610
10611 /* return an empty string by default */
10612 rettv->v_type = VAR_STRING;
10613 rettv->vval.v_string = NULL;
10614
10615 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010616 channel = send_common(argvars, text, 0, eval,
10617 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10618 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010619 {
10620 /* TODO: timeout from options */
10621 timeout = channel_get_timeout(channel, part_read);
10622 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10623 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010624}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010625
10626/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010627 * "ch_evalraw()" function
10628 */
10629 static void
10630f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10631{
10632 ch_raw_common(argvars, rettv, TRUE);
10633}
10634
10635/*
10636 * "ch_sendraw()" function
10637 */
10638 static void
10639f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10640{
10641 ch_raw_common(argvars, rettv, FALSE);
10642}
10643
10644/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010645 * "ch_setoptions()" function
10646 */
10647 static void
10648f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10649{
10650 channel_T *channel;
10651 jobopt_T opt;
10652
10653 channel = get_channel_arg(&argvars[0]);
10654 if (channel == NULL)
10655 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010656 clear_job_options(&opt);
10657 if (get_job_options(&argvars[1], &opt,
10658 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010659 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010660 channel_set_options(channel, &opt);
10661}
10662
10663/*
10664 * "ch_status()" function
10665 */
10666 static void
10667f_ch_status(typval_T *argvars, typval_T *rettv)
10668{
10669 /* return an empty string by default */
10670 rettv->v_type = VAR_STRING;
10671
10672 if (argvars[0].v_type != VAR_CHANNEL)
10673 {
10674 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10675 rettv->vval.v_string = NULL;
10676 }
10677 else
10678 rettv->vval.v_string = vim_strsave(
10679 (char_u *)channel_status(argvars[0].vval.v_channel));
10680}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010681#endif
10682
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010683/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010684 * "changenr()" function
10685 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010688{
10689 rettv->vval.v_number = curbuf->b_u_seq_cur;
10690}
10691
10692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 * "char2nr(string)" function
10694 */
10695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010696f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697{
10698#ifdef FEAT_MBYTE
10699 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010700 {
10701 int utf8 = 0;
10702
10703 if (argvars[1].v_type != VAR_UNKNOWN)
10704 utf8 = get_tv_number_chk(&argvars[1], NULL);
10705
10706 if (utf8)
10707 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10708 else
10709 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 else
10712#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
10716/*
10717 * "cindent(lnum)" function
10718 */
10719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010720f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
10722#ifdef FEAT_CINDENT
10723 pos_T pos;
10724 linenr_T lnum;
10725
10726 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10729 {
10730 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 curwin->w_cursor = pos;
10733 }
10734 else
10735#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
10739/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010740 * "clearmatches()" function
10741 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010743f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010744{
10745#ifdef FEAT_SEARCH_EXTRA
10746 clear_matches(curwin);
10747#endif
10748}
10749
10750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 * "col(string)" function
10752 */
10753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010754f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755{
10756 colnr_T col = 0;
10757 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010758 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010760 fp = var2fpos(&argvars[0], FALSE, &fnum);
10761 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 {
10763 if (fp->col == MAXCOL)
10764 {
10765 /* '> can be MAXCOL, get the length of the line then */
10766 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010767 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
10769 col = MAXCOL;
10770 }
10771 else
10772 {
10773 col = fp->col + 1;
10774#ifdef FEAT_VIRTUALEDIT
10775 /* col(".") when the cursor is on the NUL at the end of the line
10776 * because of "coladd" can be seen as an extra column. */
10777 if (virtual_active() && fp == &curwin->w_cursor)
10778 {
10779 char_u *p = ml_get_cursor();
10780
10781 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10782 curwin->w_virtcol - curwin->w_cursor.coladd))
10783 {
10784# ifdef FEAT_MBYTE
10785 int l;
10786
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010787 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 col += l;
10789# else
10790 if (*p != NUL && p[1] == NUL)
10791 ++col;
10792# endif
10793 }
10794 }
10795#endif
10796 }
10797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799}
10800
Bram Moolenaar572cb562005-08-05 21:35:02 +000010801#if defined(FEAT_INS_EXPAND)
10802/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010803 * "complete()" function
10804 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010806f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010807{
10808 int startcol;
10809
10810 if ((State & INSERT) == 0)
10811 {
10812 EMSG(_("E785: complete() can only be used in Insert mode"));
10813 return;
10814 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010815
10816 /* Check for undo allowed here, because if something was already inserted
10817 * the line was already saved for undo and this check isn't done. */
10818 if (!undo_allowed())
10819 return;
10820
Bram Moolenaarade00832006-03-10 21:46:58 +000010821 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10822 {
10823 EMSG(_(e_invarg));
10824 return;
10825 }
10826
10827 startcol = get_tv_number_chk(&argvars[0], NULL);
10828 if (startcol <= 0)
10829 return;
10830
10831 set_completion(startcol - 1, argvars[1].vval.v_list);
10832}
10833
10834/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010835 * "complete_add()" function
10836 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010838f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010839{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010840 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010841}
10842
10843/*
10844 * "complete_check()" function
10845 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010848{
10849 int saved = RedrawingDisabled;
10850
10851 RedrawingDisabled = 0;
10852 ins_compl_check_keys(0);
10853 rettv->vval.v_number = compl_interrupted;
10854 RedrawingDisabled = saved;
10855}
10856#endif
10857
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858/*
10859 * "confirm(message, buttons[, default [, type]])" function
10860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863{
10864#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10865 char_u *message;
10866 char_u *buttons = NULL;
10867 char_u buf[NUMBUFLEN];
10868 char_u buf2[NUMBUFLEN];
10869 int def = 1;
10870 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 char_u *typestr;
10872 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 message = get_tv_string_chk(&argvars[0]);
10875 if (message == NULL)
10876 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010879 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10880 if (buttons == NULL)
10881 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010885 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010887 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10888 if (typestr == NULL)
10889 error = TRUE;
10890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 switch (TOUPPER_ASC(*typestr))
10893 {
10894 case 'E': type = VIM_ERROR; break;
10895 case 'Q': type = VIM_QUESTION; break;
10896 case 'I': type = VIM_INFO; break;
10897 case 'W': type = VIM_WARNING; break;
10898 case 'G': type = VIM_GENERIC; break;
10899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901 }
10902 }
10903 }
10904
10905 if (buttons == NULL || *buttons == NUL)
10906 buttons = (char_u *)_("&Ok");
10907
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010908 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010910 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#endif
10912}
10913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010914/*
10915 * "copy()" function
10916 */
10917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010918f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010919{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010920 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010921}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010923#ifdef FEAT_FLOAT
10924/*
10925 * "cos()" function
10926 */
10927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010928f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010929{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010930 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010931
10932 rettv->v_type = VAR_FLOAT;
10933 if (get_float_arg(argvars, &f) == OK)
10934 rettv->vval.v_float = cos(f);
10935 else
10936 rettv->vval.v_float = 0.0;
10937}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010938
10939/*
10940 * "cosh()" function
10941 */
10942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010943f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010944{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010945 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010946
10947 rettv->v_type = VAR_FLOAT;
10948 if (get_float_arg(argvars, &f) == OK)
10949 rettv->vval.v_float = cosh(f);
10950 else
10951 rettv->vval.v_float = 0.0;
10952}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010953#endif
10954
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010956 * "count()" function
10957 */
10958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010959f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010960{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010961 long n = 0;
10962 int ic = FALSE;
10963
Bram Moolenaare9a41262005-01-15 22:18:47 +000010964 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 listitem_T *li;
10967 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010969
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 if ((l = argvars[0].vval.v_list) != NULL)
10971 {
10972 li = l->lv_first;
10973 if (argvars[2].v_type != VAR_UNKNOWN)
10974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 int error = FALSE;
10976
10977 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 if (argvars[3].v_type != VAR_UNKNOWN)
10979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 idx = get_tv_number_chk(&argvars[3], &error);
10981 if (!error)
10982 {
10983 li = list_find(l, idx);
10984 if (li == NULL)
10985 EMSGN(_(e_listidx), idx);
10986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 if (error)
10989 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010990 }
10991
10992 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010993 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994 ++n;
10995 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 else if (argvars[0].v_type == VAR_DICT)
10998 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 int todo;
11000 dict_T *d;
11001 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011002
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011003 if ((d = argvars[0].vval.v_dict) != NULL)
11004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 int error = FALSE;
11006
Bram Moolenaare9a41262005-01-15 22:18:47 +000011007 if (argvars[2].v_type != VAR_UNKNOWN)
11008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 if (argvars[3].v_type != VAR_UNKNOWN)
11011 EMSG(_(e_invarg));
11012 }
11013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011014 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011015 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011016 {
11017 if (!HASHITEM_EMPTY(hi))
11018 {
11019 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011020 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011021 ++n;
11022 }
11023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 }
11025 }
11026 else
11027 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011028 rettv->vval.v_number = n;
11029}
11030
11031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11033 *
11034 * Checks the existence of a cscope connection.
11035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011037f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038{
11039#ifdef FEAT_CSCOPE
11040 int num = 0;
11041 char_u *dbpath = NULL;
11042 char_u *prepend = NULL;
11043 char_u buf[NUMBUFLEN];
11044
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045 if (argvars[0].v_type != VAR_UNKNOWN
11046 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 num = (int)get_tv_number(&argvars[0]);
11049 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011050 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 }
11053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055#endif
11056}
11057
11058/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011059 * "cursor(lnum, col)" function, or
11060 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011062 * Moves the cursor to the specified line and column.
11063 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011066f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067{
11068 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011069#ifdef FEAT_VIRTUALEDIT
11070 long coladd = 0;
11071#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011072 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011074 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011075 if (argvars[1].v_type == VAR_UNKNOWN)
11076 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011077 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011078 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011079
Bram Moolenaar493c1782014-05-28 14:34:46 +020011080 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011081 {
11082 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011083 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011084 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011085 line = pos.lnum;
11086 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011087#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011088 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011089#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011090 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011091 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011092 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011093 set_curswant = FALSE;
11094 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011095 }
11096 else
11097 {
11098 line = get_tv_lnum(argvars);
11099 col = get_tv_number_chk(&argvars[1], NULL);
11100#ifdef FEAT_VIRTUALEDIT
11101 if (argvars[2].v_type != VAR_UNKNOWN)
11102 coladd = get_tv_number_chk(&argvars[2], NULL);
11103#endif
11104 }
11105 if (line < 0 || col < 0
11106#ifdef FEAT_VIRTUALEDIT
11107 || coladd < 0
11108#endif
11109 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 if (line > 0)
11112 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 if (col > 0)
11114 curwin->w_cursor.col = col - 1;
11115#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011116 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117#endif
11118
11119 /* Make sure the cursor is in a valid position. */
11120 check_cursor();
11121#ifdef FEAT_MBYTE
11122 /* Correct cursor for multi-byte character. */
11123 if (has_mbyte)
11124 mb_adjust_cursor();
11125#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011126
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011127 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011128 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129}
11130
11131/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011132 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 */
11134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011135f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011137 int noref = 0;
11138
11139 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011141 if (noref < 0 || noref > 1)
11142 EMSG(_(e_invarg));
11143 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011144 {
11145 current_copyID += COPYID_INC;
11146 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148}
11149
11150/*
11151 * "delete()" function
11152 */
11153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011154f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011156 char_u nbuf[NUMBUFLEN];
11157 char_u *name;
11158 char_u *flags;
11159
11160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011162 return;
11163
11164 name = get_tv_string(&argvars[0]);
11165 if (name == NULL || *name == NUL)
11166 {
11167 EMSG(_(e_invarg));
11168 return;
11169 }
11170
11171 if (argvars[1].v_type != VAR_UNKNOWN)
11172 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011174 flags = (char_u *)"";
11175
11176 if (*flags == NUL)
11177 /* delete a file */
11178 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11179 else if (STRCMP(flags, "d") == 0)
11180 /* delete an empty directory */
11181 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11182 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011183 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011184 rettv->vval.v_number = delete_recursive(name);
11185 else
11186 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187}
11188
11189/*
11190 * "did_filetype()" function
11191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011193f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194{
11195#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197#endif
11198}
11199
11200/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011201 * "diff_filler()" function
11202 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011204f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011205{
11206#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011208#endif
11209}
11210
11211/*
11212 * "diff_hlID()" function
11213 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011215f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011216{
11217#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011219 static linenr_T prev_lnum = 0;
11220 static int changedtick = 0;
11221 static int fnum = 0;
11222 static int change_start = 0;
11223 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011224 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011225 int filler_lines;
11226 int col;
11227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 if (lnum < 0) /* ignore type error in {lnum} arg */
11229 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011230 if (lnum != prev_lnum
11231 || changedtick != curbuf->b_changedtick
11232 || fnum != curbuf->b_fnum)
11233 {
11234 /* New line, buffer, change: need to get the values. */
11235 filler_lines = diff_check(curwin, lnum);
11236 if (filler_lines < 0)
11237 {
11238 if (filler_lines == -1)
11239 {
11240 change_start = MAXCOL;
11241 change_end = -1;
11242 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11243 hlID = HLF_ADD; /* added line */
11244 else
11245 hlID = HLF_CHD; /* changed line */
11246 }
11247 else
11248 hlID = HLF_ADD; /* added line */
11249 }
11250 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011251 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011252 prev_lnum = lnum;
11253 changedtick = curbuf->b_changedtick;
11254 fnum = curbuf->b_fnum;
11255 }
11256
11257 if (hlID == HLF_CHD || hlID == HLF_TXD)
11258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011259 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011260 if (col >= change_start && col <= change_end)
11261 hlID = HLF_TXD; /* changed text */
11262 else
11263 hlID = HLF_CHD; /* changed line */
11264 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011265 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011266#endif
11267}
11268
11269/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011270 * "disable_char_avail_for_testing({expr})" function
11271 */
11272 static void
11273f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11274{
11275 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11276}
11277
11278/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011279 * "empty({expr})" function
11280 */
11281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011282f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011283{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011284 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011285
11286 switch (argvars[0].v_type)
11287 {
11288 case VAR_STRING:
11289 case VAR_FUNC:
11290 n = argvars[0].vval.v_string == NULL
11291 || *argvars[0].vval.v_string == NUL;
11292 break;
11293 case VAR_NUMBER:
11294 n = argvars[0].vval.v_number == 0;
11295 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011296 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011297#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011298 n = argvars[0].vval.v_float == 0.0;
11299 break;
11300#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011301 case VAR_LIST:
11302 n = argvars[0].vval.v_list == NULL
11303 || argvars[0].vval.v_list->lv_first == NULL;
11304 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011305 case VAR_DICT:
11306 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011308 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011309 case VAR_SPECIAL:
11310 n = argvars[0].vval.v_number != VVAL_TRUE;
11311 break;
11312
Bram Moolenaar835dc632016-02-07 14:27:38 +010011313 case VAR_JOB:
11314#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011315 n = argvars[0].vval.v_job == NULL
11316 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11317 break;
11318#endif
11319 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011320#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011321 n = argvars[0].vval.v_channel == NULL
11322 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011323 break;
11324#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011325 case VAR_UNKNOWN:
11326 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11327 n = TRUE;
11328 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011329 }
11330
11331 rettv->vval.v_number = n;
11332}
11333
11334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 * "escape({string}, {chars})" function
11336 */
11337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011338f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339{
11340 char_u buf[NUMBUFLEN];
11341
Bram Moolenaar758711c2005-02-02 23:11:38 +000011342 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11343 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345}
11346
11347/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011348 * "eval()" function
11349 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011351f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011352{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011353 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 s = get_tv_string_chk(&argvars[0]);
11356 if (s != NULL)
11357 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011358
Bram Moolenaar615b9972015-01-14 17:15:05 +010011359 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11361 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011362 if (p != NULL && !aborting())
11363 EMSG2(_(e_invexpr2), p);
11364 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011368 else if (*s != NUL)
11369 EMSG(_(e_trailing));
11370}
11371
11372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 * "eventhandler()" function
11374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011376f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379}
11380
11381/*
11382 * "executable()" function
11383 */
11384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011385f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011387 char_u *name = get_tv_string(&argvars[0]);
11388
11389 /* Check in $PATH and also check directly if there is a directory name. */
11390 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11391 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011392}
11393
11394/*
11395 * "exepath()" function
11396 */
11397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011398f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011399{
11400 char_u *p = NULL;
11401
Bram Moolenaarb5971142015-03-21 17:32:19 +010011402 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011403 rettv->v_type = VAR_STRING;
11404 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405}
11406
11407/*
11408 * "exists()" function
11409 */
11410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011411f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
11413 char_u *p;
11414 char_u *name;
11415 int n = FALSE;
11416 int len = 0;
11417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 if (*p == '$') /* environment variable */
11420 {
11421 /* first try "normal" environment variables (fast) */
11422 if (mch_getenv(p + 1) != NULL)
11423 n = TRUE;
11424 else
11425 {
11426 /* try expanding things like $VIM and ${HOME} */
11427 p = expand_env_save(p);
11428 if (p != NULL && *p != '$')
11429 n = TRUE;
11430 vim_free(p);
11431 }
11432 }
11433 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011436 if (*skipwhite(p) != NUL)
11437 n = FALSE; /* trailing garbage */
11438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 else if (*p == '*') /* internal or user defined function */
11440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 }
11443 else if (*p == ':')
11444 {
11445 n = cmd_exists(p + 1);
11446 }
11447 else if (*p == '#')
11448 {
11449#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011450 if (p[1] == '#')
11451 n = autocmd_supported(p + 2);
11452 else
11453 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#endif
11455 }
11456 else /* internal variable */
11457 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011458 char_u *tofree;
11459 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011461 /* get_name_len() takes care of expanding curly braces */
11462 name = p;
11463 len = get_name_len(&p, &tofree, TRUE, FALSE);
11464 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011466 if (tofree != NULL)
11467 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011468 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011469 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011471 /* handle d.key, l[idx], f(expr) */
11472 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11473 if (n)
11474 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 }
11476 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011477 if (*p != NUL)
11478 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011480 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 }
11482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484}
11485
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011486#ifdef FEAT_FLOAT
11487/*
11488 * "exp()" function
11489 */
11490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011491f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011492{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011493 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011494
11495 rettv->v_type = VAR_FLOAT;
11496 if (get_float_arg(argvars, &f) == OK)
11497 rettv->vval.v_float = exp(f);
11498 else
11499 rettv->vval.v_float = 0.0;
11500}
11501#endif
11502
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503/*
11504 * "expand()" function
11505 */
11506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011507f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508{
11509 char_u *s;
11510 int len;
11511 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011512 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011515 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011518 if (argvars[1].v_type != VAR_UNKNOWN
11519 && argvars[2].v_type != VAR_UNKNOWN
11520 && get_tv_number_chk(&argvars[2], &error)
11521 && !error)
11522 {
11523 rettv->v_type = VAR_LIST;
11524 rettv->vval.v_list = NULL;
11525 }
11526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 if (*s == '%' || *s == '#' || *s == '<')
11529 {
11530 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011531 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011533 if (rettv->v_type == VAR_LIST)
11534 {
11535 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11536 list_append_string(rettv->vval.v_list, result, -1);
11537 else
11538 vim_free(result);
11539 }
11540 else
11541 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543 else
11544 {
11545 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011546 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011547 if (argvars[1].v_type != VAR_UNKNOWN
11548 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011549 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 if (!error)
11551 {
11552 ExpandInit(&xpc);
11553 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011554 if (p_wic)
11555 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011556 if (rettv->v_type == VAR_STRING)
11557 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11558 options, WILD_ALL);
11559 else if (rettv_list_alloc(rettv) != FAIL)
11560 {
11561 int i;
11562
11563 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11564 for (i = 0; i < xpc.xp_numfiles; i++)
11565 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11566 ExpandCleanup(&xpc);
11567 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 }
11569 else
11570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 }
11572}
11573
11574/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011575 * Go over all entries in "d2" and add them to "d1".
11576 * When "action" is "error" then a duplicate key is an error.
11577 * When "action" is "force" then a duplicate key is overwritten.
11578 * Otherwise duplicate keys are ignored ("action" is "keep").
11579 */
11580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011581dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011582{
11583 dictitem_T *di1;
11584 hashitem_T *hi2;
11585 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011586 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011587
11588 todo = (int)d2->dv_hashtab.ht_used;
11589 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11590 {
11591 if (!HASHITEM_EMPTY(hi2))
11592 {
11593 --todo;
11594 di1 = dict_find(d1, hi2->hi_key, -1);
11595 if (d1->dv_scope != 0)
11596 {
11597 /* Disallow replacing a builtin function in l: and g:.
11598 * Check the key to be valid when adding to any
11599 * scope. */
11600 if (d1->dv_scope == VAR_DEF_SCOPE
11601 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11602 && var_check_func_name(hi2->hi_key,
11603 di1 == NULL))
11604 break;
11605 if (!valid_varname(hi2->hi_key))
11606 break;
11607 }
11608 if (di1 == NULL)
11609 {
11610 di1 = dictitem_copy(HI2DI(hi2));
11611 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11612 dictitem_free(di1);
11613 }
11614 else if (*action == 'e')
11615 {
11616 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11617 break;
11618 }
11619 else if (*action == 'f' && HI2DI(hi2) != di1)
11620 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011621 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11622 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011623 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011624 clear_tv(&di1->di_tv);
11625 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11626 }
11627 }
11628 }
11629}
11630
11631/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011632 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011633 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011637{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011638 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011639
Bram Moolenaare9a41262005-01-15 22:18:47 +000011640 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011641 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 list_T *l1, *l2;
11643 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011644 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011645 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011646
Bram Moolenaare9a41262005-01-15 22:18:47 +000011647 l1 = argvars[0].vval.v_list;
11648 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011649 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011650 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011651 {
11652 if (argvars[2].v_type != VAR_UNKNOWN)
11653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 before = get_tv_number_chk(&argvars[2], &error);
11655 if (error)
11656 return; /* type error; errmsg already given */
11657
Bram Moolenaar758711c2005-02-02 23:11:38 +000011658 if (before == l1->lv_len)
11659 item = NULL;
11660 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011662 item = list_find(l1, before);
11663 if (item == NULL)
11664 {
11665 EMSGN(_(e_listidx), before);
11666 return;
11667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011668 }
11669 }
11670 else
11671 item = NULL;
11672 list_extend(l1, l2, item);
11673
Bram Moolenaare9a41262005-01-15 22:18:47 +000011674 copy_tv(&argvars[0], rettv);
11675 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11678 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011679 dict_T *d1, *d2;
11680 char_u *action;
11681 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011682
11683 d1 = argvars[0].vval.v_dict;
11684 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011685 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011686 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 {
11688 /* Check the third argument. */
11689 if (argvars[2].v_type != VAR_UNKNOWN)
11690 {
11691 static char *(av[]) = {"keep", "force", "error"};
11692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 action = get_tv_string_chk(&argvars[2]);
11694 if (action == NULL)
11695 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011696 for (i = 0; i < 3; ++i)
11697 if (STRCMP(action, av[i]) == 0)
11698 break;
11699 if (i == 3)
11700 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011701 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 return;
11703 }
11704 }
11705 else
11706 action = (char_u *)"force";
11707
Bram Moolenaara9922d62013-05-30 13:01:18 +020011708 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 copy_tv(&argvars[0], rettv);
11711 }
11712 }
11713 else
11714 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011715}
11716
11717/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011718 * "feedkeys()" function
11719 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011721f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011722{
11723 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011724 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011725 char_u *keys, *flags;
11726 char_u nbuf[NUMBUFLEN];
11727 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011728 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011729 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011730
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011731 /* This is not allowed in the sandbox. If the commands would still be
11732 * executed in the sandbox it would be OK, but it probably happens later,
11733 * when "sandbox" is no longer set. */
11734 if (check_secure())
11735 return;
11736
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011737 keys = get_tv_string(&argvars[0]);
11738 if (*keys != NUL)
11739 {
11740 if (argvars[1].v_type != VAR_UNKNOWN)
11741 {
11742 flags = get_tv_string_buf(&argvars[1], nbuf);
11743 for ( ; *flags != NUL; ++flags)
11744 {
11745 switch (*flags)
11746 {
11747 case 'n': remap = FALSE; break;
11748 case 'm': remap = TRUE; break;
11749 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011750 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011751 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011752 }
11753 }
11754 }
11755
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011756 /* Need to escape K_SPECIAL and CSI before putting the string in the
11757 * typeahead buffer. */
11758 keys_esc = vim_strsave_escape_csi(keys);
11759 if (keys_esc != NULL)
11760 {
11761 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011762 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011763 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011764 if (vgetc_busy)
11765 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011766 if (execute)
11767 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011768 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011769 }
11770}
11771
11772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 * "filereadable()" function
11774 */
11775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011776f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011778 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 char_u *p;
11780 int n;
11781
Bram Moolenaarc236c162008-07-13 17:41:49 +000011782#ifndef O_NONBLOCK
11783# define O_NONBLOCK 0
11784#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011786 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11787 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 {
11789 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011790 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 }
11792 else
11793 n = FALSE;
11794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796}
11797
11798/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011799 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 * rights to write into.
11801 */
11802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011803f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011805 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806}
11807
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011808 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011809findfilendir(
11810 typval_T *argvars UNUSED,
11811 typval_T *rettv,
11812 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011813{
11814#ifdef FEAT_SEARCHPATH
11815 char_u *fname;
11816 char_u *fresult = NULL;
11817 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11818 char_u *p;
11819 char_u pathbuf[NUMBUFLEN];
11820 int count = 1;
11821 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011822 int error = FALSE;
11823#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011824
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011825 rettv->vval.v_string = NULL;
11826 rettv->v_type = VAR_STRING;
11827
11828#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011830
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011833 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11834 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011835 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 else
11837 {
11838 if (*p != NUL)
11839 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011842 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011844 }
11845
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011846 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11847 error = TRUE;
11848
11849 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 do
11852 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011853 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011854 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 fresult = find_file_in_path_option(first ? fname : NULL,
11856 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011857 0, first, path,
11858 find_what,
11859 curbuf->b_ffname,
11860 find_what == FINDFILE_DIR
11861 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011862 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011863
11864 if (fresult != NULL && rettv->v_type == VAR_LIST)
11865 list_append_string(rettv->vval.v_list, fresult, -1);
11866
11867 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011869
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011870 if (rettv->v_type == VAR_STRING)
11871 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011872#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011873}
11874
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011875static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11876static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011877
11878/*
11879 * Implementation of map() and filter().
11880 */
11881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011882filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011883{
11884 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011885 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 listitem_T *li, *nli;
11887 list_T *l = NULL;
11888 dictitem_T *di;
11889 hashtab_T *ht;
11890 hashitem_T *hi;
11891 dict_T *d = NULL;
11892 typval_T save_val;
11893 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011894 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011895 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011896 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011897 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011898 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011899 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011900 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011901
Bram Moolenaare9a41262005-01-15 22:18:47 +000011902 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011903 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011904 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011905 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011906 return;
11907 }
11908 else if (argvars[0].v_type == VAR_DICT)
11909 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011910 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011911 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011912 return;
11913 }
11914 else
11915 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011916 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011917 return;
11918 }
11919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 expr = get_tv_string_buf_chk(&argvars[1], buf);
11921 /* On type errors, the preceding call has already displayed an error
11922 * message. Avoid a misleading error message for an empty string that
11923 * was not passed as argument. */
11924 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011925 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 prepare_vimvar(VV_VAL, &save_val);
11927 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011928
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011929 /* We reset "did_emsg" to be able to detect whether an error
11930 * occurred during evaluation of the expression. */
11931 save_did_emsg = did_emsg;
11932 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011933
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011934 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 vimvars[VV_KEY].vv_type = VAR_STRING;
11938
11939 ht = &d->dv_hashtab;
11940 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011941 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011942 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 if (!HASHITEM_EMPTY(hi))
11945 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011946 int r;
11947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 --todo;
11949 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011950 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011951 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11952 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011953 break;
11954 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011955 r = filter_map_one(&di->di_tv, expr, map, &rem);
11956 clear_tv(&vimvars[VV_KEY].vv_tv);
11957 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 break;
11959 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011960 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011961 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11962 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011963 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 }
11967 }
11968 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011969 }
11970 else
11971 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011972 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 for (li = l->lv_first; li != NULL; li = nli)
11975 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011976 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011977 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011978 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011979 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011980 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011981 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011982 break;
11983 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011985 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011986 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011988
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011989 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011991
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011992 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011993 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994
11995 copy_tv(&argvars[0], rettv);
11996}
11997
11998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012000{
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012003 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012004
Bram Moolenaar33570922005-01-25 22:26:29 +000012005 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012006 s = expr;
12007 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012008 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012009 if (*s != NUL) /* check for trailing chars after expr */
12010 {
12011 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012012 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012013 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012014 }
12015 if (map)
12016 {
12017 /* map(): replace the list item value */
12018 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012019 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012020 *tv = rettv;
12021 }
12022 else
12023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 int error = FALSE;
12025
Bram Moolenaare9a41262005-01-15 22:18:47 +000012026 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012028 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029 /* On type error, nothing has been removed; return FAIL to stop the
12030 * loop. The error message was given by get_tv_number_chk(). */
12031 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012032 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012033 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012034 retval = OK;
12035theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012036 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012037 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012038}
12039
12040/*
12041 * "filter()" function
12042 */
12043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012044f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012045{
12046 filter_map(argvars, rettv, FALSE);
12047}
12048
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012049/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012050 * "finddir({fname}[, {path}[, {count}]])" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012055 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012056}
12057
12058/*
12059 * "findfile({fname}[, {path}[, {count}]])" function
12060 */
12061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012064 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065}
12066
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012067#ifdef FEAT_FLOAT
12068/*
12069 * "float2nr({float})" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012074 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012075
12076 if (get_float_arg(argvars, &f) == OK)
12077 {
12078 if (f < -0x7fffffff)
12079 rettv->vval.v_number = -0x7fffffff;
12080 else if (f > 0x7fffffff)
12081 rettv->vval.v_number = 0x7fffffff;
12082 else
12083 rettv->vval.v_number = (varnumber_T)f;
12084 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012085}
12086
12087/*
12088 * "floor({float})" function
12089 */
12090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012092{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012093 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012094
12095 rettv->v_type = VAR_FLOAT;
12096 if (get_float_arg(argvars, &f) == OK)
12097 rettv->vval.v_float = floor(f);
12098 else
12099 rettv->vval.v_float = 0.0;
12100}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012101
12102/*
12103 * "fmod()" function
12104 */
12105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012106f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012107{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012108 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012109
12110 rettv->v_type = VAR_FLOAT;
12111 if (get_float_arg(argvars, &fx) == OK
12112 && get_float_arg(&argvars[1], &fy) == OK)
12113 rettv->vval.v_float = fmod(fx, fy);
12114 else
12115 rettv->vval.v_float = 0.0;
12116}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012117#endif
12118
Bram Moolenaar0d660222005-01-07 21:51:51 +000012119/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012120 * "fnameescape({string})" function
12121 */
12122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012123f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012124{
12125 rettv->vval.v_string = vim_strsave_fnameescape(
12126 get_tv_string(&argvars[0]), FALSE);
12127 rettv->v_type = VAR_STRING;
12128}
12129
12130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 * "fnamemodify({fname}, {mods})" function
12132 */
12133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012134f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135{
12136 char_u *fname;
12137 char_u *mods;
12138 int usedlen = 0;
12139 int len;
12140 char_u *fbuf = NULL;
12141 char_u buf[NUMBUFLEN];
12142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012143 fname = get_tv_string_chk(&argvars[0]);
12144 mods = get_tv_string_buf_chk(&argvars[1], buf);
12145 if (fname == NULL || mods == NULL)
12146 fname = NULL;
12147 else
12148 {
12149 len = (int)STRLEN(fname);
12150 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 vim_free(fbuf);
12159}
12160
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012161static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
12163/*
12164 * "foldclosed()" function
12165 */
12166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012167foldclosed_both(
12168 typval_T *argvars UNUSED,
12169 typval_T *rettv,
12170 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
12172#ifdef FEAT_FOLDING
12173 linenr_T lnum;
12174 linenr_T first, last;
12175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12178 {
12179 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12180 {
12181 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012182 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 return;
12186 }
12187 }
12188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190}
12191
12192/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 * "foldclosed()" function
12194 */
12195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012196f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197{
12198 foldclosed_both(argvars, rettv, FALSE);
12199}
12200
12201/*
12202 * "foldclosedend()" function
12203 */
12204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012205f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206{
12207 foldclosed_both(argvars, rettv, TRUE);
12208}
12209
12210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 * "foldlevel()" function
12212 */
12213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215{
12216#ifdef FEAT_FOLDING
12217 linenr_T lnum;
12218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012219 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012221 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
12225/*
12226 * "foldtext()" function
12227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012229f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230{
12231#ifdef FEAT_FOLDING
12232 linenr_T lnum;
12233 char_u *s;
12234 char_u *r;
12235 int len;
12236 char *txt;
12237#endif
12238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 rettv->v_type = VAR_STRING;
12240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012242 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12243 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12244 <= curbuf->b_ml.ml_line_count
12245 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 {
12247 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012248 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12249 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 {
12251 if (!linewhite(lnum))
12252 break;
12253 ++lnum;
12254 }
12255
12256 /* Find interesting text in this line. */
12257 s = skipwhite(ml_get(lnum));
12258 /* skip C comment-start */
12259 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012262 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012263 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012264 {
12265 s = skipwhite(ml_get(lnum + 1));
12266 if (*s == '*')
12267 s = skipwhite(s + 1);
12268 }
12269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 txt = _("+-%s%3ld lines: ");
12271 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012272 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 + 20 /* for %3ld */
12274 + STRLEN(s))); /* concatenated */
12275 if (r != NULL)
12276 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012277 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12278 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12279 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 len = (int)STRLEN(r);
12281 STRCAT(r, s);
12282 /* remove 'foldmarker' and 'commentstring' */
12283 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 }
12286 }
12287#endif
12288}
12289
12290/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012291 * "foldtextresult(lnum)" function
12292 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012295{
12296#ifdef FEAT_FOLDING
12297 linenr_T lnum;
12298 char_u *text;
12299 char_u buf[51];
12300 foldinfo_T foldinfo;
12301 int fold_count;
12302#endif
12303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->v_type = VAR_STRING;
12305 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012306#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012308 /* treat illegal types and illegal string values for {lnum} the same */
12309 if (lnum < 0)
12310 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012311 fold_count = foldedCount(curwin, lnum, &foldinfo);
12312 if (fold_count > 0)
12313 {
12314 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12315 &foldinfo, buf);
12316 if (text == buf)
12317 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012319 }
12320#endif
12321}
12322
12323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 * "foreground()" function
12325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012327f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329#ifdef FEAT_GUI
12330 if (gui.in_use)
12331 gui_mch_set_foreground();
12332#else
12333# ifdef WIN32
12334 win32_set_foreground();
12335# endif
12336#endif
12337}
12338
12339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012340 * "function()" function
12341 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012343f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012344{
12345 char_u *s;
12346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012348 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012349 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012350 /* Don't check an autoload name for existence here. */
12351 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012352 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012353 else
12354 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012355 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012356 {
12357 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012358 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012359
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012360 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12361 * also be called from another script. Using trans_function_name()
12362 * would also work, but some plugins depend on the name being
12363 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012364 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012365 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012366 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012367 if (rettv->vval.v_string != NULL)
12368 {
12369 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012370 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012371 }
12372 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012373 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012374 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012376 }
12377}
12378
12379/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012380 * "garbagecollect()" function
12381 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012383f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012384{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012385 /* This is postponed until we are back at the toplevel, because we may be
12386 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12387 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012388
12389 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12390 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012391}
12392
12393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394 * "get()" function
12395 */
12396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012397f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398{
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 listitem_T *li;
12400 list_T *l;
12401 dictitem_T *di;
12402 dict_T *d;
12403 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012404
Bram Moolenaare9a41262005-01-15 22:18:47 +000012405 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012406 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012407 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409 int error = FALSE;
12410
12411 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12412 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012413 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012414 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 else if (argvars[0].v_type == VAR_DICT)
12417 {
12418 if ((d = argvars[0].vval.v_dict) != NULL)
12419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012420 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012421 if (di != NULL)
12422 tv = &di->di_tv;
12423 }
12424 }
12425 else
12426 EMSG2(_(e_listdictarg), "get()");
12427
12428 if (tv == NULL)
12429 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 copy_tv(&argvars[2], rettv);
12432 }
12433 else
12434 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012435}
12436
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012437static 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 +000012438
12439/*
12440 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012441 * Return a range (from start to end) of lines in rettv from the specified
12442 * buffer.
12443 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012444 */
12445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012446get_buffer_lines(
12447 buf_T *buf,
12448 linenr_T start,
12449 linenr_T end,
12450 int retlist,
12451 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012452{
12453 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012454
Bram Moolenaar959a1432013-12-14 12:17:38 +010012455 rettv->v_type = VAR_STRING;
12456 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012457 if (retlist && rettv_list_alloc(rettv) == FAIL)
12458 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012459
12460 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12461 return;
12462
12463 if (!retlist)
12464 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012465 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12466 p = ml_get_buf(buf, start, FALSE);
12467 else
12468 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012469 rettv->vval.v_string = vim_strsave(p);
12470 }
12471 else
12472 {
12473 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012474 return;
12475
12476 if (start < 1)
12477 start = 1;
12478 if (end > buf->b_ml.ml_line_count)
12479 end = buf->b_ml.ml_line_count;
12480 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012481 if (list_append_string(rettv->vval.v_list,
12482 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012483 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012484 }
12485}
12486
12487/*
12488 * "getbufline()" function
12489 */
12490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012491f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492{
12493 linenr_T lnum;
12494 linenr_T end;
12495 buf_T *buf;
12496
12497 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12498 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012499 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012500 --emsg_off;
12501
Bram Moolenaar661b1822005-07-28 22:36:45 +000012502 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012503 if (argvars[2].v_type == VAR_UNKNOWN)
12504 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012505 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012506 end = get_tv_lnum_buf(&argvars[2], buf);
12507
Bram Moolenaar342337a2005-07-21 21:11:17 +000012508 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012509}
12510
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511/*
12512 * "getbufvar()" function
12513 */
12514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012515f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516{
12517 buf_T *buf;
12518 buf_T *save_curbuf;
12519 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012520 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012521 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12524 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012525 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012526 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012528 rettv->v_type = VAR_STRING;
12529 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012530
12531 if (buf != NULL && varname != NULL)
12532 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012533 /* set curbuf to be our buf, temporarily */
12534 save_curbuf = curbuf;
12535 curbuf = buf;
12536
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012538 {
12539 if (get_option_tv(&varname, rettv, TRUE) == OK)
12540 done = TRUE;
12541 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012542 else if (STRCMP(varname, "changedtick") == 0)
12543 {
12544 rettv->v_type = VAR_NUMBER;
12545 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012546 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012547 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548 else
12549 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012550 /* Look up the variable. */
12551 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12552 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12553 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012554 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012555 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 done = TRUE;
12558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012560
12561 /* restore previous notion of curbuf */
12562 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563 }
12564
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012565 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12566 /* use the default value */
12567 copy_tv(&argvars[2], rettv);
12568
Bram Moolenaar0d660222005-01-07 21:51:51 +000012569 --emsg_off;
12570}
12571
12572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 * "getchar()" function
12574 */
12575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012576f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577{
12578 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012581 /* Position the cursor. Needed after a message that ends in a space. */
12582 windgoto(msg_row, msg_col);
12583
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 ++no_mapping;
12585 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012586 for (;;)
12587 {
12588 if (argvars[0].v_type == VAR_UNKNOWN)
12589 /* getchar(): blocking wait. */
12590 n = safe_vgetc();
12591 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12592 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012593 n = vpeekc_any();
12594 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012595 /* illegal argument or getchar(0) and no char avail: return zero */
12596 n = 0;
12597 else
12598 /* getchar(0) and char avail: return char */
12599 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012600
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012601 if (n == K_IGNORE)
12602 continue;
12603 break;
12604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 --no_mapping;
12606 --allow_keys;
12607
Bram Moolenaar219b8702006-11-01 14:32:36 +000012608 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12609 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12610 vimvars[VV_MOUSE_COL].vv_nr = 0;
12611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 if (IS_SPECIAL(n) || mod_mask != 0)
12614 {
12615 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12616 int i = 0;
12617
12618 /* Turn a special key into three bytes, plus modifier. */
12619 if (mod_mask != 0)
12620 {
12621 temp[i++] = K_SPECIAL;
12622 temp[i++] = KS_MODIFIER;
12623 temp[i++] = mod_mask;
12624 }
12625 if (IS_SPECIAL(n))
12626 {
12627 temp[i++] = K_SPECIAL;
12628 temp[i++] = K_SECOND(n);
12629 temp[i++] = K_THIRD(n);
12630 }
12631#ifdef FEAT_MBYTE
12632 else if (has_mbyte)
12633 i += (*mb_char2bytes)(n, temp + i);
12634#endif
12635 else
12636 temp[i++] = n;
12637 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->v_type = VAR_STRING;
12639 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012640
12641#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012642 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012643 {
12644 int row = mouse_row;
12645 int col = mouse_col;
12646 win_T *win;
12647 linenr_T lnum;
12648# ifdef FEAT_WINDOWS
12649 win_T *wp;
12650# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012651 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012652
12653 if (row >= 0 && col >= 0)
12654 {
12655 /* Find the window at the mouse coordinates and compute the
12656 * text position. */
12657 win = mouse_find_win(&row, &col);
12658 (void)mouse_comp_pos(win, &row, &col, &lnum);
12659# ifdef FEAT_WINDOWS
12660 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012661 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012662# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012663 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012664 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12665 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12666 }
12667 }
12668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 }
12670}
12671
12672/*
12673 * "getcharmod()" function
12674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012676f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679}
12680
12681/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012682 * "getcharsearch()" function
12683 */
12684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012686{
12687 if (rettv_dict_alloc(rettv) != FAIL)
12688 {
12689 dict_T *dict = rettv->vval.v_dict;
12690
12691 dict_add_nr_str(dict, "char", 0L, last_csearch());
12692 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12693 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12694 }
12695}
12696
12697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 * "getcmdline()" function
12699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012701f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->v_type = VAR_STRING;
12704 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705}
12706
12707/*
12708 * "getcmdpos()" function
12709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012711f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714}
12715
12716/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012717 * "getcmdtype()" function
12718 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012720f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012721{
12722 rettv->v_type = VAR_STRING;
12723 rettv->vval.v_string = alloc(2);
12724 if (rettv->vval.v_string != NULL)
12725 {
12726 rettv->vval.v_string[0] = get_cmdline_type();
12727 rettv->vval.v_string[1] = NUL;
12728 }
12729}
12730
12731/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012732 * "getcmdwintype()" function
12733 */
12734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012735f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012736{
12737 rettv->v_type = VAR_STRING;
12738 rettv->vval.v_string = NULL;
12739#ifdef FEAT_CMDWIN
12740 rettv->vval.v_string = alloc(2);
12741 if (rettv->vval.v_string != NULL)
12742 {
12743 rettv->vval.v_string[0] = cmdwin_type;
12744 rettv->vval.v_string[1] = NUL;
12745 }
12746#endif
12747}
12748
12749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 * "getcwd()" function
12751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012753f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012755 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012756 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012759 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012760
12761 wp = find_tabwin(&argvars[0], &argvars[1]);
12762 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012764 if (wp->w_localdir != NULL)
12765 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12766 else if(globaldir != NULL)
12767 rettv->vval.v_string = vim_strsave(globaldir);
12768 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012769 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012770 cwd = alloc(MAXPATHL);
12771 if (cwd != NULL)
12772 {
12773 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12774 rettv->vval.v_string = vim_strsave(cwd);
12775 vim_free(cwd);
12776 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012777 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012778#ifdef BACKSLASH_IN_FILENAME
12779 if (rettv->vval.v_string != NULL)
12780 slash_adjust(rettv->vval.v_string);
12781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 }
12783}
12784
12785/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012786 * "getfontname()" function
12787 */
12788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->v_type = VAR_STRING;
12792 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012793#ifdef FEAT_GUI
12794 if (gui.in_use)
12795 {
12796 GuiFont font;
12797 char_u *name = NULL;
12798
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012799 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012800 {
12801 /* Get the "Normal" font. Either the name saved by
12802 * hl_set_font_name() or from the font ID. */
12803 font = gui.norm_font;
12804 name = hl_get_font_name();
12805 }
12806 else
12807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012809 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12810 return;
12811 font = gui_mch_get_font(name, FALSE);
12812 if (font == NOFONT)
12813 return; /* Invalid font name, return empty string. */
12814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012816 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012817 gui_mch_free_font(font);
12818 }
12819#endif
12820}
12821
12822/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012823 * "getfperm({fname})" function
12824 */
12825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012826f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012827{
12828 char_u *fname;
12829 struct stat st;
12830 char_u *perm = NULL;
12831 char_u flags[] = "rwx";
12832 int i;
12833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012837 if (mch_stat((char *)fname, &st) >= 0)
12838 {
12839 perm = vim_strsave((char_u *)"---------");
12840 if (perm != NULL)
12841 {
12842 for (i = 0; i < 9; i++)
12843 {
12844 if (st.st_mode & (1 << (8 - i)))
12845 perm[i] = flags[i % 3];
12846 }
12847 }
12848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012850}
12851
12852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 * "getfsize({fname})" function
12854 */
12855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012856f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857{
12858 char_u *fname;
12859 struct stat st;
12860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864
12865 if (mch_stat((char *)fname, &st) >= 0)
12866 {
12867 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012872
12873 /* non-perfect check for overflow */
12874 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12875 rettv->vval.v_number = -2;
12876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 }
12878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880}
12881
12882/*
12883 * "getftime({fname})" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
12888 char_u *fname;
12889 struct stat st;
12890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892
12893 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897}
12898
12899/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012900 * "getftype({fname})" function
12901 */
12902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012903f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012904{
12905 char_u *fname;
12906 struct stat st;
12907 char_u *type = NULL;
12908 char *t;
12909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012913 if (mch_lstat((char *)fname, &st) >= 0)
12914 {
12915#ifdef S_ISREG
12916 if (S_ISREG(st.st_mode))
12917 t = "file";
12918 else if (S_ISDIR(st.st_mode))
12919 t = "dir";
12920# ifdef S_ISLNK
12921 else if (S_ISLNK(st.st_mode))
12922 t = "link";
12923# endif
12924# ifdef S_ISBLK
12925 else if (S_ISBLK(st.st_mode))
12926 t = "bdev";
12927# endif
12928# ifdef S_ISCHR
12929 else if (S_ISCHR(st.st_mode))
12930 t = "cdev";
12931# endif
12932# ifdef S_ISFIFO
12933 else if (S_ISFIFO(st.st_mode))
12934 t = "fifo";
12935# endif
12936# ifdef S_ISSOCK
12937 else if (S_ISSOCK(st.st_mode))
12938 t = "fifo";
12939# endif
12940 else
12941 t = "other";
12942#else
12943# ifdef S_IFMT
12944 switch (st.st_mode & S_IFMT)
12945 {
12946 case S_IFREG: t = "file"; break;
12947 case S_IFDIR: t = "dir"; break;
12948# ifdef S_IFLNK
12949 case S_IFLNK: t = "link"; break;
12950# endif
12951# ifdef S_IFBLK
12952 case S_IFBLK: t = "bdev"; break;
12953# endif
12954# ifdef S_IFCHR
12955 case S_IFCHR: t = "cdev"; break;
12956# endif
12957# ifdef S_IFIFO
12958 case S_IFIFO: t = "fifo"; break;
12959# endif
12960# ifdef S_IFSOCK
12961 case S_IFSOCK: t = "socket"; break;
12962# endif
12963 default: t = "other";
12964 }
12965# else
12966 if (mch_isdir(fname))
12967 t = "dir";
12968 else
12969 t = "file";
12970# endif
12971#endif
12972 type = vim_strsave((char_u *)t);
12973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012975}
12976
12977/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012978 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979 */
12980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012981f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012982{
12983 linenr_T lnum;
12984 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012985 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012986
12987 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012988 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012989 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012990 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012991 retlist = FALSE;
12992 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012993 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012994 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012995 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012996 retlist = TRUE;
12997 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012998
Bram Moolenaar342337a2005-07-21 21:11:17 +000012999 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013000}
13001
13002/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013003 * "getmatches()" function
13004 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013006f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013007{
13008#ifdef FEAT_SEARCH_EXTRA
13009 dict_T *dict;
13010 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013011 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013012
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013013 if (rettv_list_alloc(rettv) == OK)
13014 {
13015 while (cur != NULL)
13016 {
13017 dict = dict_alloc();
13018 if (dict == NULL)
13019 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013020 if (cur->match.regprog == NULL)
13021 {
13022 /* match added with matchaddpos() */
13023 for (i = 0; i < MAXPOSMATCH; ++i)
13024 {
13025 llpos_T *llpos;
13026 char buf[6];
13027 list_T *l;
13028
13029 llpos = &cur->pos.pos[i];
13030 if (llpos->lnum == 0)
13031 break;
13032 l = list_alloc();
13033 if (l == NULL)
13034 break;
13035 list_append_number(l, (varnumber_T)llpos->lnum);
13036 if (llpos->col > 0)
13037 {
13038 list_append_number(l, (varnumber_T)llpos->col);
13039 list_append_number(l, (varnumber_T)llpos->len);
13040 }
13041 sprintf(buf, "pos%d", i + 1);
13042 dict_add_list(dict, buf, l);
13043 }
13044 }
13045 else
13046 {
13047 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13048 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013049 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013050 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13051 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013052# ifdef FEAT_CONCEAL
13053 if (cur->conceal_char)
13054 {
13055 char_u buf[MB_MAXBYTES + 1];
13056
13057 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13058 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13059 }
13060# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013061 list_append_dict(rettv->vval.v_list, dict);
13062 cur = cur->next;
13063 }
13064 }
13065#endif
13066}
13067
13068/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013069 * "getpid()" function
13070 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013072f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013073{
13074 rettv->vval.v_number = mch_get_pid();
13075}
13076
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013077static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013078
13079/*
13080 * "getcurpos()" function
13081 */
13082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013083f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013084{
13085 getpos_both(argvars, rettv, TRUE);
13086}
13087
Bram Moolenaar18081e32008-02-20 19:11:07 +000013088/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013089 * "getpos(string)" function
13090 */
13091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013092f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013093{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013094 getpos_both(argvars, rettv, FALSE);
13095}
13096
13097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013098getpos_both(
13099 typval_T *argvars,
13100 typval_T *rettv,
13101 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013102{
Bram Moolenaara5525202006-03-02 22:52:09 +000013103 pos_T *fp;
13104 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013105 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013106
13107 if (rettv_list_alloc(rettv) == OK)
13108 {
13109 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013110 if (getcurpos)
13111 fp = &curwin->w_cursor;
13112 else
13113 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013114 if (fnum != -1)
13115 list_append_number(l, (varnumber_T)fnum);
13116 else
13117 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013118 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13119 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013120 list_append_number(l, (fp != NULL)
13121 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013122 : (varnumber_T)0);
13123 list_append_number(l,
13124#ifdef FEAT_VIRTUALEDIT
13125 (fp != NULL) ? (varnumber_T)fp->coladd :
13126#endif
13127 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013128 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013129 {
13130 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013131 list_append_number(l, curwin->w_curswant == MAXCOL ?
13132 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013133 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013134 }
13135 else
13136 rettv->vval.v_number = FALSE;
13137}
13138
13139/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013140 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013141 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013143f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013144{
13145#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013146 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013147#endif
13148
Bram Moolenaar2641f772005-03-25 21:58:17 +000013149#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013150 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013151 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013152 wp = NULL;
13153 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13154 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013155 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013156 if (wp == NULL)
13157 return;
13158 }
13159
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013160 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013161 }
13162#endif
13163}
13164
13165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 * "getreg()" function
13167 */
13168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013169f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 char_u *strregname;
13172 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013173 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013174 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 strregname = get_tv_string_chk(&argvars[0]);
13180 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013181 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013184 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13185 return_list = get_tv_number_chk(&argvars[2], &error);
13186 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013189 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013190
13191 if (error)
13192 return;
13193
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 regname = (strregname == NULL ? '"' : *strregname);
13195 if (regname == 0)
13196 regname = '"';
13197
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013198 if (return_list)
13199 {
13200 rettv->v_type = VAR_LIST;
13201 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13202 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013203 if (rettv->vval.v_list != NULL)
13204 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013205 }
13206 else
13207 {
13208 rettv->v_type = VAR_STRING;
13209 rettv->vval.v_string = get_reg_contents(regname,
13210 arg2 ? GREG_EXPR_SRC : 0);
13211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212}
13213
13214/*
13215 * "getregtype()" function
13216 */
13217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219{
13220 char_u *strregname;
13221 int regname;
13222 char_u buf[NUMBUFLEN + 2];
13223 long reglen = 0;
13224
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013225 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 {
13227 strregname = get_tv_string_chk(&argvars[0]);
13228 if (strregname == NULL) /* type error; errmsg already given */
13229 {
13230 rettv->v_type = VAR_STRING;
13231 rettv->vval.v_string = NULL;
13232 return;
13233 }
13234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 else
13236 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013237 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238
13239 regname = (strregname == NULL ? '"' : *strregname);
13240 if (regname == 0)
13241 regname = '"';
13242
13243 buf[0] = NUL;
13244 buf[1] = NUL;
13245 switch (get_reg_type(regname, &reglen))
13246 {
13247 case MLINE: buf[0] = 'V'; break;
13248 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 case MBLOCK:
13250 buf[0] = Ctrl_V;
13251 sprintf((char *)buf + 1, "%ld", reglen + 1);
13252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->v_type = VAR_STRING;
13255 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013259 * "gettabvar()" function
13260 */
13261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013262f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013263{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013264 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013265 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013266 dictitem_T *v;
13267 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013268 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013269
13270 rettv->v_type = VAR_STRING;
13271 rettv->vval.v_string = NULL;
13272
13273 varname = get_tv_string_chk(&argvars[1]);
13274 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13275 if (tp != NULL && varname != NULL)
13276 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013277 /* Set tp to be our tabpage, temporarily. Also set the window to the
13278 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013279 if (switch_win(&oldcurwin, &oldtabpage,
13280 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013281 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013282 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013283 /* look up the variable */
13284 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13285 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13286 if (v != NULL)
13287 {
13288 copy_tv(&v->di_tv, rettv);
13289 done = TRUE;
13290 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013291 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013292
13293 /* restore previous notion of curwin */
13294 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013295 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013296
13297 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013298 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013299}
13300
13301/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013302 * "gettabwinvar()" function
13303 */
13304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013305f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013306{
13307 getwinvar(argvars, rettv, 1);
13308}
13309
13310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 * "getwinposx()" function
13312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013314f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317#ifdef FEAT_GUI
13318 if (gui.in_use)
13319 {
13320 int x, y;
13321
13322 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013323 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 }
13325#endif
13326}
13327
13328/*
13329 * "getwinposy()" function
13330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013332f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335#ifdef FEAT_GUI
13336 if (gui.in_use)
13337 {
13338 int x, y;
13339
13340 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 }
13343#endif
13344}
13345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013346/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013347 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013348 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013349 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013350find_win_by_nr(
13351 typval_T *vp,
13352 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013353{
13354#ifdef FEAT_WINDOWS
13355 win_T *wp;
13356#endif
13357 int nr;
13358
13359 nr = get_tv_number_chk(vp, NULL);
13360
13361#ifdef FEAT_WINDOWS
13362 if (nr < 0)
13363 return NULL;
13364 if (nr == 0)
13365 return curwin;
13366
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013367 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13368 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013369 if (--nr <= 0)
13370 break;
13371 return wp;
13372#else
13373 if (nr == 0 || nr == 1)
13374 return curwin;
13375 return NULL;
13376#endif
13377}
13378
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013380 * Find window specified by "wvp" in tabpage "tvp".
13381 */
13382 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013383find_tabwin(
13384 typval_T *wvp, /* VAR_UNKNOWN for current window */
13385 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013386{
13387 win_T *wp = NULL;
13388 tabpage_T *tp = NULL;
13389 long n;
13390
13391 if (wvp->v_type != VAR_UNKNOWN)
13392 {
13393 if (tvp->v_type != VAR_UNKNOWN)
13394 {
13395 n = get_tv_number(tvp);
13396 if (n >= 0)
13397 tp = find_tabpage(n);
13398 }
13399 else
13400 tp = curtab;
13401
13402 if (tp != NULL)
13403 wp = find_win_by_nr(wvp, tp);
13404 }
13405 else
13406 wp = curwin;
13407
13408 return wp;
13409}
13410
13411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 * "getwinvar()" function
13413 */
13414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013415f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013417 getwinvar(argvars, rettv, 0);
13418}
13419
13420/*
13421 * getwinvar() and gettabwinvar()
13422 */
13423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013424getwinvar(
13425 typval_T *argvars,
13426 typval_T *rettv,
13427 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013428{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013429 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013432 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013433 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013434#ifdef FEAT_WINDOWS
13435 win_T *oldcurwin;
13436 tabpage_T *oldtabpage;
13437 int need_switch_win;
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013440#ifdef FEAT_WINDOWS
13441 if (off == 1)
13442 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13443 else
13444 tp = curtab;
13445#endif
13446 win = find_win_by_nr(&argvars[off], tp);
13447 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013450 rettv->v_type = VAR_STRING;
13451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452
13453 if (win != NULL && varname != NULL)
13454 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013455#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013456 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013457 * otherwise the window is not valid. Only do this when needed,
13458 * autocommands get blocked. */
13459 need_switch_win = !(tp == curtab && win == curwin);
13460 if (!need_switch_win
13461 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13462#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013463 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013464 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013465 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013466 if (get_option_tv(&varname, rettv, 1) == OK)
13467 done = TRUE;
13468 }
13469 else
13470 {
13471 /* Look up the variable. */
13472 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13473 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13474 varname, FALSE);
13475 if (v != NULL)
13476 {
13477 copy_tv(&v->di_tv, rettv);
13478 done = TRUE;
13479 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013482
Bram Moolenaarba117c22015-09-29 16:53:22 +020013483#ifdef FEAT_WINDOWS
13484 if (need_switch_win)
13485 /* restore previous notion of curwin */
13486 restore_win(oldcurwin, oldtabpage, TRUE);
13487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 }
13489
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013490 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13491 /* use the default return value */
13492 copy_tv(&argvars[off + 2], rettv);
13493
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494 --emsg_off;
13495}
13496
13497/*
13498 * "glob()" function
13499 */
13500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013501f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013503 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013505 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013507 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013508 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013510 if (argvars[1].v_type != VAR_UNKNOWN)
13511 {
13512 if (get_tv_number_chk(&argvars[1], &error))
13513 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013515 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013516 if (get_tv_number_chk(&argvars[2], &error))
13517 {
13518 rettv->v_type = VAR_LIST;
13519 rettv->vval.v_list = NULL;
13520 }
13521 if (argvars[3].v_type != VAR_UNKNOWN
13522 && get_tv_number_chk(&argvars[3], &error))
13523 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013524 }
13525 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013526 if (!error)
13527 {
13528 ExpandInit(&xpc);
13529 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013530 if (p_wic)
13531 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013532 if (rettv->v_type == VAR_STRING)
13533 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013534 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013535 else if (rettv_list_alloc(rettv) != FAIL)
13536 {
13537 int i;
13538
13539 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13540 NULL, options, WILD_ALL_KEEP);
13541 for (i = 0; i < xpc.xp_numfiles; i++)
13542 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13543
13544 ExpandCleanup(&xpc);
13545 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013546 }
13547 else
13548 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549}
13550
13551/*
13552 * "globpath()" function
13553 */
13554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013555f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013557 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013559 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013560 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013561 garray_T ga;
13562 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013564 /* When the optional second argument is non-zero, don't remove matches
13565 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013567 if (argvars[2].v_type != VAR_UNKNOWN)
13568 {
13569 if (get_tv_number_chk(&argvars[2], &error))
13570 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013571 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013572 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013573 if (get_tv_number_chk(&argvars[3], &error))
13574 {
13575 rettv->v_type = VAR_LIST;
13576 rettv->vval.v_list = NULL;
13577 }
13578 if (argvars[4].v_type != VAR_UNKNOWN
13579 && get_tv_number_chk(&argvars[4], &error))
13580 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013581 }
13582 }
13583 if (file != NULL && !error)
13584 {
13585 ga_init2(&ga, (int)sizeof(char_u *), 10);
13586 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13587 if (rettv->v_type == VAR_STRING)
13588 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13589 else if (rettv_list_alloc(rettv) != FAIL)
13590 for (i = 0; i < ga.ga_len; ++i)
13591 list_append_string(rettv->vval.v_list,
13592 ((char_u **)(ga.ga_data))[i], -1);
13593 ga_clear_strings(&ga);
13594 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597}
13598
13599/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013600 * "glob2regpat()" function
13601 */
13602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013603f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013604{
13605 char_u *pat = get_tv_string_chk(&argvars[0]);
13606
13607 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013608 rettv->vval.v_string = (pat == NULL)
13609 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013610}
13611
13612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 * "has()" function
13614 */
13615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013616f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 int i;
13619 char_u *name;
13620 int n = FALSE;
13621 static char *(has_list[]) =
13622 {
13623#ifdef AMIGA
13624 "amiga",
13625# ifdef FEAT_ARP
13626 "arp",
13627# endif
13628#endif
13629#ifdef __BEOS__
13630 "beos",
13631#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013632#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 "mac",
13634#endif
13635#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013636 "macunix", /* built with 'darwin' enabled */
13637#endif
13638#if defined(__APPLE__) && __APPLE__ == 1
13639 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#ifdef __QNX__
13642 "qnx",
13643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#ifdef UNIX
13645 "unix",
13646#endif
13647#ifdef VMS
13648 "vms",
13649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#ifdef WIN32
13651 "win32",
13652#endif
13653#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13654 "win32unix",
13655#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013656#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 "win64",
13658#endif
13659#ifdef EBCDIC
13660 "ebcdic",
13661#endif
13662#ifndef CASE_INSENSITIVE_FILENAME
13663 "fname_case",
13664#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013665#ifdef HAVE_ACL
13666 "acl",
13667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef FEAT_ARABIC
13669 "arabic",
13670#endif
13671#ifdef FEAT_AUTOCMD
13672 "autocmd",
13673#endif
13674#ifdef FEAT_BEVAL
13675 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013676# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13677 "balloon_multiline",
13678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#endif
13680#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13681 "builtin_terms",
13682# ifdef ALL_BUILTIN_TCAPS
13683 "all_builtin_terms",
13684# endif
13685#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013686#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13687 || defined(FEAT_GUI_W32) \
13688 || defined(FEAT_GUI_MOTIF))
13689 "browsefilter",
13690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691#ifdef FEAT_BYTEOFF
13692 "byte_offset",
13693#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013694#ifdef FEAT_CHANNEL
13695 "channel",
13696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#ifdef FEAT_CINDENT
13698 "cindent",
13699#endif
13700#ifdef FEAT_CLIENTSERVER
13701 "clientserver",
13702#endif
13703#ifdef FEAT_CLIPBOARD
13704 "clipboard",
13705#endif
13706#ifdef FEAT_CMDL_COMPL
13707 "cmdline_compl",
13708#endif
13709#ifdef FEAT_CMDHIST
13710 "cmdline_hist",
13711#endif
13712#ifdef FEAT_COMMENTS
13713 "comments",
13714#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013715#ifdef FEAT_CONCEAL
13716 "conceal",
13717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef FEAT_CRYPT
13719 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013720 "crypt-blowfish",
13721 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722#endif
13723#ifdef FEAT_CSCOPE
13724 "cscope",
13725#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013726#ifdef FEAT_CURSORBIND
13727 "cursorbind",
13728#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013729#ifdef CURSOR_SHAPE
13730 "cursorshape",
13731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef DEBUG
13733 "debug",
13734#endif
13735#ifdef FEAT_CON_DIALOG
13736 "dialog_con",
13737#endif
13738#ifdef FEAT_GUI_DIALOG
13739 "dialog_gui",
13740#endif
13741#ifdef FEAT_DIFF
13742 "diff",
13743#endif
13744#ifdef FEAT_DIGRAPHS
13745 "digraphs",
13746#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013747#ifdef FEAT_DIRECTX
13748 "directx",
13749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#ifdef FEAT_DND
13751 "dnd",
13752#endif
13753#ifdef FEAT_EMACS_TAGS
13754 "emacs_tags",
13755#endif
13756 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013757 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef FEAT_SEARCH_EXTRA
13759 "extra_search",
13760#endif
13761#ifdef FEAT_FKMAP
13762 "farsi",
13763#endif
13764#ifdef FEAT_SEARCHPATH
13765 "file_in_path",
13766#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013767#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013768 "filterpipe",
13769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770#ifdef FEAT_FIND_ID
13771 "find_in_path",
13772#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013773#ifdef FEAT_FLOAT
13774 "float",
13775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_FOLDING
13777 "folding",
13778#endif
13779#ifdef FEAT_FOOTER
13780 "footer",
13781#endif
13782#if !defined(USE_SYSTEM) && defined(UNIX)
13783 "fork",
13784#endif
13785#ifdef FEAT_GETTEXT
13786 "gettext",
13787#endif
13788#ifdef FEAT_GUI
13789 "gui",
13790#endif
13791#ifdef FEAT_GUI_ATHENA
13792# ifdef FEAT_GUI_NEXTAW
13793 "gui_neXtaw",
13794# else
13795 "gui_athena",
13796# endif
13797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef FEAT_GUI_GTK
13799 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013800# ifdef USE_GTK3
13801 "gui_gtk3",
13802# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013806#ifdef FEAT_GUI_GNOME
13807 "gui_gnome",
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef FEAT_GUI_MAC
13810 "gui_mac",
13811#endif
13812#ifdef FEAT_GUI_MOTIF
13813 "gui_motif",
13814#endif
13815#ifdef FEAT_GUI_PHOTON
13816 "gui_photon",
13817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818#ifdef FEAT_GUI_W32
13819 "gui_win32",
13820#endif
13821#ifdef FEAT_HANGULIN
13822 "hangul_input",
13823#endif
13824#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13825 "iconv",
13826#endif
13827#ifdef FEAT_INS_EXPAND
13828 "insert_expand",
13829#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013830#ifdef FEAT_JOB
13831 "job",
13832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833#ifdef FEAT_JUMPLIST
13834 "jumplist",
13835#endif
13836#ifdef FEAT_KEYMAP
13837 "keymap",
13838#endif
13839#ifdef FEAT_LANGMAP
13840 "langmap",
13841#endif
13842#ifdef FEAT_LIBCALL
13843 "libcall",
13844#endif
13845#ifdef FEAT_LINEBREAK
13846 "linebreak",
13847#endif
13848#ifdef FEAT_LISP
13849 "lispindent",
13850#endif
13851#ifdef FEAT_LISTCMDS
13852 "listcmds",
13853#endif
13854#ifdef FEAT_LOCALMAP
13855 "localmap",
13856#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013857#ifdef FEAT_LUA
13858# ifndef DYNAMIC_LUA
13859 "lua",
13860# endif
13861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862#ifdef FEAT_MENU
13863 "menu",
13864#endif
13865#ifdef FEAT_SESSION
13866 "mksession",
13867#endif
13868#ifdef FEAT_MODIFY_FNAME
13869 "modify_fname",
13870#endif
13871#ifdef FEAT_MOUSE
13872 "mouse",
13873#endif
13874#ifdef FEAT_MOUSESHAPE
13875 "mouseshape",
13876#endif
13877#if defined(UNIX) || defined(VMS)
13878# ifdef FEAT_MOUSE_DEC
13879 "mouse_dec",
13880# endif
13881# ifdef FEAT_MOUSE_GPM
13882 "mouse_gpm",
13883# endif
13884# ifdef FEAT_MOUSE_JSB
13885 "mouse_jsbterm",
13886# endif
13887# ifdef FEAT_MOUSE_NET
13888 "mouse_netterm",
13889# endif
13890# ifdef FEAT_MOUSE_PTERM
13891 "mouse_pterm",
13892# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013893# ifdef FEAT_MOUSE_SGR
13894 "mouse_sgr",
13895# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013896# ifdef FEAT_SYSMOUSE
13897 "mouse_sysmouse",
13898# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013899# ifdef FEAT_MOUSE_URXVT
13900 "mouse_urxvt",
13901# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902# ifdef FEAT_MOUSE_XTERM
13903 "mouse_xterm",
13904# endif
13905#endif
13906#ifdef FEAT_MBYTE
13907 "multi_byte",
13908#endif
13909#ifdef FEAT_MBYTE_IME
13910 "multi_byte_ime",
13911#endif
13912#ifdef FEAT_MULTI_LANG
13913 "multi_lang",
13914#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013915#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013916#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013917 "mzscheme",
13918#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920#ifdef FEAT_OLE
13921 "ole",
13922#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013923 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924#ifdef FEAT_PATH_EXTRA
13925 "path_extra",
13926#endif
13927#ifdef FEAT_PERL
13928#ifndef DYNAMIC_PERL
13929 "perl",
13930#endif
13931#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013932#ifdef FEAT_PERSISTENT_UNDO
13933 "persistent_undo",
13934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935#ifdef FEAT_PYTHON
13936#ifndef DYNAMIC_PYTHON
13937 "python",
13938#endif
13939#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013940#ifdef FEAT_PYTHON3
13941#ifndef DYNAMIC_PYTHON3
13942 "python3",
13943#endif
13944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945#ifdef FEAT_POSTSCRIPT
13946 "postscript",
13947#endif
13948#ifdef FEAT_PRINTER
13949 "printer",
13950#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013951#ifdef FEAT_PROFILE
13952 "profile",
13953#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013954#ifdef FEAT_RELTIME
13955 "reltime",
13956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#ifdef FEAT_QUICKFIX
13958 "quickfix",
13959#endif
13960#ifdef FEAT_RIGHTLEFT
13961 "rightleft",
13962#endif
13963#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13964 "ruby",
13965#endif
13966#ifdef FEAT_SCROLLBIND
13967 "scrollbind",
13968#endif
13969#ifdef FEAT_CMDL_INFO
13970 "showcmd",
13971 "cmdline_info",
13972#endif
13973#ifdef FEAT_SIGNS
13974 "signs",
13975#endif
13976#ifdef FEAT_SMARTINDENT
13977 "smartindent",
13978#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013979#ifdef STARTUPTIME
13980 "startuptime",
13981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982#ifdef FEAT_STL_OPT
13983 "statusline",
13984#endif
13985#ifdef FEAT_SUN_WORKSHOP
13986 "sun_workshop",
13987#endif
13988#ifdef FEAT_NETBEANS_INTG
13989 "netbeans_intg",
13990#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013991#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013992 "spell",
13993#endif
13994#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 "syntax",
13996#endif
13997#if defined(USE_SYSTEM) || !defined(UNIX)
13998 "system",
13999#endif
14000#ifdef FEAT_TAG_BINS
14001 "tag_binary",
14002#endif
14003#ifdef FEAT_TAG_OLDSTATIC
14004 "tag_old_static",
14005#endif
14006#ifdef FEAT_TAG_ANYWHITE
14007 "tag_any_white",
14008#endif
14009#ifdef FEAT_TCL
14010# ifndef DYNAMIC_TCL
14011 "tcl",
14012# endif
14013#endif
14014#ifdef TERMINFO
14015 "terminfo",
14016#endif
14017#ifdef FEAT_TERMRESPONSE
14018 "termresponse",
14019#endif
14020#ifdef FEAT_TEXTOBJ
14021 "textobjects",
14022#endif
14023#ifdef HAVE_TGETENT
14024 "tgetent",
14025#endif
14026#ifdef FEAT_TITLE
14027 "title",
14028#endif
14029#ifdef FEAT_TOOLBAR
14030 "toolbar",
14031#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014032#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14033 "unnamedplus",
14034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035#ifdef FEAT_USR_CMDS
14036 "user-commands", /* was accidentally included in 5.4 */
14037 "user_commands",
14038#endif
14039#ifdef FEAT_VIMINFO
14040 "viminfo",
14041#endif
14042#ifdef FEAT_VERTSPLIT
14043 "vertsplit",
14044#endif
14045#ifdef FEAT_VIRTUALEDIT
14046 "virtualedit",
14047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049#ifdef FEAT_VISUALEXTRA
14050 "visualextra",
14051#endif
14052#ifdef FEAT_VREPLACE
14053 "vreplace",
14054#endif
14055#ifdef FEAT_WILDIGN
14056 "wildignore",
14057#endif
14058#ifdef FEAT_WILDMENU
14059 "wildmenu",
14060#endif
14061#ifdef FEAT_WINDOWS
14062 "windows",
14063#endif
14064#ifdef FEAT_WAK
14065 "winaltkeys",
14066#endif
14067#ifdef FEAT_WRITEBACKUP
14068 "writebackup",
14069#endif
14070#ifdef FEAT_XIM
14071 "xim",
14072#endif
14073#ifdef FEAT_XFONTSET
14074 "xfontset",
14075#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014076#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014077 "xpm",
14078 "xpm_w32", /* for backward compatibility */
14079#else
14080# if defined(HAVE_XPM)
14081 "xpm",
14082# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084#ifdef USE_XSMP
14085 "xsmp",
14086#endif
14087#ifdef USE_XSMP_INTERACT
14088 "xsmp_interact",
14089#endif
14090#ifdef FEAT_XCLIPBOARD
14091 "xterm_clipboard",
14092#endif
14093#ifdef FEAT_XTERM_SAVE
14094 "xterm_save",
14095#endif
14096#if defined(UNIX) && defined(FEAT_X11)
14097 "X11",
14098#endif
14099 NULL
14100 };
14101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 for (i = 0; has_list[i] != NULL; ++i)
14104 if (STRICMP(name, has_list[i]) == 0)
14105 {
14106 n = TRUE;
14107 break;
14108 }
14109
14110 if (n == FALSE)
14111 {
14112 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014113 {
14114 if (name[5] == '-'
14115 && STRLEN(name) > 11
14116 && vim_isdigit(name[6])
14117 && vim_isdigit(name[8])
14118 && vim_isdigit(name[10]))
14119 {
14120 int major = atoi((char *)name + 6);
14121 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014122
14123 /* Expect "patch-9.9.01234". */
14124 n = (major < VIM_VERSION_MAJOR
14125 || (major == VIM_VERSION_MAJOR
14126 && (minor < VIM_VERSION_MINOR
14127 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014128 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014129 }
14130 else
14131 n = has_patch(atoi((char *)name + 5));
14132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133 else if (STRICMP(name, "vim_starting") == 0)
14134 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014135#ifdef FEAT_MBYTE
14136 else if (STRICMP(name, "multi_byte_encoding") == 0)
14137 n = has_mbyte;
14138#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014139#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14140 else if (STRICMP(name, "balloon_multiline") == 0)
14141 n = multiline_balloon_available();
14142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#ifdef DYNAMIC_TCL
14144 else if (STRICMP(name, "tcl") == 0)
14145 n = tcl_enabled(FALSE);
14146#endif
14147#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14148 else if (STRICMP(name, "iconv") == 0)
14149 n = iconv_enabled(FALSE);
14150#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014151#ifdef DYNAMIC_LUA
14152 else if (STRICMP(name, "lua") == 0)
14153 n = lua_enabled(FALSE);
14154#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014155#ifdef DYNAMIC_MZSCHEME
14156 else if (STRICMP(name, "mzscheme") == 0)
14157 n = mzscheme_enabled(FALSE);
14158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159#ifdef DYNAMIC_RUBY
14160 else if (STRICMP(name, "ruby") == 0)
14161 n = ruby_enabled(FALSE);
14162#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014163#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164#ifdef DYNAMIC_PYTHON
14165 else if (STRICMP(name, "python") == 0)
14166 n = python_enabled(FALSE);
14167#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014168#endif
14169#ifdef FEAT_PYTHON3
14170#ifdef DYNAMIC_PYTHON3
14171 else if (STRICMP(name, "python3") == 0)
14172 n = python3_enabled(FALSE);
14173#endif
14174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175#ifdef DYNAMIC_PERL
14176 else if (STRICMP(name, "perl") == 0)
14177 n = perl_enabled(FALSE);
14178#endif
14179#ifdef FEAT_GUI
14180 else if (STRICMP(name, "gui_running") == 0)
14181 n = (gui.in_use || gui.starting);
14182# ifdef FEAT_GUI_W32
14183 else if (STRICMP(name, "gui_win32s") == 0)
14184 n = gui_is_win32s();
14185# endif
14186# ifdef FEAT_BROWSE
14187 else if (STRICMP(name, "browse") == 0)
14188 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14189# endif
14190#endif
14191#ifdef FEAT_SYN_HL
14192 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014193 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194#endif
14195#if defined(WIN3264)
14196 else if (STRICMP(name, "win95") == 0)
14197 n = mch_windows95();
14198#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014199#ifdef FEAT_NETBEANS_INTG
14200 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014201 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 }
14204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206}
14207
14208/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014209 * "has_key()" function
14210 */
14211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014212f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014213{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014214 if (argvars[0].v_type != VAR_DICT)
14215 {
14216 EMSG(_(e_dictreq));
14217 return;
14218 }
14219 if (argvars[0].vval.v_dict == NULL)
14220 return;
14221
14222 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014223 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014224}
14225
14226/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014227 * "haslocaldir()" function
14228 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014230f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014231{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014232 win_T *wp = NULL;
14233
14234 wp = find_tabwin(&argvars[0], &argvars[1]);
14235 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014236}
14237
14238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 * "hasmapto()" function
14240 */
14241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014242f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243{
14244 char_u *name;
14245 char_u *mode;
14246 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014247 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014250 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 mode = (char_u *)"nvo";
14252 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014255 if (argvars[2].v_type != VAR_UNKNOWN)
14256 abbr = get_tv_number(&argvars[2]);
14257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaar2c932302006-03-18 21:42:09 +000014259 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263}
14264
14265/*
14266 * "histadd()" function
14267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014269f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270{
14271#ifdef FEAT_CMDHIST
14272 int histype;
14273 char_u *str;
14274 char_u buf[NUMBUFLEN];
14275#endif
14276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 if (check_restricted() || check_secure())
14279 return;
14280#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014281 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14282 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 if (histype >= 0)
14284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 if (*str != NUL)
14287 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014288 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 return;
14292 }
14293 }
14294#endif
14295}
14296
14297/*
14298 * "histdel()" function
14299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014301f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302{
14303#ifdef FEAT_CMDHIST
14304 int n;
14305 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14309 if (str == NULL)
14310 n = 0;
14311 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014314 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 else
14319 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 get_tv_string_buf(&argvars[1], buf));
14322 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323#endif
14324}
14325
14326/*
14327 * "histget()" function
14328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014330f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331{
14332#ifdef FEAT_CMDHIST
14333 int type;
14334 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14338 if (str == NULL)
14339 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 {
14342 type = get_histtype(str);
14343 if (argvars[1].v_type == VAR_UNKNOWN)
14344 idx = get_history_idx(type);
14345 else
14346 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14347 /* -1 on type error */
14348 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354}
14355
14356/*
14357 * "histnr()" function
14358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014360f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361{
14362 int i;
14363
14364#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 char_u *history = get_tv_string_chk(&argvars[0]);
14366
14367 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 if (i >= HIST_CMD && i < HIST_COUNT)
14369 i = get_history_idx(i);
14370 else
14371#endif
14372 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 * "highlightID(name)" function
14378 */
14379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014380f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383}
14384
14385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014386 * "highlight_exists()" function
14387 */
14388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014390{
14391 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14392}
14393
14394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 * "hostname()" function
14396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014398f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399{
14400 char_u hostname[256];
14401
14402 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 rettv->v_type = VAR_STRING;
14404 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405}
14406
14407/*
14408 * iconv() function
14409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014411f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
14413#ifdef FEAT_MBYTE
14414 char_u buf1[NUMBUFLEN];
14415 char_u buf2[NUMBUFLEN];
14416 char_u *from, *to, *str;
14417 vimconv_T vimconv;
14418#endif
14419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420 rettv->v_type = VAR_STRING;
14421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422
14423#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 str = get_tv_string(&argvars[0]);
14425 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14426 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 vimconv.vc_type = CONV_NONE;
14428 convert_setup(&vimconv, from, to);
14429
14430 /* If the encodings are equal, no conversion needed. */
14431 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435
14436 convert_setup(&vimconv, NULL, NULL);
14437 vim_free(from);
14438 vim_free(to);
14439#endif
14440}
14441
14442/*
14443 * "indent()" function
14444 */
14445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014446f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447{
14448 linenr_T lnum;
14449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014450 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455}
14456
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014457/*
14458 * "index()" function
14459 */
14460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014461f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014462{
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 list_T *l;
14464 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014465 long idx = 0;
14466 int ic = FALSE;
14467
14468 rettv->vval.v_number = -1;
14469 if (argvars[0].v_type != VAR_LIST)
14470 {
14471 EMSG(_(e_listreq));
14472 return;
14473 }
14474 l = argvars[0].vval.v_list;
14475 if (l != NULL)
14476 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014477 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014478 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014480 int error = FALSE;
14481
Bram Moolenaar758711c2005-02-02 23:11:38 +000014482 /* Start at specified item. Use the cached index that list_find()
14483 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014485 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014486 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014487 ic = get_tv_number_chk(&argvars[3], &error);
14488 if (error)
14489 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014490 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014491
Bram Moolenaar758711c2005-02-02 23:11:38 +000014492 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014493 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014494 {
14495 rettv->vval.v_number = idx;
14496 break;
14497 }
14498 }
14499}
14500
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501static int inputsecret_flag = 0;
14502
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014503static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014504
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014506 * This function is used by f_input() and f_inputdialog() functions. The third
14507 * argument to f_input() specifies the type of completion to use at the
14508 * prompt. The third argument to f_inputdialog() specifies the value to return
14509 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 */
14511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014512get_user_input(
14513 typval_T *argvars,
14514 typval_T *rettv,
14515 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 char_u *p = NULL;
14519 int c;
14520 char_u buf[NUMBUFLEN];
14521 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014523 int xp_type = EXPAND_NOTHING;
14524 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528
14529#ifdef NO_CONSOLE_INPUT
14530 /* While starting up, there is no place to enter text. */
14531 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533#endif
14534
14535 cmd_silent = FALSE; /* Want to see the prompt. */
14536 if (prompt != NULL)
14537 {
14538 /* Only the part of the message after the last NL is considered as
14539 * prompt for the command line */
14540 p = vim_strrchr(prompt, '\n');
14541 if (p == NULL)
14542 p = prompt;
14543 else
14544 {
14545 ++p;
14546 c = *p;
14547 *p = NUL;
14548 msg_start();
14549 msg_clr_eos();
14550 msg_puts_attr(prompt, echo_attr);
14551 msg_didout = FALSE;
14552 msg_starthere();
14553 *p = c;
14554 }
14555 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 if (argvars[1].v_type != VAR_UNKNOWN)
14558 {
14559 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14560 if (defstr != NULL)
14561 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014563 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014564 {
14565 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014566 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014567 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014568
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014569 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014570 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014571
Bram Moolenaar4463f292005-09-25 22:20:24 +000014572 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14573 if (xp_name == NULL)
14574 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014576 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014577
Bram Moolenaar4463f292005-09-25 22:20:24 +000014578 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14579 &xp_arg) == FAIL)
14580 return;
14581 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014582 }
14583
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014585 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014586 int save_ex_normal_busy = ex_normal_busy;
14587 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014589 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14590 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014591 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014592 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014593 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014594 && argvars[1].v_type != VAR_UNKNOWN
14595 && argvars[2].v_type != VAR_UNKNOWN)
14596 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14597 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014598
14599 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 /* since the user typed this, no need to wait for return */
14602 need_wait_return = FALSE;
14603 msg_didout = FALSE;
14604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 cmd_silent = cmd_silent_save;
14606}
14607
14608/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014609 * "input()" function
14610 * Also handles inputsecret() when inputsecret is set.
14611 */
14612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014613f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014614{
14615 get_user_input(argvars, rettv, FALSE);
14616}
14617
14618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 * "inputdialog()" function
14620 */
14621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014622f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623{
14624#if defined(FEAT_GUI_TEXTDIALOG)
14625 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14626 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14627 {
14628 char_u *message;
14629 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 message = get_tv_string_chk(&argvars[0]);
14633 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014634 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014635 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 else
14637 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 if (message != NULL && defstr != NULL
14639 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014640 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 else
14643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 if (message != NULL && defstr != NULL
14645 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014646 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_string = vim_strsave(
14648 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 }
14654 else
14655#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014656 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657}
14658
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014659/*
14660 * "inputlist()" function
14661 */
14662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014663f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014664{
14665 listitem_T *li;
14666 int selected;
14667 int mouse_used;
14668
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014669#ifdef NO_CONSOLE_INPUT
14670 /* While starting up, there is no place to enter text. */
14671 if (no_console_input())
14672 return;
14673#endif
14674 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14675 {
14676 EMSG2(_(e_listarg), "inputlist()");
14677 return;
14678 }
14679
14680 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014681 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014682 lines_left = Rows; /* avoid more prompt */
14683 msg_scroll = TRUE;
14684 msg_clr_eos();
14685
14686 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14687 {
14688 msg_puts(get_tv_string(&li->li_tv));
14689 msg_putchar('\n');
14690 }
14691
14692 /* Ask for choice. */
14693 selected = prompt_for_number(&mouse_used);
14694 if (mouse_used)
14695 selected -= lines_left;
14696
14697 rettv->vval.v_number = selected;
14698}
14699
14700
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14702
14703/*
14704 * "inputrestore()" function
14705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014707f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708{
14709 if (ga_userinput.ga_len > 0)
14710 {
14711 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14713 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014714 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 }
14716 else if (p_verbose > 1)
14717 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014718 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 }
14721}
14722
14723/*
14724 * "inputsave()" function
14725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014729 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 if (ga_grow(&ga_userinput, 1) == OK)
14731 {
14732 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14733 + ga_userinput.ga_len);
14734 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014735 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739}
14740
14741/*
14742 * "inputsecret()" function
14743 */
14744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014745f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746{
14747 ++cmdline_star;
14748 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 --cmdline_star;
14751 --inputsecret_flag;
14752}
14753
14754/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014755 * "insert()" function
14756 */
14757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014758f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014759{
14760 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014761 listitem_T *item;
14762 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014763 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014764
14765 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014767 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014768 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014769 {
14770 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 before = get_tv_number_chk(&argvars[2], &error);
14772 if (error)
14773 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774
Bram Moolenaar758711c2005-02-02 23:11:38 +000014775 if (before == l->lv_len)
14776 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014777 else
14778 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014779 item = list_find(l, before);
14780 if (item == NULL)
14781 {
14782 EMSGN(_(e_listidx), before);
14783 l = NULL;
14784 }
14785 }
14786 if (l != NULL)
14787 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014788 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014789 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790 }
14791 }
14792}
14793
14794/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014795 * "invert(expr)" function
14796 */
14797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014798f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014799{
14800 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14801}
14802
14803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 * "isdirectory()" function
14805 */
14806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014807f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810}
14811
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014812/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014813 * "islocked()" function
14814 */
14815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014816f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014817{
14818 lval_T lv;
14819 char_u *end;
14820 dictitem_T *di;
14821
14822 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014823 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14824 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014825 if (end != NULL && lv.ll_name != NULL)
14826 {
14827 if (*end != NUL)
14828 EMSG(_(e_trailing));
14829 else
14830 {
14831 if (lv.ll_tv == NULL)
14832 {
14833 if (check_changedtick(lv.ll_name))
14834 rettv->vval.v_number = 1; /* always locked */
14835 else
14836 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014837 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014838 if (di != NULL)
14839 {
14840 /* Consider a variable locked when:
14841 * 1. the variable itself is locked
14842 * 2. the value of the variable is locked.
14843 * 3. the List or Dict value is locked.
14844 */
14845 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14846 || tv_islocked(&di->di_tv));
14847 }
14848 }
14849 }
14850 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014851 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014852 else if (lv.ll_newkey != NULL)
14853 EMSG2(_(e_dictkey), lv.ll_newkey);
14854 else if (lv.ll_list != NULL)
14855 /* List item. */
14856 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14857 else
14858 /* Dictionary item. */
14859 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14860 }
14861 }
14862
14863 clear_lval(&lv);
14864}
14865
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014866#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14867/*
14868 * "isnan()" function
14869 */
14870 static void
14871f_isnan(typval_T *argvars, typval_T *rettv)
14872{
14873 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14874 && isnan(argvars[0].vval.v_float);
14875}
14876#endif
14877
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014878static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014879
14880/*
14881 * Turn a dict into a list:
14882 * "what" == 0: list of keys
14883 * "what" == 1: list of values
14884 * "what" == 2: list of items
14885 */
14886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014887dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014888{
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 list_T *l2;
14890 dictitem_T *di;
14891 hashitem_T *hi;
14892 listitem_T *li;
14893 listitem_T *li2;
14894 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014895 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014896
Bram Moolenaar8c711452005-01-14 21:53:12 +000014897 if (argvars[0].v_type != VAR_DICT)
14898 {
14899 EMSG(_(e_dictreq));
14900 return;
14901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014902 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014903 return;
14904
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014905 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014906 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014907
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014908 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014909 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014911 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014912 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014913 --todo;
14914 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014915
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014916 li = listitem_alloc();
14917 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014918 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014919 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014920
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014921 if (what == 0)
14922 {
14923 /* keys() */
14924 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014925 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014926 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14927 }
14928 else if (what == 1)
14929 {
14930 /* values() */
14931 copy_tv(&di->di_tv, &li->li_tv);
14932 }
14933 else
14934 {
14935 /* items() */
14936 l2 = list_alloc();
14937 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014938 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014939 li->li_tv.vval.v_list = l2;
14940 if (l2 == NULL)
14941 break;
14942 ++l2->lv_refcount;
14943
14944 li2 = listitem_alloc();
14945 if (li2 == NULL)
14946 break;
14947 list_append(l2, li2);
14948 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014949 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14951
14952 li2 = listitem_alloc();
14953 if (li2 == NULL)
14954 break;
14955 list_append(l2, li2);
14956 copy_tv(&di->di_tv, &li2->li_tv);
14957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014958 }
14959 }
14960}
14961
14962/*
14963 * "items(dict)" function
14964 */
14965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014966f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014967{
14968 dict_list(argvars, rettv, 2);
14969}
14970
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014971#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014972/*
14973 * Get the job from the argument.
14974 * Returns NULL if the job is invalid.
14975 */
14976 static job_T *
14977get_job_arg(typval_T *tv)
14978{
14979 job_T *job;
14980
14981 if (tv->v_type != VAR_JOB)
14982 {
14983 EMSG2(_(e_invarg2), get_tv_string(tv));
14984 return NULL;
14985 }
14986 job = tv->vval.v_job;
14987
14988 if (job == NULL)
14989 EMSG(_("E916: not a valid job"));
14990 return job;
14991}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014992
14993# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014994/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014995 * "job_getchannel()" function
14996 */
14997 static void
14998f_job_getchannel(typval_T *argvars, typval_T *rettv)
14999{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015000 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015001
Bram Moolenaar65edff82016-02-21 16:40:11 +010015002 if (job != NULL)
15003 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015004 rettv->v_type = VAR_CHANNEL;
15005 rettv->vval.v_channel = job->jv_channel;
15006 if (job->jv_channel != NULL)
15007 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015008 }
15009}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015010# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015011
15012/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015013 * "job_setoptions()" function
15014 */
15015 static void
15016f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15017{
15018 job_T *job = get_job_arg(&argvars[0]);
15019 jobopt_T opt;
15020
15021 if (job == NULL)
15022 return;
15023 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015024 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015025 return;
15026 job_set_options(job, &opt);
15027}
15028
15029/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015030 * "job_start()" function
15031 */
15032 static void
15033f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15034{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015035 job_T *job;
15036 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015037#if defined(UNIX)
15038# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015039 char **argv = NULL;
15040 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015041#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015042 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015043#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015044 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015045
15046 rettv->v_type = VAR_JOB;
15047 job = job_alloc();
15048 rettv->vval.v_job = job;
15049 if (job == NULL)
15050 return;
15051
15052 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015053
15054 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015055 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015056 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015057 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015058 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015059 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015060 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015061 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015062
Bram Moolenaar835dc632016-02-07 14:27:38 +010015063#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015064 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015065#endif
15066
15067 if (argvars[0].v_type == VAR_STRING)
15068 {
15069 /* Command is a string. */
15070 cmd = argvars[0].vval.v_string;
15071#ifdef USE_ARGV
15072 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15073 return;
15074 argv[argc] = NULL;
15075#endif
15076 }
15077 else if (argvars[0].v_type != VAR_LIST
15078 || argvars[0].vval.v_list == NULL
15079 || argvars[0].vval.v_list->lv_len < 1)
15080 {
15081 EMSG(_(e_invarg));
15082 return;
15083 }
15084 else
15085 {
15086 list_T *l = argvars[0].vval.v_list;
15087 listitem_T *li;
15088 char_u *s;
15089
15090#ifdef USE_ARGV
15091 /* Pass argv[] to mch_call_shell(). */
15092 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15093 if (argv == NULL)
15094 return;
15095#endif
15096 for (li = l->lv_first; li != NULL; li = li->li_next)
15097 {
15098 s = get_tv_string_chk(&li->li_tv);
15099 if (s == NULL)
15100 goto theend;
15101#ifdef USE_ARGV
15102 argv[argc++] = (char *)s;
15103#else
15104 if (li != l->lv_first)
15105 {
15106 s = vim_strsave_shellescape(s, FALSE, TRUE);
15107 if (s == NULL)
15108 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015109 ga_concat(&ga, s);
15110 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015112 else
15113 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015114 if (li->li_next != NULL)
15115 ga_append(&ga, ' ');
15116#endif
15117 }
15118#ifdef USE_ARGV
15119 argv[argc] = NULL;
15120#else
15121 cmd = ga.ga_data;
15122#endif
15123 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015124
Bram Moolenaar835dc632016-02-07 14:27:38 +010015125#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015126# ifdef FEAT_CHANNEL
15127 if (ch_log_active())
15128 {
15129 garray_T ga;
15130 int i;
15131
15132 ga_init2(&ga, (int)sizeof(char), 200);
15133 for (i = 0; i < argc; ++i)
15134 {
15135 if (i > 0)
15136 ga_concat(&ga, (char_u *)" ");
15137 ga_concat(&ga, (char_u *)argv[i]);
15138 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015139 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015140 ga_clear(&ga);
15141 }
15142# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015143 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015144#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015145# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015146 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015147# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015148 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015149#endif
15150
15151theend:
15152#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015153 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015154#else
15155 vim_free(ga.ga_data);
15156#endif
15157}
15158
15159/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015160 * Get the status of "job" and invoke the exit callback when needed.
15161 * The returned string is not allocated.
15162 */
15163 static char *
15164job_status(job_T *job)
15165{
15166 char *result;
15167
15168 if (job->jv_status == JOB_ENDED)
15169 /* No need to check, dead is dead. */
15170 result = "dead";
15171 else if (job->jv_status == JOB_FAILED)
15172 result = "fail";
15173 else
15174 {
15175 result = mch_job_status(job);
15176# ifdef FEAT_CHANNEL
15177 if (job->jv_status == JOB_ENDED)
15178 ch_log(job->jv_channel, "Job ended");
15179# endif
15180 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15181 {
15182 typval_T argv[3];
15183 typval_T rettv;
15184 int dummy;
15185
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015186 /* invoke the exit callback; make sure the refcount is > 0 */
15187 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015188 argv[0].v_type = VAR_JOB;
15189 argv[0].vval.v_job = job;
15190 argv[1].v_type = VAR_NUMBER;
15191 argv[1].vval.v_number = job->jv_exitval;
15192 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15193 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15194 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015195 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015196 }
15197 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15198 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015199 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015200 * freed. Careful: caller must not use "job" after this! */
15201 job_free(job);
15202 }
15203 }
15204 return result;
15205}
15206
15207/*
15208 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15209 */
15210 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015211job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015212{
15213 static time_t last_check = 0;
15214 time_t now;
15215 job_T *job;
15216 job_T *next;
15217
15218 /* Only do this once in 10 seconds. */
15219 now = time(NULL);
15220 if (last_check + 10 < now)
15221 {
15222 last_check = now;
15223 for (job = first_job; job != NULL; job = next)
15224 {
15225 next = job->jv_next;
15226 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15227 job_status(job); /* may free "job" */
15228 }
15229 }
15230}
15231
15232/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015233 * "job_status()" function
15234 */
15235 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015236f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015237{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015238 job_T *job = get_job_arg(&argvars[0]);
15239 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015240
Bram Moolenaar65edff82016-02-21 16:40:11 +010015241 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015242 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015243 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015244 rettv->v_type = VAR_STRING;
15245 rettv->vval.v_string = vim_strsave((char_u *)result);
15246 }
15247}
15248
15249/*
15250 * "job_stop()" function
15251 */
15252 static void
15253f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15254{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015255 job_T *job = get_job_arg(&argvars[0]);
15256
15257 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015258 {
15259 char_u *arg;
15260
15261 if (argvars[1].v_type == VAR_UNKNOWN)
15262 arg = (char_u *)"";
15263 else
15264 {
15265 arg = get_tv_string_chk(&argvars[1]);
15266 if (arg == NULL)
15267 {
15268 EMSG(_(e_invarg));
15269 return;
15270 }
15271 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015272 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015273 rettv->vval.v_number = 0;
15274 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015275 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015276 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015277 /* Assume that "hup" does not kill the job. */
15278 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15279 job->jv_channel->ch_job_killed = TRUE;
15280 }
15281 /* We don't try freeing the job, obviously the caller still has a
15282 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015283 }
15284}
15285#endif
15286
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 * "join()" function
15289 */
15290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015291f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015292{
15293 garray_T ga;
15294 char_u *sep;
15295
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015296 if (argvars[0].v_type != VAR_LIST)
15297 {
15298 EMSG(_(e_listreq));
15299 return;
15300 }
15301 if (argvars[0].vval.v_list == NULL)
15302 return;
15303 if (argvars[1].v_type == VAR_UNKNOWN)
15304 sep = (char_u *)" ";
15305 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015306 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015307
15308 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309
15310 if (sep != NULL)
15311 {
15312 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015313 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 ga_append(&ga, NUL);
15315 rettv->vval.v_string = (char_u *)ga.ga_data;
15316 }
15317 else
15318 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015319}
15320
15321/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015322 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015323 */
15324 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015325f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015326{
15327 js_read_T reader;
15328
15329 reader.js_buf = get_tv_string(&argvars[0]);
15330 reader.js_fill = NULL;
15331 reader.js_used = 0;
15332 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15333 EMSG(_(e_invarg));
15334}
15335
15336/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015337 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015338 */
15339 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015340f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015341{
15342 rettv->v_type = VAR_STRING;
15343 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15344}
15345
15346/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015347 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015348 */
15349 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015350f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015351{
15352 js_read_T reader;
15353
15354 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015355 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015356 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015357 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015358 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015359}
15360
15361/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015362 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015363 */
15364 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015365f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015366{
15367 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015368 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015369}
15370
15371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015372 * "keys()" function
15373 */
15374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015375f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015376{
15377 dict_list(argvars, rettv, 0);
15378}
15379
15380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 * "last_buffer_nr()" function.
15382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015384f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385{
15386 int n = 0;
15387 buf_T *buf;
15388
15389 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15390 if (n < buf->b_fnum)
15391 n = buf->b_fnum;
15392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015394}
15395
15396/*
15397 * "len()" function
15398 */
15399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015400f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015401{
15402 switch (argvars[0].v_type)
15403 {
15404 case VAR_STRING:
15405 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->vval.v_number = (varnumber_T)STRLEN(
15407 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015408 break;
15409 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015411 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015412 case VAR_DICT:
15413 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15414 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015415 case VAR_UNKNOWN:
15416 case VAR_SPECIAL:
15417 case VAR_FLOAT:
15418 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015419 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015420 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015421 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015422 break;
15423 }
15424}
15425
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015426static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015427
15428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015429libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015430{
15431#ifdef FEAT_LIBCALL
15432 char_u *string_in;
15433 char_u **string_result;
15434 int nr_result;
15435#endif
15436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015438 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440
15441 if (check_restricted() || check_secure())
15442 return;
15443
15444#ifdef FEAT_LIBCALL
15445 /* The first two args must be strings, otherwise its meaningless */
15446 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15447 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015448 string_in = NULL;
15449 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450 string_in = argvars[2].vval.v_string;
15451 if (type == VAR_NUMBER)
15452 string_result = NULL;
15453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015454 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015455 if (mch_libcall(argvars[0].vval.v_string,
15456 argvars[1].vval.v_string,
15457 string_in,
15458 argvars[2].vval.v_number,
15459 string_result,
15460 &nr_result) == OK
15461 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015463 }
15464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465}
15466
15467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015468 * "libcall()" function
15469 */
15470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015471f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015472{
15473 libcall_common(argvars, rettv, VAR_STRING);
15474}
15475
15476/*
15477 * "libcallnr()" function
15478 */
15479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015480f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015481{
15482 libcall_common(argvars, rettv, VAR_NUMBER);
15483}
15484
15485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 * "line(string)" function
15487 */
15488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015489f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490{
15491 linenr_T lnum = 0;
15492 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015493 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015495 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 if (fp != NULL)
15497 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015498 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499}
15500
15501/*
15502 * "line2byte(lnum)" function
15503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015505f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506{
15507#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509#else
15510 linenr_T lnum;
15511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015512 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15517 if (rettv->vval.v_number >= 0)
15518 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519#endif
15520}
15521
15522/*
15523 * "lispindent(lnum)" function
15524 */
15525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527{
15528#ifdef FEAT_LISP
15529 pos_T pos;
15530 linenr_T lnum;
15531
15532 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15535 {
15536 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 curwin->w_cursor = pos;
15539 }
15540 else
15541#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543}
15544
15545/*
15546 * "localtime()" function
15547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015549f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552}
15553
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015554static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555
15556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558{
15559 char_u *keys;
15560 char_u *which;
15561 char_u buf[NUMBUFLEN];
15562 char_u *keys_buf = NULL;
15563 char_u *rhs;
15564 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015565 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015566 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015567 mapblock_T *mp;
15568 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
15570 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->v_type = VAR_STRING;
15572 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 if (*keys == NUL)
15576 return;
15577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015582 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015583 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015584 if (argvars[3].v_type != VAR_UNKNOWN)
15585 get_dict = get_tv_number(&argvars[3]);
15586 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 else
15589 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015590 if (which == NULL)
15591 return;
15592
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 mode = get_map_mode(&which, 0);
15594
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015595 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015596 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015598
15599 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015601 /* Return a string. */
15602 if (rhs != NULL)
15603 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
Bram Moolenaarbd743252010-10-20 21:23:33 +020015605 }
15606 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15607 {
15608 /* Return a dictionary. */
15609 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15610 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15611 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612
Bram Moolenaarbd743252010-10-20 21:23:33 +020015613 dict_add_nr_str(dict, "lhs", 0L, lhs);
15614 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15615 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15616 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15617 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15618 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15619 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015620 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015621 dict_add_nr_str(dict, "mode", 0L, mapmode);
15622
15623 vim_free(lhs);
15624 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 }
15626}
15627
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015628#ifdef FEAT_FLOAT
15629/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015630 * "log()" function
15631 */
15632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015633f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015634{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015635 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015636
15637 rettv->v_type = VAR_FLOAT;
15638 if (get_float_arg(argvars, &f) == OK)
15639 rettv->vval.v_float = log(f);
15640 else
15641 rettv->vval.v_float = 0.0;
15642}
15643
15644/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015645 * "log10()" function
15646 */
15647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015648f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015649{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015650 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015651
15652 rettv->v_type = VAR_FLOAT;
15653 if (get_float_arg(argvars, &f) == OK)
15654 rettv->vval.v_float = log10(f);
15655 else
15656 rettv->vval.v_float = 0.0;
15657}
15658#endif
15659
Bram Moolenaar1dced572012-04-05 16:54:08 +020015660#ifdef FEAT_LUA
15661/*
15662 * "luaeval()" function
15663 */
15664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015665f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015666{
15667 char_u *str;
15668 char_u buf[NUMBUFLEN];
15669
15670 str = get_tv_string_buf(&argvars[0], buf);
15671 do_luaeval(str, argvars + 1, rettv);
15672}
15673#endif
15674
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015676 * "map()" function
15677 */
15678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015679f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015680{
15681 filter_map(argvars, rettv, TRUE);
15682}
15683
15684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691}
15692
15693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 */
15696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015697f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015699 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700}
15701
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015702static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703
15704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015707 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015708 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 char_u *pat;
15711 regmatch_T regmatch;
15712 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015713 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 char_u *save_cpo;
15715 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015716 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015717 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015718 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 list_T *l = NULL;
15720 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723
15724 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15725 save_cpo = p_cpo;
15726 p_cpo = (char_u *)"";
15727
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 rettv->vval.v_number = -1;
15729 if (type == 3)
15730 {
15731 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015732 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015733 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 }
15735 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737 rettv->v_type = VAR_STRING;
15738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015741 if (argvars[0].v_type == VAR_LIST)
15742 {
15743 if ((l = argvars[0].vval.v_list) == NULL)
15744 goto theend;
15745 li = l->lv_first;
15746 }
15747 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015748 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015749 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015750 len = (long)STRLEN(str);
15751 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015753 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15754 if (pat == NULL)
15755 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015756
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015757 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015759 int error = FALSE;
15760
15761 start = get_tv_number_chk(&argvars[2], &error);
15762 if (error)
15763 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 if (l != NULL)
15765 {
15766 li = list_find(l, start);
15767 if (li == NULL)
15768 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015769 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770 }
15771 else
15772 {
15773 if (start < 0)
15774 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015775 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015776 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015777 /* When "count" argument is there ignore matches before "start",
15778 * otherwise skip part of the string. Differs when pattern is "^"
15779 * or "\<". */
15780 if (argvars[3].v_type != VAR_UNKNOWN)
15781 startcol = start;
15782 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015783 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015784 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015785 len -= start;
15786 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015788
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015789 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015790 nth = get_tv_number_chk(&argvars[3], &error);
15791 if (error)
15792 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 }
15794
15795 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15796 if (regmatch.regprog != NULL)
15797 {
15798 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015799
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015800 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015801 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015802 if (l != NULL)
15803 {
15804 if (li == NULL)
15805 {
15806 match = FALSE;
15807 break;
15808 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015809 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015810 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015811 if (str == NULL)
15812 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015813 }
15814
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015815 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015817 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015818 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015819 if (l == NULL && !match)
15820 break;
15821
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015822 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015823 if (l != NULL)
15824 {
15825 li = li->li_next;
15826 ++idx;
15827 }
15828 else
15829 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015830#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015831 startcol = (colnr_T)(regmatch.startp[0]
15832 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015833#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015834 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015835#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015836 if (startcol > (colnr_T)len
15837 || str + startcol <= regmatch.startp[0])
15838 {
15839 match = FALSE;
15840 break;
15841 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015842 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015843 }
15844
15845 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015847 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015848 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849 int i;
15850
15851 /* return list with matched string and submatches */
15852 for (i = 0; i < NSUBEXP; ++i)
15853 {
15854 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015855 {
15856 if (list_append_string(rettv->vval.v_list,
15857 (char_u *)"", 0) == FAIL)
15858 break;
15859 }
15860 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015861 regmatch.startp[i],
15862 (int)(regmatch.endp[i] - regmatch.startp[i]))
15863 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015864 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015865 }
15866 }
15867 else if (type == 2)
15868 {
15869 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015870 if (l != NULL)
15871 copy_tv(&li->li_tv, rettv);
15872 else
15873 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015875 }
15876 else if (l != NULL)
15877 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 else
15879 {
15880 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882 (varnumber_T)(regmatch.startp[0] - str);
15883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015884 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015886 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 }
15888 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015889 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 }
15891
15892theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015893 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894 p_cpo = save_cpo;
15895}
15896
15897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015898 * "match()" function
15899 */
15900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015901f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902{
15903 find_some_match(argvars, rettv, 1);
15904}
15905
15906/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015907 * "matchadd()" function
15908 */
15909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015910f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015911{
15912#ifdef FEAT_SEARCH_EXTRA
15913 char_u buf[NUMBUFLEN];
15914 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15915 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15916 int prio = 10; /* default priority */
15917 int id = -1;
15918 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015919 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015920
15921 rettv->vval.v_number = -1;
15922
15923 if (grp == NULL || pat == NULL)
15924 return;
15925 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015926 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015927 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015928 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015929 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015930 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015931 if (argvars[4].v_type != VAR_UNKNOWN)
15932 {
15933 if (argvars[4].v_type != VAR_DICT)
15934 {
15935 EMSG(_(e_dictreq));
15936 return;
15937 }
15938 if (dict_find(argvars[4].vval.v_dict,
15939 (char_u *)"conceal", -1) != NULL)
15940 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15941 (char_u *)"conceal", FALSE);
15942 }
15943 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015944 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015945 if (error == TRUE)
15946 return;
15947 if (id >= 1 && id <= 3)
15948 {
15949 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15950 return;
15951 }
15952
Bram Moolenaar6561d522015-07-21 15:48:27 +020015953 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15954 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015955#endif
15956}
15957
15958/*
15959 * "matchaddpos()" function
15960 */
15961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015963{
15964#ifdef FEAT_SEARCH_EXTRA
15965 char_u buf[NUMBUFLEN];
15966 char_u *group;
15967 int prio = 10;
15968 int id = -1;
15969 int error = FALSE;
15970 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015971 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015972
15973 rettv->vval.v_number = -1;
15974
15975 group = get_tv_string_buf_chk(&argvars[0], buf);
15976 if (group == NULL)
15977 return;
15978
15979 if (argvars[1].v_type != VAR_LIST)
15980 {
15981 EMSG2(_(e_listarg), "matchaddpos()");
15982 return;
15983 }
15984 l = argvars[1].vval.v_list;
15985 if (l == NULL)
15986 return;
15987
15988 if (argvars[2].v_type != VAR_UNKNOWN)
15989 {
15990 prio = get_tv_number_chk(&argvars[2], &error);
15991 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015992 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015993 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015994 if (argvars[4].v_type != VAR_UNKNOWN)
15995 {
15996 if (argvars[4].v_type != VAR_DICT)
15997 {
15998 EMSG(_(e_dictreq));
15999 return;
16000 }
16001 if (dict_find(argvars[4].vval.v_dict,
16002 (char_u *)"conceal", -1) != NULL)
16003 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16004 (char_u *)"conceal", FALSE);
16005 }
16006 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016007 }
16008 if (error == TRUE)
16009 return;
16010
16011 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16012 if (id == 1 || id == 2)
16013 {
16014 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16015 return;
16016 }
16017
Bram Moolenaar6561d522015-07-21 15:48:27 +020016018 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16019 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016020#endif
16021}
16022
16023/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016024 * "matcharg()" function
16025 */
16026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016027f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016028{
16029 if (rettv_list_alloc(rettv) == OK)
16030 {
16031#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016032 int id = get_tv_number(&argvars[0]);
16033 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016034
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016035 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016036 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016037 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16038 {
16039 list_append_string(rettv->vval.v_list,
16040 syn_id2name(m->hlg_id), -1);
16041 list_append_string(rettv->vval.v_list, m->pattern, -1);
16042 }
16043 else
16044 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016045 list_append_string(rettv->vval.v_list, NULL, -1);
16046 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016047 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016048 }
16049#endif
16050 }
16051}
16052
16053/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016054 * "matchdelete()" function
16055 */
16056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016057f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016058{
16059#ifdef FEAT_SEARCH_EXTRA
16060 rettv->vval.v_number = match_delete(curwin,
16061 (int)get_tv_number(&argvars[0]), TRUE);
16062#endif
16063}
16064
16065/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016066 * "matchend()" function
16067 */
16068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016069f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016070{
16071 find_some_match(argvars, rettv, 0);
16072}
16073
16074/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016075 * "matchlist()" function
16076 */
16077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016078f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016079{
16080 find_some_match(argvars, rettv, 3);
16081}
16082
16083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016084 * "matchstr()" function
16085 */
16086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016087f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016088{
16089 find_some_match(argvars, rettv, 2);
16090}
16091
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016092static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016093
16094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016095max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016096{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016097 long n = 0;
16098 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016099 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016100
16101 if (argvars[0].v_type == VAR_LIST)
16102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016103 list_T *l;
16104 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016105
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016106 l = argvars[0].vval.v_list;
16107 if (l != NULL)
16108 {
16109 li = l->lv_first;
16110 if (li != NULL)
16111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016113 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016114 {
16115 li = li->li_next;
16116 if (li == NULL)
16117 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016119 if (domax ? i > n : i < n)
16120 n = i;
16121 }
16122 }
16123 }
16124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016125 else if (argvars[0].v_type == VAR_DICT)
16126 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016128 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016129 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016130 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016131
16132 d = argvars[0].vval.v_dict;
16133 if (d != NULL)
16134 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016135 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016136 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016138 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016140 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016142 if (first)
16143 {
16144 n = i;
16145 first = FALSE;
16146 }
16147 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016148 n = i;
16149 }
16150 }
16151 }
16152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016153 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016154 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016155 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016156}
16157
16158/*
16159 * "max()" function
16160 */
16161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016162f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016163{
16164 max_min(argvars, rettv, TRUE);
16165}
16166
16167/*
16168 * "min()" function
16169 */
16170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016171f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016172{
16173 max_min(argvars, rettv, FALSE);
16174}
16175
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016176static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016177
16178/*
16179 * Create the directory in which "dir" is located, and higher levels when
16180 * needed.
16181 */
16182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016184{
16185 char_u *p;
16186 char_u *updir;
16187 int r = FAIL;
16188
16189 /* Get end of directory name in "dir".
16190 * We're done when it's "/" or "c:/". */
16191 p = gettail_sep(dir);
16192 if (p <= get_past_head(dir))
16193 return OK;
16194
16195 /* If the directory exists we're done. Otherwise: create it.*/
16196 updir = vim_strnsave(dir, (int)(p - dir));
16197 if (updir == NULL)
16198 return FAIL;
16199 if (mch_isdir(updir))
16200 r = OK;
16201 else if (mkdir_recurse(updir, prot) == OK)
16202 r = vim_mkdir_emsg(updir, prot);
16203 vim_free(updir);
16204 return r;
16205}
16206
16207#ifdef vim_mkdir
16208/*
16209 * "mkdir()" function
16210 */
16211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016212f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016213{
16214 char_u *dir;
16215 char_u buf[NUMBUFLEN];
16216 int prot = 0755;
16217
16218 rettv->vval.v_number = FAIL;
16219 if (check_restricted() || check_secure())
16220 return;
16221
16222 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016223 if (*dir == NUL)
16224 rettv->vval.v_number = FAIL;
16225 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016226 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016227 if (*gettail(dir) == NUL)
16228 /* remove trailing slashes */
16229 *gettail_sep(dir) = NUL;
16230
16231 if (argvars[1].v_type != VAR_UNKNOWN)
16232 {
16233 if (argvars[2].v_type != VAR_UNKNOWN)
16234 prot = get_tv_number_chk(&argvars[2], NULL);
16235 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16236 mkdir_recurse(dir, prot);
16237 }
16238 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016239 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016240}
16241#endif
16242
Bram Moolenaar0d660222005-01-07 21:51:51 +000016243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 * "mode()" function
16245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016247f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016249 char_u buf[3];
16250
16251 buf[1] = NUL;
16252 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 if (VIsual_active)
16255 {
16256 if (VIsual_select)
16257 buf[0] = VIsual_mode + 's' - 'v';
16258 else
16259 buf[0] = VIsual_mode;
16260 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016261 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016262 || State == CONFIRM)
16263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016265 if (State == ASKMORE)
16266 buf[1] = 'm';
16267 else if (State == CONFIRM)
16268 buf[1] = '?';
16269 }
16270 else if (State == EXTERNCMD)
16271 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 else if (State & INSERT)
16273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016274#ifdef FEAT_VREPLACE
16275 if (State & VREPLACE_FLAG)
16276 {
16277 buf[0] = 'R';
16278 buf[1] = 'v';
16279 }
16280 else
16281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 if (State & REPLACE_FLAG)
16283 buf[0] = 'R';
16284 else
16285 buf[0] = 'i';
16286 }
16287 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016290 if (exmode_active)
16291 buf[1] = 'v';
16292 }
16293 else if (exmode_active)
16294 {
16295 buf[0] = 'c';
16296 buf[1] = 'e';
16297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016301 if (finish_op)
16302 buf[1] = 'o';
16303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016305 /* Clear out the minor mode when the argument is not a non-zero number or
16306 * non-empty string. */
16307 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016308 buf[1] = NUL;
16309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310 rettv->vval.v_string = vim_strsave(buf);
16311 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312}
16313
Bram Moolenaar429fa852013-04-15 12:27:36 +020016314#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016315/*
16316 * "mzeval()" function
16317 */
16318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016319f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016320{
16321 char_u *str;
16322 char_u buf[NUMBUFLEN];
16323
16324 str = get_tv_string_buf(&argvars[0], buf);
16325 do_mzeval(str, rettv);
16326}
Bram Moolenaar75676462013-01-30 14:55:42 +010016327
16328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016329mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016330{
16331 typval_T argvars[3];
16332
16333 argvars[0].v_type = VAR_STRING;
16334 argvars[0].vval.v_string = name;
16335 copy_tv(args, &argvars[1]);
16336 argvars[2].v_type = VAR_UNKNOWN;
16337 f_call(argvars, rettv);
16338 clear_tv(&argvars[1]);
16339}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016340#endif
16341
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 * "nextnonblank()" function
16344 */
16345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016346f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347{
16348 linenr_T lnum;
16349
16350 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016352 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 {
16354 lnum = 0;
16355 break;
16356 }
16357 if (*skipwhite(ml_get(lnum)) != NUL)
16358 break;
16359 }
16360 rettv->vval.v_number = lnum;
16361}
16362
16363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364 * "nr2char()" function
16365 */
16366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016367f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368{
16369 char_u buf[NUMBUFLEN];
16370
16371#ifdef FEAT_MBYTE
16372 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016373 {
16374 int utf8 = 0;
16375
16376 if (argvars[1].v_type != VAR_UNKNOWN)
16377 utf8 = get_tv_number_chk(&argvars[1], NULL);
16378 if (utf8)
16379 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16380 else
16381 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 else
16384#endif
16385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016386 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 buf[1] = NUL;
16388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016389 rettv->v_type = VAR_STRING;
16390 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016391}
16392
16393/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016394 * "or(expr, expr)" function
16395 */
16396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016398{
16399 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16400 | get_tv_number_chk(&argvars[1], NULL);
16401}
16402
16403/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016404 * "pathshorten()" function
16405 */
16406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016408{
16409 char_u *p;
16410
16411 rettv->v_type = VAR_STRING;
16412 p = get_tv_string_chk(&argvars[0]);
16413 if (p == NULL)
16414 rettv->vval.v_string = NULL;
16415 else
16416 {
16417 p = vim_strsave(p);
16418 rettv->vval.v_string = p;
16419 if (p != NULL)
16420 shorten_dir(p);
16421 }
16422}
16423
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016424#ifdef FEAT_PERL
16425/*
16426 * "perleval()" function
16427 */
16428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016429f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016430{
16431 char_u *str;
16432 char_u buf[NUMBUFLEN];
16433
16434 str = get_tv_string_buf(&argvars[0], buf);
16435 do_perleval(str, rettv);
16436}
16437#endif
16438
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016439#ifdef FEAT_FLOAT
16440/*
16441 * "pow()" function
16442 */
16443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016444f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016445{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016446 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016447
16448 rettv->v_type = VAR_FLOAT;
16449 if (get_float_arg(argvars, &fx) == OK
16450 && get_float_arg(&argvars[1], &fy) == OK)
16451 rettv->vval.v_float = pow(fx, fy);
16452 else
16453 rettv->vval.v_float = 0.0;
16454}
16455#endif
16456
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016457/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 * "prevnonblank()" function
16459 */
16460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016461f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016462{
16463 linenr_T lnum;
16464
16465 lnum = get_tv_lnum(argvars);
16466 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16467 lnum = 0;
16468 else
16469 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16470 --lnum;
16471 rettv->vval.v_number = lnum;
16472}
16473
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016474/* This dummy va_list is here because:
16475 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16476 * - locally in the function results in a "used before set" warning
16477 * - using va_start() to initialize it gives "function with fixed args" error */
16478static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016479
Bram Moolenaar8c711452005-01-14 21:53:12 +000016480/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016481 * "printf()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016485{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016486 char_u buf[NUMBUFLEN];
16487 int len;
16488 char_u *s;
16489 int saved_did_emsg = did_emsg;
16490 char *fmt;
16491
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016492 rettv->v_type = VAR_STRING;
16493 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016494
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016495 /* Get the required length, allocate the buffer and do it for real. */
16496 did_emsg = FALSE;
16497 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16498 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16499 if (!did_emsg)
16500 {
16501 s = alloc(len + 1);
16502 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016503 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016504 rettv->vval.v_string = s;
16505 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016506 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016507 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016508 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016509}
16510
16511/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016512 * "pumvisible()" function
16513 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016515f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016516{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016517#ifdef FEAT_INS_EXPAND
16518 if (pum_visible())
16519 rettv->vval.v_number = 1;
16520#endif
16521}
16522
Bram Moolenaardb913952012-06-29 12:54:53 +020016523#ifdef FEAT_PYTHON3
16524/*
16525 * "py3eval()" function
16526 */
16527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016528f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016529{
16530 char_u *str;
16531 char_u buf[NUMBUFLEN];
16532
16533 str = get_tv_string_buf(&argvars[0], buf);
16534 do_py3eval(str, rettv);
16535}
16536#endif
16537
16538#ifdef FEAT_PYTHON
16539/*
16540 * "pyeval()" function
16541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016544{
16545 char_u *str;
16546 char_u buf[NUMBUFLEN];
16547
16548 str = get_tv_string_buf(&argvars[0], buf);
16549 do_pyeval(str, rettv);
16550}
16551#endif
16552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016554 * "range()" function
16555 */
16556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016557f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016558{
16559 long start;
16560 long end;
16561 long stride = 1;
16562 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016563 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016566 if (argvars[1].v_type == VAR_UNKNOWN)
16567 {
16568 end = start - 1;
16569 start = 0;
16570 }
16571 else
16572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016576 }
16577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016578 if (error)
16579 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016580 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016581 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016582 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016583 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016584 else
16585 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016586 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016588 if (list_append_number(rettv->vval.v_list,
16589 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016590 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016591 }
16592}
16593
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016594/*
16595 * "readfile()" function
16596 */
16597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016598f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599{
16600 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016601 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016602 char_u *fname;
16603 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016604 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16605 int io_size = sizeof(buf);
16606 int readlen; /* size of last fread() */
16607 char_u *prev = NULL; /* previously read bytes, if any */
16608 long prevlen = 0; /* length of data in prev */
16609 long prevsize = 0; /* size of prev buffer */
16610 long maxline = MAXLNUM;
16611 long cnt = 0;
16612 char_u *p; /* position in buf */
16613 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016614
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016615 if (argvars[1].v_type != VAR_UNKNOWN)
16616 {
16617 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16618 binary = TRUE;
16619 if (argvars[2].v_type != VAR_UNKNOWN)
16620 maxline = get_tv_number(&argvars[2]);
16621 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016622
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016623 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625
16626 /* Always open the file in binary mode, library functions have a mind of
16627 * their own about CR-LF conversion. */
16628 fname = get_tv_string(&argvars[0]);
16629 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16630 {
16631 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16632 return;
16633 }
16634
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016635 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016636 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016639 /* This for loop processes what was read, but is also entered at end
16640 * of file so that either:
16641 * - an incomplete line gets written
16642 * - a "binary" file gets an empty line at the end if it ends in a
16643 * newline. */
16644 for (p = buf, start = buf;
16645 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16646 ++p)
16647 {
16648 if (*p == '\n' || readlen <= 0)
16649 {
16650 listitem_T *li;
16651 char_u *s = NULL;
16652 long_u len = p - start;
16653
16654 /* Finished a line. Remove CRs before NL. */
16655 if (readlen > 0 && !binary)
16656 {
16657 while (len > 0 && start[len - 1] == '\r')
16658 --len;
16659 /* removal may cross back to the "prev" string */
16660 if (len == 0)
16661 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16662 --prevlen;
16663 }
16664 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016665 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016666 else
16667 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016668 /* Change "prev" buffer to be the right size. This way
16669 * the bytes are only copied once, and very long lines are
16670 * allocated only once. */
16671 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016672 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016673 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016675 prev = NULL; /* the list will own the string */
16676 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016677 }
16678 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016679 if (s == NULL)
16680 {
16681 do_outofmem_msg((long_u) prevlen + len + 1);
16682 failed = TRUE;
16683 break;
16684 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016685
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016686 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016687 {
16688 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016689 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016690 break;
16691 }
16692 li->li_tv.v_type = VAR_STRING;
16693 li->li_tv.v_lock = 0;
16694 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016697 start = p + 1; /* step over newline */
16698 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016699 break;
16700 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016701 else if (*p == NUL)
16702 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016703#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016704 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16705 * when finding the BF and check the previous two bytes. */
16706 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16709 * + 1, these may be in the "prev" string. */
16710 char_u back1 = p >= buf + 1 ? p[-1]
16711 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16712 char_u back2 = p >= buf + 2 ? p[-2]
16713 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16714 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016715
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016716 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016718 char_u *dest = p - 2;
16719
16720 /* Usually a BOM is at the beginning of a file, and so at
16721 * the beginning of a line; then we can just step over it.
16722 */
16723 if (start == dest)
16724 start = p + 1;
16725 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016726 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 /* have to shuffle buf to close gap */
16728 int adjust_prevlen = 0;
16729
16730 if (dest < buf)
16731 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016732 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016733 dest = buf;
16734 }
16735 if (readlen > p - buf + 1)
16736 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16737 readlen -= 3 - adjust_prevlen;
16738 prevlen -= adjust_prevlen;
16739 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016740 }
16741 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016742 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016743#endif
16744 } /* for */
16745
16746 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16747 break;
16748 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 /* There's part of a line in buf, store it in "prev". */
16751 if (p - start + prevlen >= prevsize)
16752 {
16753 /* need bigger "prev" buffer */
16754 char_u *newprev;
16755
16756 /* A common use case is ordinary text files and "prev" gets a
16757 * fragment of a line, so the first allocation is made
16758 * small, to avoid repeatedly 'allocing' large and
16759 * 'reallocing' small. */
16760 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016761 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016762 else
16763 {
16764 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016765 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016766 prevsize = grow50pc > growmin ? grow50pc : growmin;
16767 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016768 newprev = prev == NULL ? alloc(prevsize)
16769 : vim_realloc(prev, prevsize);
16770 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016771 {
16772 do_outofmem_msg((long_u)prevsize);
16773 failed = TRUE;
16774 break;
16775 }
16776 prev = newprev;
16777 }
16778 /* Add the line part to end of "prev". */
16779 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016780 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016781 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016782 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016783
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016784 /*
16785 * For a negative line count use only the lines at the end of the file,
16786 * free the rest.
16787 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016788 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016789 while (cnt > -maxline)
16790 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016791 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016792 --cnt;
16793 }
16794
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016795 if (failed)
16796 {
16797 list_free(rettv->vval.v_list, TRUE);
16798 /* readfile doc says an empty list is returned on error */
16799 rettv->vval.v_list = list_alloc();
16800 }
16801
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016802 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803 fclose(fd);
16804}
16805
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016806#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016807static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016808
16809/*
16810 * Convert a List to proftime_T.
16811 * Return FAIL when there is something wrong.
16812 */
16813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016814list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016815{
16816 long n1, n2;
16817 int error = FALSE;
16818
16819 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16820 || arg->vval.v_list->lv_len != 2)
16821 return FAIL;
16822 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16823 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16824# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016825 tm->HighPart = n1;
16826 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016827# else
16828 tm->tv_sec = n1;
16829 tm->tv_usec = n2;
16830# endif
16831 return error ? FAIL : OK;
16832}
16833#endif /* FEAT_RELTIME */
16834
16835/*
16836 * "reltime()" function
16837 */
16838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016839f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016840{
16841#ifdef FEAT_RELTIME
16842 proftime_T res;
16843 proftime_T start;
16844
16845 if (argvars[0].v_type == VAR_UNKNOWN)
16846 {
16847 /* No arguments: get current time. */
16848 profile_start(&res);
16849 }
16850 else if (argvars[1].v_type == VAR_UNKNOWN)
16851 {
16852 if (list2proftime(&argvars[0], &res) == FAIL)
16853 return;
16854 profile_end(&res);
16855 }
16856 else
16857 {
16858 /* Two arguments: compute the difference. */
16859 if (list2proftime(&argvars[0], &start) == FAIL
16860 || list2proftime(&argvars[1], &res) == FAIL)
16861 return;
16862 profile_sub(&res, &start);
16863 }
16864
16865 if (rettv_list_alloc(rettv) == OK)
16866 {
16867 long n1, n2;
16868
16869# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016870 n1 = res.HighPart;
16871 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016872# else
16873 n1 = res.tv_sec;
16874 n2 = res.tv_usec;
16875# endif
16876 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16877 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16878 }
16879#endif
16880}
16881
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016882#ifdef FEAT_FLOAT
16883/*
16884 * "reltimefloat()" function
16885 */
16886 static void
16887f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16888{
16889# ifdef FEAT_RELTIME
16890 proftime_T tm;
16891# endif
16892
16893 rettv->v_type = VAR_FLOAT;
16894 rettv->vval.v_float = 0;
16895# ifdef FEAT_RELTIME
16896 if (list2proftime(&argvars[0], &tm) == OK)
16897 rettv->vval.v_float = profile_float(&tm);
16898# endif
16899}
16900#endif
16901
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016902/*
16903 * "reltimestr()" function
16904 */
16905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016906f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016907{
16908#ifdef FEAT_RELTIME
16909 proftime_T tm;
16910#endif
16911
16912 rettv->v_type = VAR_STRING;
16913 rettv->vval.v_string = NULL;
16914#ifdef FEAT_RELTIME
16915 if (list2proftime(&argvars[0], &tm) == OK)
16916 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16917#endif
16918}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016919
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016921static void make_connection(void);
16922static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923
16924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016925make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926{
16927 if (X_DISPLAY == NULL
16928# ifdef FEAT_GUI
16929 && !gui.in_use
16930# endif
16931 )
16932 {
16933 x_force_connect = TRUE;
16934 setup_term_clip();
16935 x_force_connect = FALSE;
16936 }
16937}
16938
16939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016940check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941{
16942 make_connection();
16943 if (X_DISPLAY == NULL)
16944 {
16945 EMSG(_("E240: No connection to Vim server"));
16946 return FAIL;
16947 }
16948 return OK;
16949}
16950#endif
16951
16952#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016953static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954
16955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016956remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957{
16958 char_u *server_name;
16959 char_u *keys;
16960 char_u *r = NULL;
16961 char_u buf[NUMBUFLEN];
16962# ifdef WIN32
16963 HWND w;
16964# else
16965 Window w;
16966# endif
16967
16968 if (check_restricted() || check_secure())
16969 return;
16970
16971# ifdef FEAT_X11
16972 if (check_connection() == FAIL)
16973 return;
16974# endif
16975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 server_name = get_tv_string_chk(&argvars[0]);
16977 if (server_name == NULL)
16978 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 keys = get_tv_string_buf(&argvars[1], buf);
16980# ifdef WIN32
16981 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16982# else
16983 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16984 < 0)
16985# endif
16986 {
16987 if (r != NULL)
16988 EMSG(r); /* sending worked but evaluation failed */
16989 else
16990 EMSG2(_("E241: Unable to send to %s"), server_name);
16991 return;
16992 }
16993
16994 rettv->vval.v_string = r;
16995
16996 if (argvars[2].v_type != VAR_UNKNOWN)
16997 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016999 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017002 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017003 v.di_tv.v_type = VAR_STRING;
17004 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 idvar = get_tv_string_chk(&argvars[2]);
17006 if (idvar != NULL)
17007 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017008 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 }
17010}
17011#endif
17012
17013/*
17014 * "remote_expr()" function
17015 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017017f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017018{
17019 rettv->v_type = VAR_STRING;
17020 rettv->vval.v_string = NULL;
17021#ifdef FEAT_CLIENTSERVER
17022 remote_common(argvars, rettv, TRUE);
17023#endif
17024}
17025
17026/*
17027 * "remote_foreground()" function
17028 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017030f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032#ifdef FEAT_CLIENTSERVER
17033# ifdef WIN32
17034 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 {
17036 char_u *server_name = get_tv_string_chk(&argvars[0]);
17037
17038 if (server_name != NULL)
17039 serverForeground(server_name);
17040 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041# else
17042 /* Send a foreground() expression to the server. */
17043 argvars[1].v_type = VAR_STRING;
17044 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17045 argvars[2].v_type = VAR_UNKNOWN;
17046 remote_common(argvars, rettv, TRUE);
17047 vim_free(argvars[1].vval.v_string);
17048# endif
17049#endif
17050}
17051
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017053f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054{
17055#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 char_u *s = NULL;
17058# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017059 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017061 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062
17063 if (check_restricted() || check_secure())
17064 {
17065 rettv->vval.v_number = -1;
17066 return;
17067 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017068 serverid = get_tv_string_chk(&argvars[0]);
17069 if (serverid == NULL)
17070 {
17071 rettv->vval.v_number = -1;
17072 return; /* type error; errmsg already given */
17073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017075 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 if (n == 0)
17077 rettv->vval.v_number = -1;
17078 else
17079 {
17080 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17081 rettv->vval.v_number = (s != NULL);
17082 }
17083# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084 if (check_connection() == FAIL)
17085 return;
17086
17087 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089# endif
17090
17091 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017093 char_u *retvar;
17094
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 v.di_tv.v_type = VAR_STRING;
17096 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 retvar = get_tv_string_chk(&argvars[1]);
17098 if (retvar != NULL)
17099 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 }
17102#else
17103 rettv->vval.v_number = -1;
17104#endif
17105}
17106
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017108f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109{
17110 char_u *r = NULL;
17111
17112#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017113 char_u *serverid = get_tv_string_chk(&argvars[0]);
17114
17115 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 {
17117# ifdef WIN32
17118 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017119 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017121 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 if (n != 0)
17123 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17124 if (r == NULL)
17125# else
17126 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017127 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128# endif
17129 EMSG(_("E277: Unable to read a server reply"));
17130 }
17131#endif
17132 rettv->v_type = VAR_STRING;
17133 rettv->vval.v_string = r;
17134}
17135
17136/*
17137 * "remote_send()" function
17138 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017140f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141{
17142 rettv->v_type = VAR_STRING;
17143 rettv->vval.v_string = NULL;
17144#ifdef FEAT_CLIENTSERVER
17145 remote_common(argvars, rettv, FALSE);
17146#endif
17147}
17148
17149/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017150 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017151 */
17152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017153f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017154{
Bram Moolenaar33570922005-01-25 22:26:29 +000017155 list_T *l;
17156 listitem_T *item, *item2;
17157 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017158 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017160 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017161 dict_T *d;
17162 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017163 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017164
Bram Moolenaar8c711452005-01-14 21:53:12 +000017165 if (argvars[0].v_type == VAR_DICT)
17166 {
17167 if (argvars[2].v_type != VAR_UNKNOWN)
17168 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017169 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017170 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 key = get_tv_string_chk(&argvars[1]);
17173 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017175 di = dict_find(d, key, -1);
17176 if (di == NULL)
17177 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017178 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17179 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 {
17181 *rettv = di->di_tv;
17182 init_tv(&di->di_tv);
17183 dictitem_remove(d, di);
17184 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017185 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017186 }
17187 }
17188 else if (argvars[0].v_type != VAR_LIST)
17189 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017190 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017191 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 int error = FALSE;
17194
17195 idx = get_tv_number_chk(&argvars[1], &error);
17196 if (error)
17197 ; /* type error: do nothing, errmsg already given */
17198 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017199 EMSGN(_(e_listidx), idx);
17200 else
17201 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017202 if (argvars[2].v_type == VAR_UNKNOWN)
17203 {
17204 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017205 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017206 *rettv = item->li_tv;
17207 vim_free(item);
17208 }
17209 else
17210 {
17211 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 end = get_tv_number_chk(&argvars[2], &error);
17213 if (error)
17214 ; /* type error: do nothing */
17215 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017216 EMSGN(_(e_listidx), end);
17217 else
17218 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017219 int cnt = 0;
17220
17221 for (li = item; li != NULL; li = li->li_next)
17222 {
17223 ++cnt;
17224 if (li == item2)
17225 break;
17226 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017227 if (li == NULL) /* didn't find "item2" after "item" */
17228 EMSG(_(e_invrange));
17229 else
17230 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017231 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017232 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017233 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017234 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017235 l->lv_first = item;
17236 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017237 item->li_prev = NULL;
17238 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017239 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017240 }
17241 }
17242 }
17243 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017244 }
17245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246}
17247
17248/*
17249 * "rename({from}, {to})" function
17250 */
17251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017252f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253{
17254 char_u buf[NUMBUFLEN];
17255
17256 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17260 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261}
17262
17263/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017264 * "repeat()" function
17265 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017267f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017268{
17269 char_u *p;
17270 int n;
17271 int slen;
17272 int len;
17273 char_u *r;
17274 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017275
17276 n = get_tv_number(&argvars[1]);
17277 if (argvars[0].v_type == VAR_LIST)
17278 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017279 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017280 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017281 if (list_extend(rettv->vval.v_list,
17282 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017283 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017284 }
17285 else
17286 {
17287 p = get_tv_string(&argvars[0]);
17288 rettv->v_type = VAR_STRING;
17289 rettv->vval.v_string = NULL;
17290
17291 slen = (int)STRLEN(p);
17292 len = slen * n;
17293 if (len <= 0)
17294 return;
17295
17296 r = alloc(len + 1);
17297 if (r != NULL)
17298 {
17299 for (i = 0; i < n; i++)
17300 mch_memmove(r + i * slen, p, (size_t)slen);
17301 r[len] = NUL;
17302 }
17303
17304 rettv->vval.v_string = r;
17305 }
17306}
17307
17308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 * "resolve()" function
17310 */
17311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017312f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313{
17314 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017315#ifdef HAVE_READLINK
17316 char_u *buf = NULL;
17317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320#ifdef FEAT_SHORTCUT
17321 {
17322 char_u *v = NULL;
17323
17324 v = mch_resolve_shortcut(p);
17325 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017326 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 }
17330#else
17331# ifdef HAVE_READLINK
17332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 char_u *cpy;
17334 int len;
17335 char_u *remain = NULL;
17336 char_u *q;
17337 int is_relative_to_current = FALSE;
17338 int has_trailing_pathsep = FALSE;
17339 int limit = 100;
17340
17341 p = vim_strsave(p);
17342
17343 if (p[0] == '.' && (vim_ispathsep(p[1])
17344 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17345 is_relative_to_current = TRUE;
17346
17347 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017348 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017351 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
17354 q = getnextcomp(p);
17355 if (*q != NUL)
17356 {
17357 /* Separate the first path component in "p", and keep the
17358 * remainder (beginning with the path separator). */
17359 remain = vim_strsave(q - 1);
17360 q[-1] = NUL;
17361 }
17362
Bram Moolenaard9462e32011-04-11 21:35:11 +020017363 buf = alloc(MAXPATHL + 1);
17364 if (buf == NULL)
17365 goto fail;
17366
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 for (;;)
17368 {
17369 for (;;)
17370 {
17371 len = readlink((char *)p, (char *)buf, MAXPATHL);
17372 if (len <= 0)
17373 break;
17374 buf[len] = NUL;
17375
17376 if (limit-- == 0)
17377 {
17378 vim_free(p);
17379 vim_free(remain);
17380 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 goto fail;
17383 }
17384
17385 /* Ensure that the result will have a trailing path separator
17386 * if the argument has one. */
17387 if (remain == NULL && has_trailing_pathsep)
17388 add_pathsep(buf);
17389
17390 /* Separate the first path component in the link value and
17391 * concatenate the remainders. */
17392 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17393 if (*q != NUL)
17394 {
17395 if (remain == NULL)
17396 remain = vim_strsave(q - 1);
17397 else
17398 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017399 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 if (cpy != NULL)
17401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 vim_free(remain);
17403 remain = cpy;
17404 }
17405 }
17406 q[-1] = NUL;
17407 }
17408
17409 q = gettail(p);
17410 if (q > p && *q == NUL)
17411 {
17412 /* Ignore trailing path separator. */
17413 q[-1] = NUL;
17414 q = gettail(p);
17415 }
17416 if (q > p && !mch_isFullName(buf))
17417 {
17418 /* symlink is relative to directory of argument */
17419 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17420 if (cpy != NULL)
17421 {
17422 STRCPY(cpy, p);
17423 STRCPY(gettail(cpy), buf);
17424 vim_free(p);
17425 p = cpy;
17426 }
17427 }
17428 else
17429 {
17430 vim_free(p);
17431 p = vim_strsave(buf);
17432 }
17433 }
17434
17435 if (remain == NULL)
17436 break;
17437
17438 /* Append the first path component of "remain" to "p". */
17439 q = getnextcomp(remain + 1);
17440 len = q - remain - (*q != NUL);
17441 cpy = vim_strnsave(p, STRLEN(p) + len);
17442 if (cpy != NULL)
17443 {
17444 STRNCAT(cpy, remain, len);
17445 vim_free(p);
17446 p = cpy;
17447 }
17448 /* Shorten "remain". */
17449 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017450 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 else
17452 {
17453 vim_free(remain);
17454 remain = NULL;
17455 }
17456 }
17457
17458 /* If the result is a relative path name, make it explicitly relative to
17459 * the current directory if and only if the argument had this form. */
17460 if (!vim_ispathsep(*p))
17461 {
17462 if (is_relative_to_current
17463 && *p != NUL
17464 && !(p[0] == '.'
17465 && (p[1] == NUL
17466 || vim_ispathsep(p[1])
17467 || (p[1] == '.'
17468 && (p[2] == NUL
17469 || vim_ispathsep(p[2]))))))
17470 {
17471 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017472 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 if (cpy != NULL)
17474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 vim_free(p);
17476 p = cpy;
17477 }
17478 }
17479 else if (!is_relative_to_current)
17480 {
17481 /* Strip leading "./". */
17482 q = p;
17483 while (q[0] == '.' && vim_ispathsep(q[1]))
17484 q += 2;
17485 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017486 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 }
17488 }
17489
17490 /* Ensure that the result will have no trailing path separator
17491 * if the argument had none. But keep "/" or "//". */
17492 if (!has_trailing_pathsep)
17493 {
17494 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017495 if (after_pathsep(p, q))
17496 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 }
17498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 }
17501# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017502 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503# endif
17504#endif
17505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017506 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
17508#ifdef HAVE_READLINK
17509fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017510 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513}
17514
17515/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017516 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 */
17518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017519f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520{
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 list_T *l;
17522 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 if (argvars[0].v_type != VAR_LIST)
17525 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017526 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017527 && !tv_check_lock(l->lv_lock,
17528 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 {
17530 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017531 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017532 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017533 while (li != NULL)
17534 {
17535 ni = li->li_prev;
17536 list_append(l, li);
17537 li = ni;
17538 }
17539 rettv->vval.v_list = l;
17540 rettv->v_type = VAR_LIST;
17541 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017542 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544}
17545
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017546#define SP_NOMOVE 0x01 /* don't move cursor */
17547#define SP_REPEAT 0x02 /* repeat to find outer pair */
17548#define SP_RETCOUNT 0x04 /* return matchcount */
17549#define SP_SETPCMARK 0x08 /* set previous context mark */
17550#define SP_START 0x10 /* accept match at start position */
17551#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17552#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017553#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017554
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017555static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556
17557/*
17558 * Get flags for a search function.
17559 * Possibly sets "p_ws".
17560 * Returns BACKWARD, FORWARD or zero (for an error).
17561 */
17562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017563get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564{
17565 int dir = FORWARD;
17566 char_u *flags;
17567 char_u nbuf[NUMBUFLEN];
17568 int mask;
17569
17570 if (varp->v_type != VAR_UNKNOWN)
17571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017572 flags = get_tv_string_buf_chk(varp, nbuf);
17573 if (flags == NULL)
17574 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 while (*flags != NUL)
17576 {
17577 switch (*flags)
17578 {
17579 case 'b': dir = BACKWARD; break;
17580 case 'w': p_ws = TRUE; break;
17581 case 'W': p_ws = FALSE; break;
17582 default: mask = 0;
17583 if (flagsp != NULL)
17584 switch (*flags)
17585 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017586 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017587 case 'e': mask = SP_END; break;
17588 case 'm': mask = SP_RETCOUNT; break;
17589 case 'n': mask = SP_NOMOVE; break;
17590 case 'p': mask = SP_SUBPAT; break;
17591 case 'r': mask = SP_REPEAT; break;
17592 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017593 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594 }
17595 if (mask == 0)
17596 {
17597 EMSG2(_(e_invarg2), flags);
17598 dir = 0;
17599 }
17600 else
17601 *flagsp |= mask;
17602 }
17603 if (dir == 0)
17604 break;
17605 ++flags;
17606 }
17607 }
17608 return dir;
17609}
17610
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017612 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017615search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017617 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 char_u *pat;
17619 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017620 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 int save_p_ws = p_ws;
17622 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017623 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017624 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017625 proftime_T tm;
17626#ifdef FEAT_RELTIME
17627 long time_limit = 0;
17628#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017629 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017630 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017632 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017633 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017634 if (dir == 0)
17635 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017636 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017637 if (flags & SP_START)
17638 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017639 if (flags & SP_END)
17640 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017641 if (flags & SP_COLUMN)
17642 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017643
Bram Moolenaar76929292008-01-06 19:07:36 +000017644 /* Optional arguments: line number to stop searching and timeout. */
17645 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017646 {
17647 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17648 if (lnum_stop < 0)
17649 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017650#ifdef FEAT_RELTIME
17651 if (argvars[3].v_type != VAR_UNKNOWN)
17652 {
17653 time_limit = get_tv_number_chk(&argvars[3], NULL);
17654 if (time_limit < 0)
17655 goto theend;
17656 }
17657#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017658 }
17659
Bram Moolenaar76929292008-01-06 19:07:36 +000017660#ifdef FEAT_RELTIME
17661 /* Set the time limit, if there is one. */
17662 profile_setlimit(time_limit, &tm);
17663#endif
17664
Bram Moolenaar231334e2005-07-25 20:46:57 +000017665 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017666 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017667 * Check to make sure only those flags are set.
17668 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17669 * flags cannot be set. Check for that condition also.
17670 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017671 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017672 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017675 goto theend;
17676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017678 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017679 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017680 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017681 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017683 if (flags & SP_SUBPAT)
17684 retval = subpatnum;
17685 else
17686 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017687 if (flags & SP_SETPCMARK)
17688 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017690 if (match_pos != NULL)
17691 {
17692 /* Store the match cursor position */
17693 match_pos->lnum = pos.lnum;
17694 match_pos->col = pos.col + 1;
17695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 /* "/$" will put the cursor after the end of the line, may need to
17697 * correct that here */
17698 check_cursor();
17699 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017700
17701 /* If 'n' flag is used: restore cursor position. */
17702 if (flags & SP_NOMOVE)
17703 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017704 else
17705 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017706theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017708
17709 return retval;
17710}
17711
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017712#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017713
17714/*
17715 * round() is not in C90, use ceil() or floor() instead.
17716 */
17717 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017718vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017719{
17720 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17721}
17722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017723/*
17724 * "round({float})" function
17725 */
17726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017727f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017728{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017729 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017730
17731 rettv->v_type = VAR_FLOAT;
17732 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017733 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017734 else
17735 rettv->vval.v_float = 0.0;
17736}
17737#endif
17738
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017739/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017740 * "screenattr()" function
17741 */
17742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017743f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017744{
17745 int row;
17746 int col;
17747 int c;
17748
17749 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17750 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17751 if (row < 0 || row >= screen_Rows
17752 || col < 0 || col >= screen_Columns)
17753 c = -1;
17754 else
17755 c = ScreenAttrs[LineOffset[row] + col];
17756 rettv->vval.v_number = c;
17757}
17758
17759/*
17760 * "screenchar()" function
17761 */
17762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017763f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017764{
17765 int row;
17766 int col;
17767 int off;
17768 int c;
17769
17770 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17771 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17772 if (row < 0 || row >= screen_Rows
17773 || col < 0 || col >= screen_Columns)
17774 c = -1;
17775 else
17776 {
17777 off = LineOffset[row] + col;
17778#ifdef FEAT_MBYTE
17779 if (enc_utf8 && ScreenLinesUC[off] != 0)
17780 c = ScreenLinesUC[off];
17781 else
17782#endif
17783 c = ScreenLines[off];
17784 }
17785 rettv->vval.v_number = c;
17786}
17787
17788/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017789 * "screencol()" function
17790 *
17791 * First column is 1 to be consistent with virtcol().
17792 */
17793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017794f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017795{
17796 rettv->vval.v_number = screen_screencol() + 1;
17797}
17798
17799/*
17800 * "screenrow()" function
17801 */
17802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017803f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017804{
17805 rettv->vval.v_number = screen_screenrow() + 1;
17806}
17807
17808/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809 * "search()" function
17810 */
17811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017812f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017813{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017814 int flags = 0;
17815
17816 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817}
17818
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017820 * "searchdecl()" function
17821 */
17822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017823f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017824{
17825 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017826 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017827 int error = FALSE;
17828 char_u *name;
17829
17830 rettv->vval.v_number = 1; /* default: FAIL */
17831
17832 name = get_tv_string_chk(&argvars[0]);
17833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017834 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017835 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017836 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17837 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17838 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017839 if (!error && name != NULL)
17840 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017841 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017842}
17843
17844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017845 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017848searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849{
17850 char_u *spat, *mpat, *epat;
17851 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 int dir;
17854 int flags = 0;
17855 char_u nbuf1[NUMBUFLEN];
17856 char_u nbuf2[NUMBUFLEN];
17857 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017858 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017859 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017860 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863 spat = get_tv_string_chk(&argvars[0]);
17864 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17865 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17866 if (spat == NULL || mpat == NULL || epat == NULL)
17867 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 /* Handle the optional fourth argument: flags */
17870 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017871 if (dir == 0)
17872 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017873
17874 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017875 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17876 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017877 if ((flags & (SP_END | SP_SUBPAT)) != 0
17878 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017879 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017880 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017881 goto theend;
17882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017884 /* Using 'r' implies 'W', otherwise it doesn't work. */
17885 if (flags & SP_REPEAT)
17886 p_ws = FALSE;
17887
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017888 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017889 if (argvars[3].v_type == VAR_UNKNOWN
17890 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 skip = (char_u *)"";
17892 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017894 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017895 if (argvars[5].v_type != VAR_UNKNOWN)
17896 {
17897 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17898 if (lnum_stop < 0)
17899 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017900#ifdef FEAT_RELTIME
17901 if (argvars[6].v_type != VAR_UNKNOWN)
17902 {
17903 time_limit = get_tv_number_chk(&argvars[6], NULL);
17904 if (time_limit < 0)
17905 goto theend;
17906 }
17907#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017908 }
17909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017910 if (skip == NULL)
17911 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017913 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017914 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017915
17916theend:
17917 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017918
17919 return retval;
17920}
17921
17922/*
17923 * "searchpair()" function
17924 */
17925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017926f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017927{
17928 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17929}
17930
17931/*
17932 * "searchpairpos()" function
17933 */
17934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017935f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017936{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017937 pos_T match_pos;
17938 int lnum = 0;
17939 int col = 0;
17940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017941 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017942 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017943
17944 if (searchpair_cmn(argvars, &match_pos) > 0)
17945 {
17946 lnum = match_pos.lnum;
17947 col = match_pos.col;
17948 }
17949
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017950 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17951 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017952}
17953
17954/*
17955 * Search for a start/middle/end thing.
17956 * Used by searchpair(), see its documentation for the details.
17957 * Returns 0 or -1 for no match,
17958 */
17959 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017960do_searchpair(
17961 char_u *spat, /* start pattern */
17962 char_u *mpat, /* middle pattern */
17963 char_u *epat, /* end pattern */
17964 int dir, /* BACKWARD or FORWARD */
17965 char_u *skip, /* skip expression */
17966 int flags, /* SP_SETPCMARK and other SP_ values */
17967 pos_T *match_pos,
17968 linenr_T lnum_stop, /* stop at this line if not zero */
17969 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017970{
17971 char_u *save_cpo;
17972 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17973 long retval = 0;
17974 pos_T pos;
17975 pos_T firstpos;
17976 pos_T foundpos;
17977 pos_T save_cursor;
17978 pos_T save_pos;
17979 int n;
17980 int r;
17981 int nest = 1;
17982 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017983 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017984 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017985
17986 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17987 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017988 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017989
Bram Moolenaar76929292008-01-06 19:07:36 +000017990#ifdef FEAT_RELTIME
17991 /* Set the time limit, if there is one. */
17992 profile_setlimit(time_limit, &tm);
17993#endif
17994
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017995 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17996 * start/middle/end (pat3, for the top pair). */
17997 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17998 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17999 if (pat2 == NULL || pat3 == NULL)
18000 goto theend;
18001 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18002 if (*mpat == NUL)
18003 STRCPY(pat3, pat2);
18004 else
18005 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18006 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018007 if (flags & SP_START)
18008 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018009
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 save_cursor = curwin->w_cursor;
18011 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018012 clearpos(&firstpos);
18013 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 pat = pat3;
18015 for (;;)
18016 {
18017 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018018 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18020 /* didn't find it or found the first match again: FAIL */
18021 break;
18022
18023 if (firstpos.lnum == 0)
18024 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018025 if (equalpos(pos, foundpos))
18026 {
18027 /* Found the same position again. Can happen with a pattern that
18028 * has "\zs" at the end and searching backwards. Advance one
18029 * character and try again. */
18030 if (dir == BACKWARD)
18031 decl(&pos);
18032 else
18033 incl(&pos);
18034 }
18035 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018037 /* clear the start flag to avoid getting stuck here */
18038 options &= ~SEARCH_START;
18039
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 /* If the skip pattern matches, ignore this match. */
18041 if (*skip != NUL)
18042 {
18043 save_pos = curwin->w_cursor;
18044 curwin->w_cursor = pos;
18045 r = eval_to_bool(skip, &err, NULL, FALSE);
18046 curwin->w_cursor = save_pos;
18047 if (err)
18048 {
18049 /* Evaluating {skip} caused an error, break here. */
18050 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018051 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 break;
18053 }
18054 if (r)
18055 continue;
18056 }
18057
18058 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18059 {
18060 /* Found end when searching backwards or start when searching
18061 * forward: nested pair. */
18062 ++nest;
18063 pat = pat2; /* nested, don't search for middle */
18064 }
18065 else
18066 {
18067 /* Found end when searching forward or start when searching
18068 * backward: end of (nested) pair; or found middle in outer pair. */
18069 if (--nest == 1)
18070 pat = pat3; /* outer level, search for middle */
18071 }
18072
18073 if (nest == 0)
18074 {
18075 /* Found the match: return matchcount or line number. */
18076 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018077 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018079 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018080 if (flags & SP_SETPCMARK)
18081 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 curwin->w_cursor = pos;
18083 if (!(flags & SP_REPEAT))
18084 break;
18085 nest = 1; /* search for next unmatched */
18086 }
18087 }
18088
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018089 if (match_pos != NULL)
18090 {
18091 /* Store the match cursor position */
18092 match_pos->lnum = curwin->w_cursor.lnum;
18093 match_pos->col = curwin->w_cursor.col + 1;
18094 }
18095
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018097 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 curwin->w_cursor = save_cursor;
18099
18100theend:
18101 vim_free(pat2);
18102 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018103 if (p_cpo == empty_option)
18104 p_cpo = save_cpo;
18105 else
18106 /* Darn, evaluating the {skip} expression changed the value. */
18107 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018108
18109 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018112/*
18113 * "searchpos()" function
18114 */
18115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018116f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018117{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018118 pos_T match_pos;
18119 int lnum = 0;
18120 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018121 int n;
18122 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018125 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018126
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018127 n = search_cmn(argvars, &match_pos, &flags);
18128 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018129 {
18130 lnum = match_pos.lnum;
18131 col = match_pos.col;
18132 }
18133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018134 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18135 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018136 if (flags & SP_SUBPAT)
18137 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018138}
18139
Bram Moolenaar0d660222005-01-07 21:51:51 +000018140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018141f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018143#ifdef FEAT_CLIENTSERVER
18144 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018145 char_u *server = get_tv_string_chk(&argvars[0]);
18146 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147
Bram Moolenaar0d660222005-01-07 21:51:51 +000018148 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018149 if (server == NULL || reply == NULL)
18150 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 if (check_restricted() || check_secure())
18152 return;
18153# ifdef FEAT_X11
18154 if (check_connection() == FAIL)
18155 return;
18156# endif
18157
18158 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018160 EMSG(_("E258: Unable to send to client"));
18161 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018163 rettv->vval.v_number = 0;
18164#else
18165 rettv->vval.v_number = -1;
18166#endif
18167}
18168
Bram Moolenaar0d660222005-01-07 21:51:51 +000018169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018170f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018171{
18172 char_u *r = NULL;
18173
18174#ifdef FEAT_CLIENTSERVER
18175# ifdef WIN32
18176 r = serverGetVimNames();
18177# else
18178 make_connection();
18179 if (X_DISPLAY != NULL)
18180 r = serverGetVimNames(X_DISPLAY);
18181# endif
18182#endif
18183 rettv->v_type = VAR_STRING;
18184 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185}
18186
18187/*
18188 * "setbufvar()" function
18189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018191f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192{
18193 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 char_u nbuf[NUMBUFLEN];
18198
18199 if (check_restricted() || check_secure())
18200 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18202 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018203 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 varp = &argvars[2];
18205
18206 if (buf != NULL && varname != NULL && varp != NULL)
18207 {
18208 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210
18211 if (*varname == '&')
18212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018213 long numval;
18214 char_u *strval;
18215 int error = FALSE;
18216
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018218 numval = get_tv_number_chk(varp, &error);
18219 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018220 if (!error && strval != NULL)
18221 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 }
18223 else
18224 {
18225 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18226 if (bufvarname != NULL)
18227 {
18228 STRCPY(bufvarname, "b:");
18229 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018230 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 vim_free(bufvarname);
18232 }
18233 }
18234
18235 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238}
18239
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018241f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018242{
18243 dict_T *d;
18244 dictitem_T *di;
18245 char_u *csearch;
18246
18247 if (argvars[0].v_type != VAR_DICT)
18248 {
18249 EMSG(_(e_dictreq));
18250 return;
18251 }
18252
18253 if ((d = argvars[0].vval.v_dict) != NULL)
18254 {
18255 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18256 if (csearch != NULL)
18257 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018258#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018259 if (enc_utf8)
18260 {
18261 int pcc[MAX_MCO];
18262 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018263
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018264 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18265 }
18266 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018267#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018268 set_last_csearch(PTR2CHAR(csearch),
18269 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018270 }
18271
18272 di = dict_find(d, (char_u *)"forward", -1);
18273 if (di != NULL)
18274 set_csearch_direction(get_tv_number(&di->di_tv)
18275 ? FORWARD : BACKWARD);
18276
18277 di = dict_find(d, (char_u *)"until", -1);
18278 if (di != NULL)
18279 set_csearch_until(!!get_tv_number(&di->di_tv));
18280 }
18281}
18282
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283/*
18284 * "setcmdpos()" function
18285 */
18286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018287f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018289 int pos = (int)get_tv_number(&argvars[0]) - 1;
18290
18291 if (pos >= 0)
18292 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293}
18294
18295/*
18296 * "setline()" function
18297 */
18298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018299f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300{
18301 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018302 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018303 list_T *l = NULL;
18304 listitem_T *li = NULL;
18305 long added = 0;
18306 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018308 lnum = get_tv_lnum(&argvars[0]);
18309 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018311 l = argvars[1].vval.v_list;
18312 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018314 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018315 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018316
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018317 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018318 for (;;)
18319 {
18320 if (l != NULL)
18321 {
18322 /* list argument, get next string */
18323 if (li == NULL)
18324 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018326 li = li->li_next;
18327 }
18328
18329 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018330 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018331 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018332
18333 /* When coming here from Insert mode, sync undo, so that this can be
18334 * undone separately from what was previously inserted. */
18335 if (u_sync_once == 2)
18336 {
18337 u_sync_once = 1; /* notify that u_sync() was called */
18338 u_sync(TRUE);
18339 }
18340
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018341 if (lnum <= curbuf->b_ml.ml_line_count)
18342 {
18343 /* existing line, replace it */
18344 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18345 {
18346 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018347 if (lnum == curwin->w_cursor.lnum)
18348 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018349 rettv->vval.v_number = 0; /* OK */
18350 }
18351 }
18352 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18353 {
18354 /* lnum is one past the last line, append the line */
18355 ++added;
18356 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18357 rettv->vval.v_number = 0; /* OK */
18358 }
18359
18360 if (l == NULL) /* only one string argument */
18361 break;
18362 ++lnum;
18363 }
18364
18365 if (added > 0)
18366 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367}
18368
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018369static 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 +000018370
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018372 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018373 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018375set_qf_ll_list(
18376 win_T *wp UNUSED,
18377 typval_T *list_arg UNUSED,
18378 typval_T *action_arg UNUSED,
18379 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018380{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018381#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018382 char_u *act;
18383 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018384#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018385
Bram Moolenaar2641f772005-03-25 21:58:17 +000018386 rettv->vval.v_number = -1;
18387
18388#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018389 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018390 EMSG(_(e_listreq));
18391 else
18392 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018393 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018394
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018395 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018396 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018397 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018398 if (act == NULL)
18399 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018400 if (*act == 'a' || *act == 'r')
18401 action = *act;
18402 }
18403
Bram Moolenaar81484f42012-12-05 15:16:47 +010018404 if (l != NULL && set_errorlist(wp, l, action,
18405 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018406 rettv->vval.v_number = 0;
18407 }
18408#endif
18409}
18410
18411/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018412 * "setloclist()" function
18413 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018415f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018416{
18417 win_T *win;
18418
18419 rettv->vval.v_number = -1;
18420
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018421 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018422 if (win != NULL)
18423 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18424}
18425
18426/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018427 * "setmatches()" function
18428 */
18429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018430f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018431{
18432#ifdef FEAT_SEARCH_EXTRA
18433 list_T *l;
18434 listitem_T *li;
18435 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018436 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018437
18438 rettv->vval.v_number = -1;
18439 if (argvars[0].v_type != VAR_LIST)
18440 {
18441 EMSG(_(e_listreq));
18442 return;
18443 }
18444 if ((l = argvars[0].vval.v_list) != NULL)
18445 {
18446
18447 /* To some extent make sure that we are dealing with a list from
18448 * "getmatches()". */
18449 li = l->lv_first;
18450 while (li != NULL)
18451 {
18452 if (li->li_tv.v_type != VAR_DICT
18453 || (d = li->li_tv.vval.v_dict) == NULL)
18454 {
18455 EMSG(_(e_invarg));
18456 return;
18457 }
18458 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018459 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18460 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018461 && dict_find(d, (char_u *)"priority", -1) != NULL
18462 && dict_find(d, (char_u *)"id", -1) != NULL))
18463 {
18464 EMSG(_(e_invarg));
18465 return;
18466 }
18467 li = li->li_next;
18468 }
18469
18470 clear_matches(curwin);
18471 li = l->lv_first;
18472 while (li != NULL)
18473 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018474 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018475 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018476 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018477 char_u *group;
18478 int priority;
18479 int id;
18480 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018481
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018482 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018483 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18484 {
18485 if (s == NULL)
18486 {
18487 s = list_alloc();
18488 if (s == NULL)
18489 return;
18490 }
18491
18492 /* match from matchaddpos() */
18493 for (i = 1; i < 9; i++)
18494 {
18495 sprintf((char *)buf, (char *)"pos%d", i);
18496 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18497 {
18498 if (di->di_tv.v_type != VAR_LIST)
18499 return;
18500
18501 list_append_tv(s, &di->di_tv);
18502 s->lv_refcount++;
18503 }
18504 else
18505 break;
18506 }
18507 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018508
18509 group = get_dict_string(d, (char_u *)"group", FALSE);
18510 priority = (int)get_dict_number(d, (char_u *)"priority");
18511 id = (int)get_dict_number(d, (char_u *)"id");
18512 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18513 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18514 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018515 if (i == 0)
18516 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018517 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018518 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018519 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018520 }
18521 else
18522 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018523 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018524 list_unref(s);
18525 s = NULL;
18526 }
18527
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018528 li = li->li_next;
18529 }
18530 rettv->vval.v_number = 0;
18531 }
18532#endif
18533}
18534
18535/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018536 * "setpos()" function
18537 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018539f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018540{
18541 pos_T pos;
18542 int fnum;
18543 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018544 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018545
Bram Moolenaar08250432008-02-13 11:42:46 +000018546 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018547 name = get_tv_string_chk(argvars);
18548 if (name != NULL)
18549 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018550 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018551 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018552 if (--pos.col < 0)
18553 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018554 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018555 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018556 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018557 if (fnum == curbuf->b_fnum)
18558 {
18559 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018560 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018561 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018562 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018563 curwin->w_set_curswant = FALSE;
18564 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018565 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018566 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018567 }
18568 else
18569 EMSG(_(e_invarg));
18570 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018571 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18572 {
18573 /* set mark */
18574 if (setmark_pos(name[1], &pos, fnum) == OK)
18575 rettv->vval.v_number = 0;
18576 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018577 else
18578 EMSG(_(e_invarg));
18579 }
18580 }
18581}
18582
18583/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018584 * "setqflist()" function
18585 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018587f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018588{
18589 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18590}
18591
18592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 * "setreg()" function
18594 */
18595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018596f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597{
18598 int regname;
18599 char_u *strregname;
18600 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018601 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 int append;
18603 char_u yank_type;
18604 long block_len;
18605
18606 block_len = -1;
18607 yank_type = MAUTO;
18608 append = FALSE;
18609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018610 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613 if (strregname == NULL)
18614 return; /* type error; errmsg already given */
18615 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 if (regname == 0 || regname == '@')
18617 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018621 stropt = get_tv_string_chk(&argvars[2]);
18622 if (stropt == NULL)
18623 return; /* type error */
18624 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 switch (*stropt)
18626 {
18627 case 'a': case 'A': /* append */
18628 append = TRUE;
18629 break;
18630 case 'v': case 'c': /* character-wise selection */
18631 yank_type = MCHAR;
18632 break;
18633 case 'V': case 'l': /* line-wise selection */
18634 yank_type = MLINE;
18635 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 case 'b': case Ctrl_V: /* block-wise selection */
18637 yank_type = MBLOCK;
18638 if (VIM_ISDIGIT(stropt[1]))
18639 {
18640 ++stropt;
18641 block_len = getdigits(&stropt) - 1;
18642 --stropt;
18643 }
18644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 }
18646 }
18647
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018648 if (argvars[1].v_type == VAR_LIST)
18649 {
18650 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018651 char_u **allocval;
18652 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018653 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018654 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018655 int len = argvars[1].vval.v_list->lv_len;
18656 listitem_T *li;
18657
Bram Moolenaar7d647822014-04-05 21:28:56 +020018658 /* First half: use for pointers to result lines; second half: use for
18659 * pointers to allocated copies. */
18660 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018661 if (lstval == NULL)
18662 return;
18663 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018664 allocval = lstval + len + 2;
18665 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018666
18667 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18668 li = li->li_next)
18669 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018670 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018671 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018672 goto free_lstval;
18673 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018674 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018675 /* Need to make a copy, next get_tv_string_buf_chk() will
18676 * overwrite the string. */
18677 strval = vim_strsave(buf);
18678 if (strval == NULL)
18679 goto free_lstval;
18680 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018681 }
18682 *curval++ = strval;
18683 }
18684 *curval++ = NULL;
18685
18686 write_reg_contents_lst(regname, lstval, -1,
18687 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018688free_lstval:
18689 while (curallocval > allocval)
18690 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018691 vim_free(lstval);
18692 }
18693 else
18694 {
18695 strval = get_tv_string_chk(&argvars[1]);
18696 if (strval == NULL)
18697 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018698 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702}
18703
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018704/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018705 * "settabvar()" function
18706 */
18707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018708f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018709{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018710#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018711 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018712 tabpage_T *tp;
18713#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018714 char_u *varname, *tabvarname;
18715 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018716
18717 rettv->vval.v_number = 0;
18718
18719 if (check_restricted() || check_secure())
18720 return;
18721
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018722#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018723 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018724#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018725 varname = get_tv_string_chk(&argvars[1]);
18726 varp = &argvars[2];
18727
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018728 if (varname != NULL && varp != NULL
18729#ifdef FEAT_WINDOWS
18730 && tp != NULL
18731#endif
18732 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018733 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018734#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018735 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018736 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018737#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018738
18739 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18740 if (tabvarname != NULL)
18741 {
18742 STRCPY(tabvarname, "t:");
18743 STRCPY(tabvarname + 2, varname);
18744 set_var(tabvarname, varp, TRUE);
18745 vim_free(tabvarname);
18746 }
18747
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018748#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018749 /* Restore current tabpage */
18750 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018751 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018752#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018753 }
18754}
18755
18756/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018757 * "settabwinvar()" function
18758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018761{
18762 setwinvar(argvars, rettv, 1);
18763}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764
18765/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018766 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018769f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018771 setwinvar(argvars, rettv, 0);
18772}
18773
18774/*
18775 * "setwinvar()" and "settabwinvar()" functions
18776 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018777
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018779setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018780{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 win_T *win;
18782#ifdef FEAT_WINDOWS
18783 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018784 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018785 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786#endif
18787 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018788 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018790 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791
18792 if (check_restricted() || check_secure())
18793 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018794
18795#ifdef FEAT_WINDOWS
18796 if (off == 1)
18797 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18798 else
18799 tp = curtab;
18800#endif
18801 win = find_win_by_nr(&argvars[off], tp);
18802 varname = get_tv_string_chk(&argvars[off + 1]);
18803 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804
18805 if (win != NULL && varname != NULL && varp != NULL)
18806 {
18807#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018808 need_switch_win = !(tp == curtab && win == curwin);
18809 if (!need_switch_win
18810 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018813 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018815 long numval;
18816 char_u *strval;
18817 int error = FALSE;
18818
18819 ++varname;
18820 numval = get_tv_number_chk(varp, &error);
18821 strval = get_tv_string_buf_chk(varp, nbuf);
18822 if (!error && strval != NULL)
18823 set_option_value(varname, numval, strval, OPT_LOCAL);
18824 }
18825 else
18826 {
18827 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18828 if (winvarname != NULL)
18829 {
18830 STRCPY(winvarname, "w:");
18831 STRCPY(winvarname + 2, varname);
18832 set_var(winvarname, varp, TRUE);
18833 vim_free(winvarname);
18834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 }
18836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018838 if (need_switch_win)
18839 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840#endif
18841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842}
18843
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018844#ifdef FEAT_CRYPT
18845/*
18846 * "sha256({string})" function
18847 */
18848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018849f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018850{
18851 char_u *p;
18852
18853 p = get_tv_string(&argvars[0]);
18854 rettv->vval.v_string = vim_strsave(
18855 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18856 rettv->v_type = VAR_STRING;
18857}
18858#endif /* FEAT_CRYPT */
18859
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018861 * "shellescape({string})" function
18862 */
18863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018864f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018865{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018866 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018867 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018868 rettv->v_type = VAR_STRING;
18869}
18870
18871/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018872 * shiftwidth() function
18873 */
18874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018875f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018876{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018877 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018878}
18879
18880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018881 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 */
18883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018884f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888 p = get_tv_string(&argvars[0]);
18889 rettv->vval.v_string = vim_strsave(p);
18890 simplify_filename(rettv->vval.v_string); /* simplify in place */
18891 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892}
18893
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018894#ifdef FEAT_FLOAT
18895/*
18896 * "sin()" function
18897 */
18898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018899f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018900{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018901 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018902
18903 rettv->v_type = VAR_FLOAT;
18904 if (get_float_arg(argvars, &f) == OK)
18905 rettv->vval.v_float = sin(f);
18906 else
18907 rettv->vval.v_float = 0.0;
18908}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018909
18910/*
18911 * "sinh()" function
18912 */
18913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018914f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018915{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018916 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018917
18918 rettv->v_type = VAR_FLOAT;
18919 if (get_float_arg(argvars, &f) == OK)
18920 rettv->vval.v_float = sinh(f);
18921 else
18922 rettv->vval.v_float = 0.0;
18923}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018924#endif
18925
Bram Moolenaar0d660222005-01-07 21:51:51 +000018926static int
18927#ifdef __BORLANDC__
18928 _RTLENTRYF
18929#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018930 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018931static int
18932#ifdef __BORLANDC__
18933 _RTLENTRYF
18934#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018935 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018937/* struct used in the array that's given to qsort() */
18938typedef struct
18939{
18940 listitem_T *item;
18941 int idx;
18942} sortItem_T;
18943
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944/* struct storing information about current sort */
18945typedef struct
18946{
18947 int item_compare_ic;
18948 int item_compare_numeric;
18949 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018950#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018951 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018952#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 char_u *item_compare_func;
18954 dict_T *item_compare_selfdict;
18955 int item_compare_func_err;
18956 int item_compare_keep_zero;
18957} sortinfo_T;
18958static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018959static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960#define ITEM_COMPARE_FAIL 999
18961
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018963 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 static int
18966#ifdef __BORLANDC__
18967_RTLENTRYF
18968#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018969item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018971 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018972 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018974 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 int res;
18976 char_u numbuf1[NUMBUFLEN];
18977 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018979 si1 = (sortItem_T *)s1;
18980 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018981 tv1 = &si1->item->li_tv;
18982 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018983
Bram Moolenaar0b962472016-02-22 22:51:33 +010018984 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018985 {
18986 long v1 = get_tv_number(tv1);
18987 long v2 = get_tv_number(tv2);
18988
18989 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18990 }
18991
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018992#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018993 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018994 {
18995 float_T v1 = get_tv_float(tv1);
18996 float_T v2 = get_tv_float(tv2);
18997
18998 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18999 }
19000#endif
19001
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019002 /* tv2string() puts quotes around a string and allocates memory. Don't do
19003 * that for string variables. Use a single quote when comparing with a
19004 * non-string to do what the docs promise. */
19005 if (tv1->v_type == VAR_STRING)
19006 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019007 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019008 p1 = (char_u *)"'";
19009 else
19010 p1 = tv1->vval.v_string;
19011 }
19012 else
19013 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19014 if (tv2->v_type == VAR_STRING)
19015 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019017 p2 = (char_u *)"'";
19018 else
19019 p2 = tv2->vval.v_string;
19020 }
19021 else
19022 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019023 if (p1 == NULL)
19024 p1 = (char_u *)"";
19025 if (p2 == NULL)
19026 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019028 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019029 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019030 res = STRICMP(p1, p2);
19031 else
19032 res = STRCMP(p1, p2);
19033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019035 {
19036 double n1, n2;
19037 n1 = strtod((char *)p1, (char **)&p1);
19038 n2 = strtod((char *)p2, (char **)&p2);
19039 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19040 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019041
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019042 /* When the result would be zero, compare the item indexes. Makes the
19043 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019045 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019046
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 vim_free(tofree1);
19048 vim_free(tofree2);
19049 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051
19052 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053#ifdef __BORLANDC__
19054_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019056item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019058 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019059 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019061 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019064 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019066 return 0;
19067
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019068 si1 = (sortItem_T *)s1;
19069 si2 = (sortItem_T *)s2;
19070
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019071 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019072 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019073 copy_tv(&si1->item->li_tv, &argv[0]);
19074 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075
19076 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019078 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019079 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019080 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 clear_tv(&argv[0]);
19082 clear_tv(&argv[1]);
19083
19084 if (res == FAIL)
19085 res = ITEM_COMPARE_FAIL;
19086 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019087 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19088 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019089 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019090 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019091
19092 /* When the result would be zero, compare the pointers themselves. Makes
19093 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019095 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019096
Bram Moolenaar0d660222005-01-07 21:51:51 +000019097 return res;
19098}
19099
19100/*
19101 * "sort({list})" function
19102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019104do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105{
Bram Moolenaar33570922005-01-25 22:26:29 +000019106 list_T *l;
19107 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019108 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 sortinfo_T *old_sortinfo;
19110 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111 long len;
19112 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 /* Pointer to current info struct used in compare function. Save and
19115 * restore the current one for nested calls. */
19116 old_sortinfo = sortinfo;
19117 sortinfo = &info;
19118
Bram Moolenaar0d660222005-01-07 21:51:51 +000019119 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019120 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 else
19122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019124 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019125 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19126 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 rettv->vval.v_list = l;
19129 rettv->v_type = VAR_LIST;
19130 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132 len = list_len(l);
19133 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 info.item_compare_ic = FALSE;
19137 info.item_compare_numeric = FALSE;
19138 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019139#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019141#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 info.item_compare_func = NULL;
19143 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144 if (argvars[1].v_type != VAR_UNKNOWN)
19145 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019146 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019147 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019149 else
19150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019151 int error = FALSE;
19152
19153 i = get_tv_number_chk(&argvars[1], &error);
19154 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019155 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019157 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019158 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019159 info.item_compare_func = get_tv_string(&argvars[1]);
19160 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019161 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019163 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164 info.item_compare_func = NULL;
19165 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019166 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019168 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 info.item_compare_func = NULL;
19170 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019171 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019172#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019173 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019174 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 info.item_compare_func = NULL;
19176 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019177 }
19178#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019180 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 info.item_compare_func = NULL;
19182 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019183 }
19184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019186
19187 if (argvars[2].v_type != VAR_UNKNOWN)
19188 {
19189 /* optional third argument: {dict} */
19190 if (argvars[2].v_type != VAR_DICT)
19191 {
19192 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019194 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019195 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019196 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198
Bram Moolenaar0d660222005-01-07 21:51:51 +000019199 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019200 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019201 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019202 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
Bram Moolenaar327aa022014-03-25 18:24:23 +010019204 i = 0;
19205 if (sort)
19206 {
19207 /* sort(): ptrs will be the list to sort */
19208 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019209 {
19210 ptrs[i].item = li;
19211 ptrs[i].idx = i;
19212 ++i;
19213 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 info.item_compare_func_err = FALSE;
19216 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019217 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019219 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 EMSG(_("E702: Sort compare function failed"));
19222 else
19223 {
19224 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019225 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019226 info.item_compare_func == NULL
19227 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019228
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019230 {
19231 /* Clear the List and append the items in sorted order. */
19232 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19233 l->lv_len = 0;
19234 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019235 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019236 }
19237 }
19238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019241 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019242
19243 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 info.item_compare_func_err = FALSE;
19245 info.item_compare_keep_zero = TRUE;
19246 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247 ? item_compare2 : item_compare;
19248
19249 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19250 li = li->li_next)
19251 {
19252 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19253 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019254 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256 {
19257 EMSG(_("E882: Uniq compare function failed"));
19258 break;
19259 }
19260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019261
Bram Moolenaar0b962472016-02-22 22:51:33 +010019262 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019263 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019264 while (--i >= 0)
19265 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019266 li = ptrs[i].item->li_next;
19267 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019268 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019269 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019270 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019271 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019272 list_fix_watch(l, li);
19273 listitem_free(li);
19274 l->lv_len--;
19275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277 }
19278
19279 vim_free(ptrs);
19280 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019281theend:
19282 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019283}
19284
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019285/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019286 * "sort({list})" function
19287 */
19288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019289f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019290{
19291 do_sort_uniq(argvars, rettv, TRUE);
19292}
19293
19294/*
19295 * "uniq({list})" function
19296 */
19297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019298f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019299{
19300 do_sort_uniq(argvars, rettv, FALSE);
19301}
19302
19303/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019304 * "soundfold({word})" function
19305 */
19306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019307f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019308{
19309 char_u *s;
19310
19311 rettv->v_type = VAR_STRING;
19312 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019313#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019314 rettv->vval.v_string = eval_soundfold(s);
19315#else
19316 rettv->vval.v_string = vim_strsave(s);
19317#endif
19318}
19319
19320/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019321 * "spellbadword()" function
19322 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019324f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019326 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019327 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019328 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019330 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019331 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019332
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019333#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019334 if (argvars[0].v_type == VAR_UNKNOWN)
19335 {
19336 /* Find the start and length of the badly spelled word. */
19337 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19338 if (len != 0)
19339 word = ml_get_cursor();
19340 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019341 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019342 {
19343 char_u *str = get_tv_string_chk(&argvars[0]);
19344 int capcol = -1;
19345
19346 if (str != NULL)
19347 {
19348 /* Check the argument for spelling. */
19349 while (*str != NUL)
19350 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019351 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019352 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019353 {
19354 word = str;
19355 break;
19356 }
19357 str += len;
19358 }
19359 }
19360 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019361#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019363 list_append_string(rettv->vval.v_list, word, len);
19364 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019365 attr == HLF_SPB ? "bad" :
19366 attr == HLF_SPR ? "rare" :
19367 attr == HLF_SPL ? "local" :
19368 attr == HLF_SPC ? "caps" :
19369 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019370}
19371
19372/*
19373 * "spellsuggest()" function
19374 */
19375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019376f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019377{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019378#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019379 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019380 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019381 int maxcount;
19382 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019383 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019384 listitem_T *li;
19385 int need_capital = FALSE;
19386#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019389 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019390
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019391#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019392 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019393 {
19394 str = get_tv_string(&argvars[0]);
19395 if (argvars[1].v_type != VAR_UNKNOWN)
19396 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019397 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019398 if (maxcount <= 0)
19399 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019400 if (argvars[2].v_type != VAR_UNKNOWN)
19401 {
19402 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19403 if (typeerr)
19404 return;
19405 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019406 }
19407 else
19408 maxcount = 25;
19409
Bram Moolenaar4770d092006-01-12 23:22:24 +000019410 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019411
19412 for (i = 0; i < ga.ga_len; ++i)
19413 {
19414 str = ((char_u **)ga.ga_data)[i];
19415
19416 li = listitem_alloc();
19417 if (li == NULL)
19418 vim_free(str);
19419 else
19420 {
19421 li->li_tv.v_type = VAR_STRING;
19422 li->li_tv.v_lock = 0;
19423 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019424 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019425 }
19426 }
19427 ga_clear(&ga);
19428 }
19429#endif
19430}
19431
Bram Moolenaar0d660222005-01-07 21:51:51 +000019432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019433f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434{
19435 char_u *str;
19436 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019437 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019438 regmatch_T regmatch;
19439 char_u patbuf[NUMBUFLEN];
19440 char_u *save_cpo;
19441 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019442 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019443 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019444 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019445
19446 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19447 save_cpo = p_cpo;
19448 p_cpo = (char_u *)"";
19449
19450 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019451 if (argvars[1].v_type != VAR_UNKNOWN)
19452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19454 if (pat == NULL)
19455 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019458 }
19459 if (pat == NULL || *pat == NUL)
19460 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019462 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019464 if (typeerr)
19465 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466
Bram Moolenaar0d660222005-01-07 21:51:51 +000019467 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19468 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019470 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019471 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019473 if (*str == NUL)
19474 match = FALSE; /* empty item at the end */
19475 else
19476 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477 if (match)
19478 end = regmatch.startp[0];
19479 else
19480 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019481 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19482 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019483 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019484 if (list_append_string(rettv->vval.v_list, str,
19485 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019486 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019487 }
19488 if (!match)
19489 break;
19490 /* Advance to just after the match. */
19491 if (regmatch.endp[0] > str)
19492 col = 0;
19493 else
19494 {
19495 /* Don't get stuck at the same match. */
19496#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019497 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019498#else
19499 col = 1;
19500#endif
19501 }
19502 str = regmatch.endp[0];
19503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504
Bram Moolenaar473de612013-06-08 18:19:48 +020019505 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509}
19510
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019511#ifdef FEAT_FLOAT
19512/*
19513 * "sqrt()" function
19514 */
19515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019516f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019517{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019518 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019519
19520 rettv->v_type = VAR_FLOAT;
19521 if (get_float_arg(argvars, &f) == OK)
19522 rettv->vval.v_float = sqrt(f);
19523 else
19524 rettv->vval.v_float = 0.0;
19525}
19526
19527/*
19528 * "str2float()" function
19529 */
19530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019531f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019532{
19533 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19534
19535 if (*p == '+')
19536 p = skipwhite(p + 1);
19537 (void)string2float(p, &rettv->vval.v_float);
19538 rettv->v_type = VAR_FLOAT;
19539}
19540#endif
19541
Bram Moolenaar2c932302006-03-18 21:42:09 +000019542/*
19543 * "str2nr()" function
19544 */
19545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019547{
19548 int base = 10;
19549 char_u *p;
19550 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019551 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019552
19553 if (argvars[1].v_type != VAR_UNKNOWN)
19554 {
19555 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019556 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019557 {
19558 EMSG(_(e_invarg));
19559 return;
19560 }
19561 }
19562
19563 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019564 if (*p == '+')
19565 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019566 switch (base)
19567 {
19568 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19569 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19570 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19571 default: what = 0;
19572 }
19573 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019574 rettv->vval.v_number = n;
19575}
19576
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577#ifdef HAVE_STRFTIME
19578/*
19579 * "strftime({format}[, {time}])" function
19580 */
19581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019582f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583{
19584 char_u result_buf[256];
19585 struct tm *curtime;
19586 time_t seconds;
19587 char_u *p;
19588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019589 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019592 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 seconds = time(NULL);
19594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 curtime = localtime(&seconds);
19597 /* MSVC returns NULL for an invalid value of seconds. */
19598 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 else
19601 {
19602# ifdef FEAT_MBYTE
19603 vimconv_T conv;
19604 char_u *enc;
19605
19606 conv.vc_type = CONV_NONE;
19607 enc = enc_locale();
19608 convert_setup(&conv, p_enc, enc);
19609 if (conv.vc_type != CONV_NONE)
19610 p = string_convert(&conv, p, NULL);
19611# endif
19612 if (p != NULL)
19613 (void)strftime((char *)result_buf, sizeof(result_buf),
19614 (char *)p, curtime);
19615 else
19616 result_buf[0] = NUL;
19617
19618# ifdef FEAT_MBYTE
19619 if (conv.vc_type != CONV_NONE)
19620 vim_free(p);
19621 convert_setup(&conv, enc, p_enc);
19622 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019623 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 else
19625# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019626 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627
19628# ifdef FEAT_MBYTE
19629 /* Release conversion descriptors */
19630 convert_setup(&conv, NULL, NULL);
19631 vim_free(enc);
19632# endif
19633 }
19634}
19635#endif
19636
19637/*
19638 * "stridx()" function
19639 */
19640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019641f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642{
19643 char_u buf[NUMBUFLEN];
19644 char_u *needle;
19645 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 needle = get_tv_string_chk(&argvars[1]);
19651 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019653 if (needle == NULL || haystack == NULL)
19654 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 if (argvars[2].v_type != VAR_UNKNOWN)
19657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019658 int error = FALSE;
19659
19660 start_idx = get_tv_number_chk(&argvars[2], &error);
19661 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019663 if (start_idx >= 0)
19664 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 }
19666
19667 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19668 if (pos != NULL)
19669 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670}
19671
19672/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673 * "string()" function
19674 */
19675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019676f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019677{
19678 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019679 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019681 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019682 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019683 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019684 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019685 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686}
19687
19688/*
19689 * "strlen()" function
19690 */
19691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 rettv->vval.v_number = (varnumber_T)(STRLEN(
19695 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696}
19697
19698/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019699 * "strchars()" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019703{
19704 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019705 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019706#ifdef FEAT_MBYTE
19707 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019708 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019709#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019710
19711 if (argvars[1].v_type != VAR_UNKNOWN)
19712 skipcc = get_tv_number_chk(&argvars[1], NULL);
19713 if (skipcc < 0 || skipcc > 1)
19714 EMSG(_(e_invarg));
19715 else
19716 {
19717#ifdef FEAT_MBYTE
19718 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19719 while (*s != NUL)
19720 {
19721 func_mb_ptr2char_adv(&s);
19722 ++len;
19723 }
19724 rettv->vval.v_number = len;
19725#else
19726 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19727#endif
19728 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019729}
19730
19731/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019732 * "strdisplaywidth()" function
19733 */
19734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019735f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019736{
19737 char_u *s = get_tv_string(&argvars[0]);
19738 int col = 0;
19739
19740 if (argvars[1].v_type != VAR_UNKNOWN)
19741 col = get_tv_number(&argvars[1]);
19742
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019743 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019744}
19745
19746/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019747 * "strwidth()" function
19748 */
19749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019750f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019751{
19752 char_u *s = get_tv_string(&argvars[0]);
19753
19754 rettv->vval.v_number = (varnumber_T)(
19755#ifdef FEAT_MBYTE
19756 mb_string2cells(s, -1)
19757#else
19758 STRLEN(s)
19759#endif
19760 );
19761}
19762
19763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 * "strpart()" function
19765 */
19766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019767f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768{
19769 char_u *p;
19770 int n;
19771 int len;
19772 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019773 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 slen = (int)STRLEN(p);
19777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019778 n = get_tv_number_chk(&argvars[1], &error);
19779 if (error)
19780 len = 0;
19781 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 else
19784 len = slen - n; /* default len: all bytes that are available. */
19785
19786 /*
19787 * Only return the overlap between the specified part and the actual
19788 * string.
19789 */
19790 if (n < 0)
19791 {
19792 len += n;
19793 n = 0;
19794 }
19795 else if (n > slen)
19796 n = slen;
19797 if (len < 0)
19798 len = 0;
19799 else if (n + len > slen)
19800 len = slen - n;
19801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802 rettv->v_type = VAR_STRING;
19803 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804}
19805
19806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019807 * "strridx()" function
19808 */
19809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019810f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019811{
19812 char_u buf[NUMBUFLEN];
19813 char_u *needle;
19814 char_u *haystack;
19815 char_u *rest;
19816 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019817 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019819 needle = get_tv_string_chk(&argvars[1]);
19820 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821
19822 rettv->vval.v_number = -1;
19823 if (needle == NULL || haystack == NULL)
19824 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019825
19826 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019827 if (argvars[2].v_type != VAR_UNKNOWN)
19828 {
19829 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019830 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019831 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019833 }
19834 else
19835 end_idx = haystack_len;
19836
Bram Moolenaar0d660222005-01-07 21:51:51 +000019837 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019838 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019839 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019840 lastmatch = haystack + end_idx;
19841 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019842 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019843 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019844 for (rest = haystack; *rest != '\0'; ++rest)
19845 {
19846 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019847 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019848 break;
19849 lastmatch = rest;
19850 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019852
19853 if (lastmatch == NULL)
19854 rettv->vval.v_number = -1;
19855 else
19856 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19857}
19858
19859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 * "strtrans()" function
19861 */
19862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019863f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865 rettv->v_type = VAR_STRING;
19866 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867}
19868
19869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019870 * "submatch()" function
19871 */
19872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019873f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019874{
Bram Moolenaar41571762014-04-02 19:00:58 +020019875 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019876 int no;
19877 int retList = 0;
19878
19879 no = (int)get_tv_number_chk(&argvars[0], &error);
19880 if (error)
19881 return;
19882 error = FALSE;
19883 if (argvars[1].v_type != VAR_UNKNOWN)
19884 retList = get_tv_number_chk(&argvars[1], &error);
19885 if (error)
19886 return;
19887
19888 if (retList == 0)
19889 {
19890 rettv->v_type = VAR_STRING;
19891 rettv->vval.v_string = reg_submatch(no);
19892 }
19893 else
19894 {
19895 rettv->v_type = VAR_LIST;
19896 rettv->vval.v_list = reg_submatch_list(no);
19897 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019898}
19899
19900/*
19901 * "substitute()" function
19902 */
19903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019904f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019905{
19906 char_u patbuf[NUMBUFLEN];
19907 char_u subbuf[NUMBUFLEN];
19908 char_u flagsbuf[NUMBUFLEN];
19909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019910 char_u *str = get_tv_string_chk(&argvars[0]);
19911 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19912 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19913 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19914
Bram Moolenaar0d660222005-01-07 21:51:51 +000019915 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019916 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19917 rettv->vval.v_string = NULL;
19918 else
19919 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019920}
19921
19922/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019923 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019926f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927{
19928 int id = 0;
19929#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019930 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 long col;
19932 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019933 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019935 lnum = get_tv_lnum(argvars); /* -1 on type error */
19936 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19937 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019940 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019941 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942#endif
19943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945}
19946
19947/*
19948 * "synIDattr(id, what [, mode])" function
19949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952{
19953 char_u *p = NULL;
19954#ifdef FEAT_SYN_HL
19955 int id;
19956 char_u *what;
19957 char_u *mode;
19958 char_u modebuf[NUMBUFLEN];
19959 int modec;
19960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 id = get_tv_number(&argvars[0]);
19962 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019967 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 modec = 0; /* replace invalid with current */
19969 }
19970 else
19971 {
19972#ifdef FEAT_GUI
19973 if (gui.in_use)
19974 modec = 'g';
19975 else
19976#endif
19977 if (t_colors > 1)
19978 modec = 'c';
19979 else
19980 modec = 't';
19981 }
19982
19983
19984 switch (TOLOWER_ASC(what[0]))
19985 {
19986 case 'b':
19987 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19988 p = highlight_color(id, what, modec);
19989 else /* bold */
19990 p = highlight_has_attr(id, HL_BOLD, modec);
19991 break;
19992
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019993 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 p = highlight_color(id, what, modec);
19995 break;
19996
19997 case 'i':
19998 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19999 p = highlight_has_attr(id, HL_INVERSE, modec);
20000 else /* italic */
20001 p = highlight_has_attr(id, HL_ITALIC, modec);
20002 break;
20003
20004 case 'n': /* name */
20005 p = get_highlight_name(NULL, id - 1);
20006 break;
20007
20008 case 'r': /* reverse */
20009 p = highlight_has_attr(id, HL_INVERSE, modec);
20010 break;
20011
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020012 case 's':
20013 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20014 p = highlight_color(id, what, modec);
20015 else /* standout */
20016 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 break;
20018
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020019 case 'u':
20020 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20021 /* underline */
20022 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20023 else
20024 /* undercurl */
20025 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 break;
20027 }
20028
20029 if (p != NULL)
20030 p = vim_strsave(p);
20031#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032 rettv->v_type = VAR_STRING;
20033 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034}
20035
20036/*
20037 * "synIDtrans(id)" function
20038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020040f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041{
20042 int id;
20043
20044#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046
20047 if (id > 0)
20048 id = syn_get_final_id(id);
20049 else
20050#endif
20051 id = 0;
20052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054}
20055
20056/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020057 * "synconcealed(lnum, col)" function
20058 */
20059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020060f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020061{
20062#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20063 long lnum;
20064 long col;
20065 int syntax_flags = 0;
20066 int cchar;
20067 int matchid = 0;
20068 char_u str[NUMBUFLEN];
20069#endif
20070
20071 rettv->v_type = VAR_LIST;
20072 rettv->vval.v_list = NULL;
20073
20074#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20075 lnum = get_tv_lnum(argvars); /* -1 on type error */
20076 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20077
20078 vim_memset(str, NUL, sizeof(str));
20079
20080 if (rettv_list_alloc(rettv) != FAIL)
20081 {
20082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20083 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20084 && curwin->w_p_cole > 0)
20085 {
20086 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20087 syntax_flags = get_syntax_info(&matchid);
20088
20089 /* get the conceal character */
20090 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20091 {
20092 cchar = syn_get_sub_char();
20093 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20094 cchar = lcs_conceal;
20095 if (cchar != NUL)
20096 {
20097# ifdef FEAT_MBYTE
20098 if (has_mbyte)
20099 (*mb_char2bytes)(cchar, str);
20100 else
20101# endif
20102 str[0] = cchar;
20103 }
20104 }
20105 }
20106
20107 list_append_number(rettv->vval.v_list,
20108 (syntax_flags & HL_CONCEAL) != 0);
20109 /* -1 to auto-determine strlen */
20110 list_append_string(rettv->vval.v_list, str, -1);
20111 list_append_number(rettv->vval.v_list, matchid);
20112 }
20113#endif
20114}
20115
20116/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020117 * "synstack(lnum, col)" function
20118 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020120f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020121{
20122#ifdef FEAT_SYN_HL
20123 long lnum;
20124 long col;
20125 int i;
20126 int id;
20127#endif
20128
20129 rettv->v_type = VAR_LIST;
20130 rettv->vval.v_list = NULL;
20131
20132#ifdef FEAT_SYN_HL
20133 lnum = get_tv_lnum(argvars); /* -1 on type error */
20134 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20135
20136 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020137 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020138 && rettv_list_alloc(rettv) != FAIL)
20139 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020140 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020141 for (i = 0; ; ++i)
20142 {
20143 id = syn_get_stack_item(i);
20144 if (id < 0)
20145 break;
20146 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20147 break;
20148 }
20149 }
20150#endif
20151}
20152
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020154get_cmd_output_as_rettv(
20155 typval_T *argvars,
20156 typval_T *rettv,
20157 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020159 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020161 char_u *infile = NULL;
20162 char_u buf[NUMBUFLEN];
20163 int err = FALSE;
20164 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020165 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020166 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020168 rettv->v_type = VAR_STRING;
20169 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020170 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020171 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020173 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020174 {
20175 /*
20176 * Write the string to a temp file, to be used for input of the shell
20177 * command.
20178 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020179 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020180 {
20181 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020182 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020183 }
20184
20185 fd = mch_fopen((char *)infile, WRITEBIN);
20186 if (fd == NULL)
20187 {
20188 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020189 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020190 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020191 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020192 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020193 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20194 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020195 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020196 else
20197 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020198 size_t len;
20199
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020200 p = get_tv_string_buf_chk(&argvars[1], buf);
20201 if (p == NULL)
20202 {
20203 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020204 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020205 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020206 len = STRLEN(p);
20207 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020208 err = TRUE;
20209 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020210 if (fclose(fd) != 0)
20211 err = TRUE;
20212 if (err)
20213 {
20214 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020215 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020216 }
20217 }
20218
Bram Moolenaar52a72462014-08-29 15:53:52 +020020219 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20220 * echoes typeahead, that messes up the display. */
20221 if (!msg_silent)
20222 flags += SHELL_COOKED;
20223
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020224 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020226 int len;
20227 listitem_T *li;
20228 char_u *s = NULL;
20229 char_u *start;
20230 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020231 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232
Bram Moolenaar52a72462014-08-29 15:53:52 +020020233 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020234 if (res == NULL)
20235 goto errret;
20236
20237 list = list_alloc();
20238 if (list == NULL)
20239 goto errret;
20240
20241 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020243 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020244 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020245 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020246 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020247
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020248 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020249 if (s == NULL)
20250 goto errret;
20251
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020252 for (p = s; start < end; ++p, ++start)
20253 *p = *start == NUL ? NL : *start;
20254 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020255
20256 li = listitem_alloc();
20257 if (li == NULL)
20258 {
20259 vim_free(s);
20260 goto errret;
20261 }
20262 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020263 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264 li->li_tv.vval.v_string = s;
20265 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020267
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020268 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020269 rettv->v_type = VAR_LIST;
20270 rettv->vval.v_list = list;
20271 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020273 else
20274 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020275 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020276#ifdef USE_CR
20277 /* translate <CR> into <NL> */
20278 if (res != NULL)
20279 {
20280 char_u *s;
20281
20282 for (s = res; *s; ++s)
20283 {
20284 if (*s == CAR)
20285 *s = NL;
20286 }
20287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288#else
20289# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020290 /* translate <CR><NL> into <NL> */
20291 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020293 char_u *s, *d;
20294
20295 d = res;
20296 for (s = res; *s; ++s)
20297 {
20298 if (s[0] == CAR && s[1] == NL)
20299 ++s;
20300 *d++ = *s;
20301 }
20302 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304# endif
20305#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306 rettv->vval.v_string = res;
20307 res = NULL;
20308 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020309
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020310errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020311 if (infile != NULL)
20312 {
20313 mch_remove(infile);
20314 vim_free(infile);
20315 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316 if (res != NULL)
20317 vim_free(res);
20318 if (list != NULL)
20319 list_free(list, TRUE);
20320}
20321
20322/*
20323 * "system()" function
20324 */
20325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020326f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020327{
20328 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20329}
20330
20331/*
20332 * "systemlist()" function
20333 */
20334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020335f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020336{
20337 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338}
20339
20340/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020341 * "tabpagebuflist()" function
20342 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020344f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020345{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020346#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020347 tabpage_T *tp;
20348 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020349
20350 if (argvars[0].v_type == VAR_UNKNOWN)
20351 wp = firstwin;
20352 else
20353 {
20354 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20355 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020356 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020357 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020358 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020359 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020360 for (; wp != NULL; wp = wp->w_next)
20361 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020362 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020363 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020364 }
20365#endif
20366}
20367
20368
20369/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020370 * "tabpagenr()" function
20371 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020373f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020374{
20375 int nr = 1;
20376#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020377 char_u *arg;
20378
20379 if (argvars[0].v_type != VAR_UNKNOWN)
20380 {
20381 arg = get_tv_string_chk(&argvars[0]);
20382 nr = 0;
20383 if (arg != NULL)
20384 {
20385 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020386 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020387 else
20388 EMSG2(_(e_invexpr2), arg);
20389 }
20390 }
20391 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020392 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020393#endif
20394 rettv->vval.v_number = nr;
20395}
20396
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020397
20398#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020399static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020400
20401/*
20402 * Common code for tabpagewinnr() and winnr().
20403 */
20404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020405get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020406{
20407 win_T *twin;
20408 int nr = 1;
20409 win_T *wp;
20410 char_u *arg;
20411
20412 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20413 if (argvar->v_type != VAR_UNKNOWN)
20414 {
20415 arg = get_tv_string_chk(argvar);
20416 if (arg == NULL)
20417 nr = 0; /* type error; errmsg already given */
20418 else if (STRCMP(arg, "$") == 0)
20419 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20420 else if (STRCMP(arg, "#") == 0)
20421 {
20422 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20423 if (twin == NULL)
20424 nr = 0;
20425 }
20426 else
20427 {
20428 EMSG2(_(e_invexpr2), arg);
20429 nr = 0;
20430 }
20431 }
20432
20433 if (nr > 0)
20434 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20435 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020436 {
20437 if (wp == NULL)
20438 {
20439 /* didn't find it in this tabpage */
20440 nr = 0;
20441 break;
20442 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020443 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020444 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020445 return nr;
20446}
20447#endif
20448
20449/*
20450 * "tabpagewinnr()" function
20451 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020453f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020454{
20455 int nr = 1;
20456#ifdef FEAT_WINDOWS
20457 tabpage_T *tp;
20458
20459 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20460 if (tp == NULL)
20461 nr = 0;
20462 else
20463 nr = get_winnr(tp, &argvars[1]);
20464#endif
20465 rettv->vval.v_number = nr;
20466}
20467
20468
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020469/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020470 * "tagfiles()" function
20471 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020473f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020474{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020475 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020476 tagname_T tn;
20477 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020479 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020480 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020481 fname = alloc(MAXPATHL);
20482 if (fname == NULL)
20483 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020484
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020485 for (first = TRUE; ; first = FALSE)
20486 if (get_tagfname(&tn, first, fname) == FAIL
20487 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020488 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020489 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020490 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020491}
20492
20493/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020494 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020495 */
20496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020497f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020498{
20499 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020500
20501 tag_pattern = get_tv_string(&argvars[0]);
20502
20503 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020504 if (*tag_pattern == NUL)
20505 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020507 if (rettv_list_alloc(rettv) == OK)
20508 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020509}
20510
20511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 * "tempname()" function
20513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020515f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516{
20517 static int x = 'A';
20518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020520 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521
20522 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20523 * names. Skip 'I' and 'O', they are used for shell redirection. */
20524 do
20525 {
20526 if (x == 'Z')
20527 x = '0';
20528 else if (x == '9')
20529 x = 'A';
20530 else
20531 {
20532#ifdef EBCDIC
20533 if (x == 'I')
20534 x = 'J';
20535 else if (x == 'R')
20536 x = 'S';
20537 else
20538#endif
20539 ++x;
20540 }
20541 } while (x == 'I' || x == 'O');
20542}
20543
20544/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020545 * "test(list)" function: Just checking the walls...
20546 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020548f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020549{
20550 /* Used for unit testing. Change the code below to your liking. */
20551#if 0
20552 listitem_T *li;
20553 list_T *l;
20554 char_u *bad, *good;
20555
20556 if (argvars[0].v_type != VAR_LIST)
20557 return;
20558 l = argvars[0].vval.v_list;
20559 if (l == NULL)
20560 return;
20561 li = l->lv_first;
20562 if (li == NULL)
20563 return;
20564 bad = get_tv_string(&li->li_tv);
20565 li = li->li_next;
20566 if (li == NULL)
20567 return;
20568 good = get_tv_string(&li->li_tv);
20569 rettv->vval.v_number = test_edit_score(bad, good);
20570#endif
20571}
20572
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020573#ifdef FEAT_FLOAT
20574/*
20575 * "tan()" function
20576 */
20577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020578f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020579{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020580 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020581
20582 rettv->v_type = VAR_FLOAT;
20583 if (get_float_arg(argvars, &f) == OK)
20584 rettv->vval.v_float = tan(f);
20585 else
20586 rettv->vval.v_float = 0.0;
20587}
20588
20589/*
20590 * "tanh()" function
20591 */
20592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020594{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020595 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020596
20597 rettv->v_type = VAR_FLOAT;
20598 if (get_float_arg(argvars, &f) == OK)
20599 rettv->vval.v_float = tanh(f);
20600 else
20601 rettv->vval.v_float = 0.0;
20602}
20603#endif
20604
Bram Moolenaard52d9742005-08-21 22:20:28 +000020605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 * "tolower(string)" function
20607 */
20608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610{
20611 char_u *p;
20612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020613 p = vim_strsave(get_tv_string(&argvars[0]));
20614 rettv->v_type = VAR_STRING;
20615 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616
20617 if (p != NULL)
20618 while (*p != NUL)
20619 {
20620#ifdef FEAT_MBYTE
20621 int l;
20622
20623 if (enc_utf8)
20624 {
20625 int c, lc;
20626
20627 c = utf_ptr2char(p);
20628 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020629 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 /* TODO: reallocate string when byte count changes. */
20631 if (utf_char2len(lc) == l)
20632 utf_char2bytes(lc, p);
20633 p += l;
20634 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020635 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 p += l; /* skip multi-byte character */
20637 else
20638#endif
20639 {
20640 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20641 ++p;
20642 }
20643 }
20644}
20645
20646/*
20647 * "toupper(string)" function
20648 */
20649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020650f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020652 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020653 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654}
20655
20656/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020657 * "tr(string, fromstr, tostr)" function
20658 */
20659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020660f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020661{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020662 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020663 char_u *fromstr;
20664 char_u *tostr;
20665 char_u *p;
20666#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020667 int inlen;
20668 int fromlen;
20669 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020670 int idx;
20671 char_u *cpstr;
20672 int cplen;
20673 int first = TRUE;
20674#endif
20675 char_u buf[NUMBUFLEN];
20676 char_u buf2[NUMBUFLEN];
20677 garray_T ga;
20678
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020679 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020680 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20681 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020682
20683 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020684 rettv->v_type = VAR_STRING;
20685 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020686 if (fromstr == NULL || tostr == NULL)
20687 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020688 ga_init2(&ga, (int)sizeof(char), 80);
20689
20690#ifdef FEAT_MBYTE
20691 if (!has_mbyte)
20692#endif
20693 /* not multi-byte: fromstr and tostr must be the same length */
20694 if (STRLEN(fromstr) != STRLEN(tostr))
20695 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020696#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020697error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020698#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020699 EMSG2(_(e_invarg2), fromstr);
20700 ga_clear(&ga);
20701 return;
20702 }
20703
20704 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020705 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020706 {
20707#ifdef FEAT_MBYTE
20708 if (has_mbyte)
20709 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020710 inlen = (*mb_ptr2len)(in_str);
20711 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020712 cplen = inlen;
20713 idx = 0;
20714 for (p = fromstr; *p != NUL; p += fromlen)
20715 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020716 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020717 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020718 {
20719 for (p = tostr; *p != NUL; p += tolen)
20720 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020721 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020722 if (idx-- == 0)
20723 {
20724 cplen = tolen;
20725 cpstr = p;
20726 break;
20727 }
20728 }
20729 if (*p == NUL) /* tostr is shorter than fromstr */
20730 goto error;
20731 break;
20732 }
20733 ++idx;
20734 }
20735
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020736 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020737 {
20738 /* Check that fromstr and tostr have the same number of
20739 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020740 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020741 first = FALSE;
20742 for (p = tostr; *p != NUL; p += tolen)
20743 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020744 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020745 --idx;
20746 }
20747 if (idx != 0)
20748 goto error;
20749 }
20750
Bram Moolenaarcde88542015-08-11 19:14:00 +020020751 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020752 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020753 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020754
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020755 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020756 }
20757 else
20758#endif
20759 {
20760 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020761 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020762 if (p != NULL)
20763 ga_append(&ga, tostr[p - fromstr]);
20764 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020765 ga_append(&ga, *in_str);
20766 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020767 }
20768 }
20769
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020770 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020771 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020772 ga_append(&ga, NUL);
20773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020774 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020775}
20776
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020777#ifdef FEAT_FLOAT
20778/*
20779 * "trunc({float})" function
20780 */
20781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020783{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020784 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020785
20786 rettv->v_type = VAR_FLOAT;
20787 if (get_float_arg(argvars, &f) == OK)
20788 /* trunc() is not in C90, use floor() or ceil() instead. */
20789 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20790 else
20791 rettv->vval.v_float = 0.0;
20792}
20793#endif
20794
Bram Moolenaar8299df92004-07-10 09:47:34 +000020795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 * "type(expr)" function
20797 */
20798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020799f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020801 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020802
20803 switch (argvars[0].v_type)
20804 {
20805 case VAR_NUMBER: n = 0; break;
20806 case VAR_STRING: n = 1; break;
20807 case VAR_FUNC: n = 2; break;
20808 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020809 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020810 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020811 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020812 if (argvars[0].vval.v_number == VVAL_FALSE
20813 || argvars[0].vval.v_number == VVAL_TRUE)
20814 n = 6;
20815 else
20816 n = 7;
20817 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020818 case VAR_JOB: n = 8; break;
20819 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020820 case VAR_UNKNOWN:
20821 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20822 n = -1;
20823 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020824 }
20825 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826}
20827
20828/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020829 * "undofile(name)" function
20830 */
20831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020832f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020833{
20834 rettv->v_type = VAR_STRING;
20835#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020836 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020837 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020838
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020839 if (*fname == NUL)
20840 {
20841 /* If there is no file name there will be no undo file. */
20842 rettv->vval.v_string = NULL;
20843 }
20844 else
20845 {
20846 char_u *ffname = FullName_save(fname, FALSE);
20847
20848 if (ffname != NULL)
20849 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20850 vim_free(ffname);
20851 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020852 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020853#else
20854 rettv->vval.v_string = NULL;
20855#endif
20856}
20857
20858/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020859 * "undotree()" function
20860 */
20861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020862f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020863{
20864 if (rettv_dict_alloc(rettv) == OK)
20865 {
20866 dict_T *dict = rettv->vval.v_dict;
20867 list_T *list;
20868
Bram Moolenaar730cde92010-06-27 05:18:54 +020020869 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020870 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020871 dict_add_nr_str(dict, "save_last",
20872 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020873 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20874 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020875 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020876
20877 list = list_alloc();
20878 if (list != NULL)
20879 {
20880 u_eval_tree(curbuf->b_u_oldhead, list);
20881 dict_add_list(dict, "entries", list);
20882 }
20883 }
20884}
20885
20886/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020887 * "values(dict)" function
20888 */
20889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020891{
20892 dict_list(argvars, rettv, 1);
20893}
20894
20895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 * "virtcol(string)" function
20897 */
20898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020899f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900{
20901 colnr_T vcol = 0;
20902 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020903 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020905 fp = var2fpos(&argvars[0], FALSE, &fnum);
20906 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20907 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 {
20909 getvvcol(curwin, fp, NULL, NULL, &vcol);
20910 ++vcol;
20911 }
20912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914}
20915
20916/*
20917 * "visualmode()" function
20918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020920f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 char_u str[2];
20923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020924 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925 str[0] = curbuf->b_visual_mode_eval;
20926 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020927 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928
20929 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020930 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932}
20933
20934/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020935 * "wildmenumode()" function
20936 */
20937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020938f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020939{
20940#ifdef FEAT_WILDMENU
20941 if (wild_menu_showing)
20942 rettv->vval.v_number = 1;
20943#endif
20944}
20945
20946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 * "winbufnr(nr)" function
20948 */
20949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020950f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951{
20952 win_T *wp;
20953
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020954 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020958 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959}
20960
20961/*
20962 * "wincol()" function
20963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966{
20967 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969}
20970
20971/*
20972 * "winheight(nr)" function
20973 */
20974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020975f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976{
20977 win_T *wp;
20978
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020979 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020983 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984}
20985
20986/*
20987 * "winline()" function
20988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020990f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991{
20992 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994}
20995
20996/*
20997 * "winnr()" function
20998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021000f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001{
21002 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021003
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021005 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021007 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008}
21009
21010/*
21011 * "winrestcmd()" function
21012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021014f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
21016#ifdef FEAT_WINDOWS
21017 win_T *wp;
21018 int winnr = 1;
21019 garray_T ga;
21020 char_u buf[50];
21021
21022 ga_init2(&ga, (int)sizeof(char), 70);
21023 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21024 {
21025 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21026 ga_concat(&ga, buf);
21027# ifdef FEAT_VERTSPLIT
21028 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21029 ga_concat(&ga, buf);
21030# endif
21031 ++winnr;
21032 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021033 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021037 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040}
21041
21042/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021043 * "winrestview()" function
21044 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021047{
21048 dict_T *dict;
21049
21050 if (argvars[0].v_type != VAR_DICT
21051 || (dict = argvars[0].vval.v_dict) == NULL)
21052 EMSG(_(e_invarg));
21053 else
21054 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021055 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21056 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21057 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21058 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021059#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021060 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21061 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021062#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021063 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21064 {
21065 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21066 curwin->w_set_curswant = FALSE;
21067 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021068
Bram Moolenaar82c25852014-05-28 16:47:16 +020021069 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21070 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021071#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021072 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21073 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021074#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021075 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21076 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21077 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21078 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021079
21080 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021081 win_new_height(curwin, curwin->w_height);
21082# ifdef FEAT_VERTSPLIT
21083 win_new_width(curwin, W_WIDTH(curwin));
21084# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021085 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021086
Bram Moolenaarb851a962014-10-31 15:45:52 +010021087 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021088 curwin->w_topline = 1;
21089 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21090 curwin->w_topline = curbuf->b_ml.ml_line_count;
21091#ifdef FEAT_DIFF
21092 check_topfill(curwin, TRUE);
21093#endif
21094 }
21095}
21096
21097/*
21098 * "winsaveview()" function
21099 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021102{
21103 dict_T *dict;
21104
Bram Moolenaara800b422010-06-27 01:15:55 +020021105 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021106 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021107 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021108
21109 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21110 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21111#ifdef FEAT_VIRTUALEDIT
21112 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21113#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021114 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021115 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21116
21117 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21118#ifdef FEAT_DIFF
21119 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21120#endif
21121 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21122 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21123}
21124
21125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 * "winwidth(nr)" function
21127 */
21128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021129f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130{
21131 win_T *wp;
21132
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021133 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 else
21137#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141#endif
21142}
21143
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021145 * "wordcount()" function
21146 */
21147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021148f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021149{
21150 if (rettv_dict_alloc(rettv) == FAIL)
21151 return;
21152 cursor_pos_info(rettv->vval.v_dict);
21153}
21154
21155/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021156 * Write list of strings to file
21157 */
21158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021159write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021160{
21161 listitem_T *li;
21162 int c;
21163 int ret = OK;
21164 char_u *s;
21165
21166 for (li = list->lv_first; li != NULL; li = li->li_next)
21167 {
21168 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21169 {
21170 if (*s == '\n')
21171 c = putc(NUL, fd);
21172 else
21173 c = putc(*s, fd);
21174 if (c == EOF)
21175 {
21176 ret = FAIL;
21177 break;
21178 }
21179 }
21180 if (!binary || li->li_next != NULL)
21181 if (putc('\n', fd) == EOF)
21182 {
21183 ret = FAIL;
21184 break;
21185 }
21186 if (ret == FAIL)
21187 {
21188 EMSG(_(e_write));
21189 break;
21190 }
21191 }
21192 return ret;
21193}
21194
21195/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021196 * "writefile()" function
21197 */
21198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021199f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021200{
21201 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021202 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021203 char_u *fname;
21204 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021205 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021206
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021207 if (check_restricted() || check_secure())
21208 return;
21209
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021210 if (argvars[0].v_type != VAR_LIST)
21211 {
21212 EMSG2(_(e_listarg), "writefile()");
21213 return;
21214 }
21215 if (argvars[0].vval.v_list == NULL)
21216 return;
21217
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021218 if (argvars[2].v_type != VAR_UNKNOWN)
21219 {
21220 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21221 binary = TRUE;
21222 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21223 append = TRUE;
21224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021225
21226 /* Always open the file in binary mode, library functions have a mind of
21227 * their own about CR-LF conversion. */
21228 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021229 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21230 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021231 {
21232 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21233 ret = -1;
21234 }
21235 else
21236 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021237 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21238 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021239 fclose(fd);
21240 }
21241
21242 rettv->vval.v_number = ret;
21243}
21244
21245/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021246 * "xor(expr, expr)" function
21247 */
21248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021249f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021250{
21251 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21252 ^ get_tv_number_chk(&argvars[1], NULL);
21253}
21254
21255
21256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021258 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 */
21260 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021261var2fpos(
21262 typval_T *varp,
21263 int dollar_lnum, /* TRUE when $ is last line */
21264 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021266 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021268 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269
Bram Moolenaara5525202006-03-02 22:52:09 +000021270 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021271 if (varp->v_type == VAR_LIST)
21272 {
21273 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021274 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021275 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021276 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021277
21278 l = varp->vval.v_list;
21279 if (l == NULL)
21280 return NULL;
21281
21282 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021283 pos.lnum = list_find_nr(l, 0L, &error);
21284 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021285 return NULL; /* invalid line number */
21286
21287 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021288 pos.col = list_find_nr(l, 1L, &error);
21289 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021290 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021291 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021292
21293 /* We accept "$" for the column number: last column. */
21294 li = list_find(l, 1L);
21295 if (li != NULL && li->li_tv.v_type == VAR_STRING
21296 && li->li_tv.vval.v_string != NULL
21297 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21298 pos.col = len + 1;
21299
Bram Moolenaara5525202006-03-02 22:52:09 +000021300 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021301 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021302 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021303 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021304
Bram Moolenaara5525202006-03-02 22:52:09 +000021305#ifdef FEAT_VIRTUALEDIT
21306 /* Get the virtual offset. Defaults to zero. */
21307 pos.coladd = list_find_nr(l, 2L, &error);
21308 if (error)
21309 pos.coladd = 0;
21310#endif
21311
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021312 return &pos;
21313 }
21314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021315 name = get_tv_string_chk(varp);
21316 if (name == NULL)
21317 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021318 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021320 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21321 {
21322 if (VIsual_active)
21323 return &VIsual;
21324 return &curwin->w_cursor;
21325 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021326 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021328 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21330 return NULL;
21331 return pp;
21332 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021333
21334#ifdef FEAT_VIRTUALEDIT
21335 pos.coladd = 0;
21336#endif
21337
Bram Moolenaar477933c2007-07-17 14:32:23 +000021338 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021339 {
21340 pos.col = 0;
21341 if (name[1] == '0') /* "w0": first visible line */
21342 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021343 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021344 pos.lnum = curwin->w_topline;
21345 return &pos;
21346 }
21347 else if (name[1] == '$') /* "w$": last visible line */
21348 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021349 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021350 pos.lnum = curwin->w_botline - 1;
21351 return &pos;
21352 }
21353 }
21354 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021356 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 {
21358 pos.lnum = curbuf->b_ml.ml_line_count;
21359 pos.col = 0;
21360 }
21361 else
21362 {
21363 pos.lnum = curwin->w_cursor.lnum;
21364 pos.col = (colnr_T)STRLEN(ml_get_curline());
21365 }
21366 return &pos;
21367 }
21368 return NULL;
21369}
21370
21371/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021372 * Convert list in "arg" into a position and optional file number.
21373 * When "fnump" is NULL there is no file number, only 3 items.
21374 * Note that the column is passed on as-is, the caller may want to decrement
21375 * it to use 1 for the first column.
21376 * Return FAIL when conversion is not possible, doesn't check the position for
21377 * validity.
21378 */
21379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380list2fpos(
21381 typval_T *arg,
21382 pos_T *posp,
21383 int *fnump,
21384 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021385{
21386 list_T *l = arg->vval.v_list;
21387 long i = 0;
21388 long n;
21389
Bram Moolenaar493c1782014-05-28 14:34:46 +020021390 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21391 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021392 if (arg->v_type != VAR_LIST
21393 || l == NULL
21394 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021395 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021396 return FAIL;
21397
21398 if (fnump != NULL)
21399 {
21400 n = list_find_nr(l, i++, NULL); /* fnum */
21401 if (n < 0)
21402 return FAIL;
21403 if (n == 0)
21404 n = curbuf->b_fnum; /* current buffer */
21405 *fnump = n;
21406 }
21407
21408 n = list_find_nr(l, i++, NULL); /* lnum */
21409 if (n < 0)
21410 return FAIL;
21411 posp->lnum = n;
21412
21413 n = list_find_nr(l, i++, NULL); /* col */
21414 if (n < 0)
21415 return FAIL;
21416 posp->col = n;
21417
21418#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021419 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021420 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021421 posp->coladd = 0;
21422 else
21423 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021424#endif
21425
Bram Moolenaar493c1782014-05-28 14:34:46 +020021426 if (curswantp != NULL)
21427 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21428
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021429 return OK;
21430}
21431
21432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 * Get the length of an environment variable name.
21434 * Advance "arg" to the first character after the name.
21435 * Return 0 for error.
21436 */
21437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021438get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439{
21440 char_u *p;
21441 int len;
21442
21443 for (p = *arg; vim_isIDc(*p); ++p)
21444 ;
21445 if (p == *arg) /* no name found */
21446 return 0;
21447
21448 len = (int)(p - *arg);
21449 *arg = p;
21450 return len;
21451}
21452
21453/*
21454 * Get the length of the name of a function or internal variable.
21455 * "arg" is advanced to the first non-white character after the name.
21456 * Return 0 if something is wrong.
21457 */
21458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021459get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460{
21461 char_u *p;
21462 int len;
21463
21464 /* Find the end of the name. */
21465 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021466 {
21467 if (*p == ':')
21468 {
21469 /* "s:" is start of "s:var", but "n:" is not and can be used in
21470 * slice "[n:]". Also "xx:" is not a namespace. */
21471 len = (int)(p - *arg);
21472 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21473 || len > 1)
21474 break;
21475 }
21476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 if (p == *arg) /* no name found */
21478 return 0;
21479
21480 len = (int)(p - *arg);
21481 *arg = skipwhite(p);
21482
21483 return len;
21484}
21485
21486/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021487 * Get the length of the name of a variable or function.
21488 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021490 * Return -1 if curly braces expansion failed.
21491 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * If the name contains 'magic' {}'s, expand them and return the
21493 * expanded name in an allocated string via 'alias' - caller must free.
21494 */
21495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021496get_name_len(
21497 char_u **arg,
21498 char_u **alias,
21499 int evaluate,
21500 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501{
21502 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 char_u *p;
21504 char_u *expr_start;
21505 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506
21507 *alias = NULL; /* default to no alias */
21508
21509 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21510 && (*arg)[2] == (int)KE_SNR)
21511 {
21512 /* hard coded <SNR>, already translated */
21513 *arg += 3;
21514 return get_id_len(arg) + 3;
21515 }
21516 len = eval_fname_script(*arg);
21517 if (len > 0)
21518 {
21519 /* literal "<SID>", "s:" or "<SNR>" */
21520 *arg += len;
21521 }
21522
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021524 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021526 p = find_name_end(*arg, &expr_start, &expr_end,
21527 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 if (expr_start != NULL)
21529 {
21530 char_u *temp_string;
21531
21532 if (!evaluate)
21533 {
21534 len += (int)(p - *arg);
21535 *arg = skipwhite(p);
21536 return len;
21537 }
21538
21539 /*
21540 * Include any <SID> etc in the expanded string:
21541 * Thus the -len here.
21542 */
21543 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21544 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021545 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 *alias = temp_string;
21547 *arg = skipwhite(p);
21548 return (int)STRLEN(temp_string);
21549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550
21551 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021552 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 EMSG2(_(e_invexpr2), *arg);
21554
21555 return len;
21556}
21557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021558/*
21559 * Find the end of a variable or function name, taking care of magic braces.
21560 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21561 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021562 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021563 * Return a pointer to just after the name. Equal to "arg" if there is no
21564 * valid name.
21565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567find_name_end(
21568 char_u *arg,
21569 char_u **expr_start,
21570 char_u **expr_end,
21571 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 int mb_nest = 0;
21574 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021576 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021578 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021580 *expr_start = NULL;
21581 *expr_end = NULL;
21582 }
21583
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021584 /* Quick check for valid starting character. */
21585 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21586 return arg;
21587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021588 for (p = arg; *p != NUL
21589 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021590 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021591 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021592 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021593 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021594 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021595 if (*p == '\'')
21596 {
21597 /* skip over 'string' to avoid counting [ and ] inside it. */
21598 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21599 ;
21600 if (*p == NUL)
21601 break;
21602 }
21603 else if (*p == '"')
21604 {
21605 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21606 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21607 if (*p == '\\' && p[1] != NUL)
21608 ++p;
21609 if (*p == NUL)
21610 break;
21611 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021612 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21613 {
21614 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021615 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021616 len = (int)(p - arg);
21617 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021618 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021619 break;
21620 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 if (*p == '[')
21625 ++br_nest;
21626 else if (*p == ']')
21627 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021630 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 if (*p == '{')
21633 {
21634 mb_nest++;
21635 if (expr_start != NULL && *expr_start == NULL)
21636 *expr_start = p;
21637 }
21638 else if (*p == '}')
21639 {
21640 mb_nest--;
21641 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21642 *expr_end = p;
21643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 }
21646
21647 return p;
21648}
21649
21650/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021651 * Expands out the 'magic' {}'s in a variable/function name.
21652 * Note that this can call itself recursively, to deal with
21653 * constructs like foo{bar}{baz}{bam}
21654 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21655 * "in_start" ^
21656 * "expr_start" ^
21657 * "expr_end" ^
21658 * "in_end" ^
21659 *
21660 * Returns a new allocated string, which the caller must free.
21661 * Returns NULL for failure.
21662 */
21663 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021664make_expanded_name(
21665 char_u *in_start,
21666 char_u *expr_start,
21667 char_u *expr_end,
21668 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021669{
21670 char_u c1;
21671 char_u *retval = NULL;
21672 char_u *temp_result;
21673 char_u *nextcmd = NULL;
21674
21675 if (expr_end == NULL || in_end == NULL)
21676 return NULL;
21677 *expr_start = NUL;
21678 *expr_end = NUL;
21679 c1 = *in_end;
21680 *in_end = NUL;
21681
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021682 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021683 if (temp_result != NULL && nextcmd == NULL)
21684 {
21685 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21686 + (in_end - expr_end) + 1));
21687 if (retval != NULL)
21688 {
21689 STRCPY(retval, in_start);
21690 STRCAT(retval, temp_result);
21691 STRCAT(retval, expr_end + 1);
21692 }
21693 }
21694 vim_free(temp_result);
21695
21696 *in_end = c1; /* put char back for error messages */
21697 *expr_start = '{';
21698 *expr_end = '}';
21699
21700 if (retval != NULL)
21701 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021702 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021703 if (expr_start != NULL)
21704 {
21705 /* Further expansion! */
21706 temp_result = make_expanded_name(retval, expr_start,
21707 expr_end, temp_result);
21708 vim_free(retval);
21709 retval = temp_result;
21710 }
21711 }
21712
21713 return retval;
21714}
21715
21716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021718 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 */
21720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021721eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021723 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21724}
21725
21726/*
21727 * Return TRUE if character "c" can be used as the first character in a
21728 * variable or function name (excluding '{' and '}').
21729 */
21730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021732{
21733 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734}
21735
21736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 * Set number v: variable to "val".
21738 */
21739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021740set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743}
21744
21745/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021746 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 */
21748 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021749get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021751 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752}
21753
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021754/*
21755 * Get string v: variable value. Uses a static buffer, can only be used once.
21756 */
21757 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021758get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021759{
21760 return get_tv_string(&vimvars[idx].vv_tv);
21761}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021762
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021764 * Get List v: variable value. Caller must take care of reference count when
21765 * needed.
21766 */
21767 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021768get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021769{
21770 return vimvars[idx].vv_list;
21771}
21772
21773/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021774 * Set v:char to character "c".
21775 */
21776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021777set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021778{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021779 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021780
21781#ifdef FEAT_MBYTE
21782 if (has_mbyte)
21783 buf[(*mb_char2bytes)(c, buf)] = NUL;
21784 else
21785#endif
21786 {
21787 buf[0] = c;
21788 buf[1] = NUL;
21789 }
21790 set_vim_var_string(VV_CHAR, buf, -1);
21791}
21792
21793/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021794 * Set v:count to "count" and v:count1 to "count1".
21795 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 */
21797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021798set_vcount(
21799 long count,
21800 long count1,
21801 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021803 if (set_prevcount)
21804 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021805 vimvars[VV_COUNT].vv_nr = count;
21806 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807}
21808
21809/*
21810 * Set string v: variable to a copy of "val".
21811 */
21812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021813set_vim_var_string(
21814 int idx,
21815 char_u *val,
21816 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817{
Bram Moolenaara542c682016-01-31 16:28:04 +010021818 clear_tv(&vimvars[idx].vv_di.di_tv);
21819 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021821 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021823 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021825 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826}
21827
21828/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021829 * Set List v: variable to "val".
21830 */
21831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021832set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021833{
Bram Moolenaara542c682016-01-31 16:28:04 +010021834 clear_tv(&vimvars[idx].vv_di.di_tv);
21835 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021836 vimvars[idx].vv_list = val;
21837 if (val != NULL)
21838 ++val->lv_refcount;
21839}
21840
21841/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021842 * Set Dictionary v: variable to "val".
21843 */
21844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021845set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021846{
21847 int todo;
21848 hashitem_T *hi;
21849
Bram Moolenaara542c682016-01-31 16:28:04 +010021850 clear_tv(&vimvars[idx].vv_di.di_tv);
21851 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021852 vimvars[idx].vv_dict = val;
21853 if (val != NULL)
21854 {
21855 ++val->dv_refcount;
21856
21857 /* Set readonly */
21858 todo = (int)val->dv_hashtab.ht_used;
21859 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21860 {
21861 if (HASHITEM_EMPTY(hi))
21862 continue;
21863 --todo;
21864 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21865 }
21866 }
21867}
21868
21869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 * Set v:register if needed.
21871 */
21872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021873set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874{
21875 char_u regname;
21876
21877 if (c == 0 || c == ' ')
21878 regname = '"';
21879 else
21880 regname = c;
21881 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021882 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 set_vim_var_string(VV_REG, &regname, 1);
21884}
21885
21886/*
21887 * Get or set v:exception. If "oldval" == NULL, return the current value.
21888 * Otherwise, restore the value to "oldval" and return NULL.
21889 * Must always be called in pairs to save and restore v:exception! Does not
21890 * take care of memory allocations.
21891 */
21892 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021893v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894{
21895 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021896 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897
Bram Moolenaare9a41262005-01-15 22:18:47 +000021898 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 return NULL;
21900}
21901
21902/*
21903 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21904 * Otherwise, restore the value to "oldval" and return NULL.
21905 * Must always be called in pairs to save and restore v:throwpoint! Does not
21906 * take care of memory allocations.
21907 */
21908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021909v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910{
21911 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021912 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913
Bram Moolenaare9a41262005-01-15 22:18:47 +000021914 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 return NULL;
21916}
21917
21918#if defined(FEAT_AUTOCMD) || defined(PROTO)
21919/*
21920 * Set v:cmdarg.
21921 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21922 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21923 * Must always be called in pairs!
21924 */
21925 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021926set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927{
21928 char_u *oldval;
21929 char_u *newval;
21930 unsigned len;
21931
Bram Moolenaare9a41262005-01-15 22:18:47 +000021932 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021933 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021935 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 }
21939
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021940 if (eap->force_bin == FORCE_BIN)
21941 len = 6;
21942 else if (eap->force_bin == FORCE_NOBIN)
21943 len = 8;
21944 else
21945 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021946
21947 if (eap->read_edit)
21948 len += 7;
21949
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021950 if (eap->force_ff != 0)
21951 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21952# ifdef FEAT_MBYTE
21953 if (eap->force_enc != 0)
21954 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021955 if (eap->bad_char != 0)
21956 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021957# endif
21958
21959 newval = alloc(len + 1);
21960 if (newval == NULL)
21961 return NULL;
21962
21963 if (eap->force_bin == FORCE_BIN)
21964 sprintf((char *)newval, " ++bin");
21965 else if (eap->force_bin == FORCE_NOBIN)
21966 sprintf((char *)newval, " ++nobin");
21967 else
21968 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021969
21970 if (eap->read_edit)
21971 STRCAT(newval, " ++edit");
21972
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021973 if (eap->force_ff != 0)
21974 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21975 eap->cmd + eap->force_ff);
21976# ifdef FEAT_MBYTE
21977 if (eap->force_enc != 0)
21978 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21979 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021980 if (eap->bad_char == BAD_KEEP)
21981 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21982 else if (eap->bad_char == BAD_DROP)
21983 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21984 else if (eap->bad_char != 0)
21985 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021986# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021987 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021988 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989}
21990#endif
21991
21992/*
21993 * Get the value of internal variable "name".
21994 * Return OK or FAIL.
21995 */
21996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021997get_var_tv(
21998 char_u *name,
21999 int len, /* length of "name" */
22000 typval_T *rettv, /* NULL when only checking existence */
22001 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22002 int verbose, /* may give error message */
22003 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004{
22005 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 typval_T *tv = NULL;
22007 typval_T atv;
22008 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010
22011 /* truncate the name, so that we can use strcmp() */
22012 cc = name[len];
22013 name[len] = NUL;
22014
22015 /*
22016 * Check for "b:changedtick".
22017 */
22018 if (STRCMP(name, "b:changedtick") == 0)
22019 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022020 atv.v_type = VAR_NUMBER;
22021 atv.vval.v_number = curbuf->b_changedtick;
22022 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 }
22024
22025 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 * Check for user-defined variables.
22027 */
22028 else
22029 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022030 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022032 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022034 if (dip != NULL)
22035 *dip = v;
22036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 }
22038
Bram Moolenaare9a41262005-01-15 22:18:47 +000022039 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022041 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022042 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 ret = FAIL;
22044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022045 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022046 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047
22048 name[len] = cc;
22049
22050 return ret;
22051}
22052
22053/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022054 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22055 * Also handle function call with Funcref variable: func(expr)
22056 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22057 */
22058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022059handle_subscript(
22060 char_u **arg,
22061 typval_T *rettv,
22062 int evaluate, /* do more than finding the end */
22063 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022064{
22065 int ret = OK;
22066 dict_T *selfdict = NULL;
22067 char_u *s;
22068 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022069 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022070
22071 while (ret == OK
22072 && (**arg == '['
22073 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022074 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022075 && !vim_iswhite(*(*arg - 1)))
22076 {
22077 if (**arg == '(')
22078 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022079 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022080 if (evaluate)
22081 {
22082 functv = *rettv;
22083 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022084
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022085 /* Invoke the function. Recursive! */
22086 s = functv.vval.v_string;
22087 }
22088 else
22089 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022090 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022091 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22092 &len, evaluate, selfdict);
22093
22094 /* Clear the funcref afterwards, so that deleting it while
22095 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022096 if (evaluate)
22097 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022098
22099 /* Stop the expression evaluation when immediately aborting on
22100 * error, or when an interrupt occurred or an exception was thrown
22101 * but not caught. */
22102 if (aborting())
22103 {
22104 if (ret == OK)
22105 clear_tv(rettv);
22106 ret = FAIL;
22107 }
22108 dict_unref(selfdict);
22109 selfdict = NULL;
22110 }
22111 else /* **arg == '[' || **arg == '.' */
22112 {
22113 dict_unref(selfdict);
22114 if (rettv->v_type == VAR_DICT)
22115 {
22116 selfdict = rettv->vval.v_dict;
22117 if (selfdict != NULL)
22118 ++selfdict->dv_refcount;
22119 }
22120 else
22121 selfdict = NULL;
22122 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22123 {
22124 clear_tv(rettv);
22125 ret = FAIL;
22126 }
22127 }
22128 }
22129 dict_unref(selfdict);
22130 return ret;
22131}
22132
22133/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022134 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135 * value).
22136 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022137 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022138alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022139{
Bram Moolenaar33570922005-01-25 22:26:29 +000022140 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022141}
22142
22143/*
22144 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 * The string "s" must have been allocated, it is consumed.
22146 * Return NULL for out of memory, the variable otherwise.
22147 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022148 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022149alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150{
Bram Moolenaar33570922005-01-25 22:26:29 +000022151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153 rettv = alloc_tv();
22154 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022156 rettv->v_type = VAR_STRING;
22157 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
22159 else
22160 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022161 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162}
22163
22164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022165 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022168free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169{
22170 if (varp != NULL)
22171 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022172 switch (varp->v_type)
22173 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022175 func_unref(varp->vval.v_string);
22176 /*FALLTHROUGH*/
22177 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022178 vim_free(varp->vval.v_string);
22179 break;
22180 case VAR_LIST:
22181 list_unref(varp->vval.v_list);
22182 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183 case VAR_DICT:
22184 dict_unref(varp->vval.v_dict);
22185 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022186 case VAR_JOB:
22187#ifdef FEAT_JOB
22188 job_unref(varp->vval.v_job);
22189 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022190#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022191 case VAR_CHANNEL:
22192#ifdef FEAT_CHANNEL
22193 channel_unref(varp->vval.v_channel);
22194 break;
22195#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022196 case VAR_NUMBER:
22197 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022198 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022199 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022200 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 vim_free(varp);
22203 }
22204}
22205
22206/*
22207 * Free the memory for a variable value and set the value to NULL or 0.
22208 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022210clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211{
22212 if (varp != NULL)
22213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022214 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022216 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022217 func_unref(varp->vval.v_string);
22218 /*FALLTHROUGH*/
22219 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022220 vim_free(varp->vval.v_string);
22221 varp->vval.v_string = NULL;
22222 break;
22223 case VAR_LIST:
22224 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022225 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022226 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022227 case VAR_DICT:
22228 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022229 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022230 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022231 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022232 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022233 varp->vval.v_number = 0;
22234 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022235 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022236#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022237 varp->vval.v_float = 0.0;
22238 break;
22239#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022240 case VAR_JOB:
22241#ifdef FEAT_JOB
22242 job_unref(varp->vval.v_job);
22243 varp->vval.v_job = NULL;
22244#endif
22245 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022246 case VAR_CHANNEL:
22247#ifdef FEAT_CHANNEL
22248 channel_unref(varp->vval.v_channel);
22249 varp->vval.v_channel = NULL;
22250#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022251 case VAR_UNKNOWN:
22252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022254 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 }
22256}
22257
22258/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022259 * Set the value of a variable to NULL without freeing items.
22260 */
22261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022262init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022263{
22264 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022265 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022266}
22267
22268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 * Get the number value of a variable.
22270 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022271 * For incompatible types, return 0.
22272 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22273 * caller of incompatible types: it sets *denote to TRUE if "denote"
22274 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 */
22276 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022279 int error = FALSE;
22280
22281 return get_tv_number_chk(varp, &error); /* return 0L on error */
22282}
22283
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022284 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022285get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022286{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022287 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022291 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022292 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022293 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022294#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022295 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022296 break;
22297#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022298 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022299 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022300 break;
22301 case VAR_STRING:
22302 if (varp->vval.v_string != NULL)
22303 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022304 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022305 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022306 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022307 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022308 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022309 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022310 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022311 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022312 case VAR_SPECIAL:
22313 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22314 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022315 case VAR_JOB:
22316#ifdef FEAT_JOB
22317 EMSG(_("E910: Using a Job as a Number"));
22318 break;
22319#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022320 case VAR_CHANNEL:
22321#ifdef FEAT_CHANNEL
22322 EMSG(_("E913: Using a Channel as a Number"));
22323 break;
22324#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022325 case VAR_UNKNOWN:
22326 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022329 if (denote == NULL) /* useful for values that must be unsigned */
22330 n = -1;
22331 else
22332 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022333 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334}
22335
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022336#ifdef FEAT_FLOAT
22337 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022338get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022339{
22340 switch (varp->v_type)
22341 {
22342 case VAR_NUMBER:
22343 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022344 case VAR_FLOAT:
22345 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022346 case VAR_FUNC:
22347 EMSG(_("E891: Using a Funcref as a Float"));
22348 break;
22349 case VAR_STRING:
22350 EMSG(_("E892: Using a String as a Float"));
22351 break;
22352 case VAR_LIST:
22353 EMSG(_("E893: Using a List as a Float"));
22354 break;
22355 case VAR_DICT:
22356 EMSG(_("E894: Using a Dictionary as a Float"));
22357 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022358 case VAR_SPECIAL:
22359 EMSG(_("E907: Using a special value as a Float"));
22360 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022361 case VAR_JOB:
22362# ifdef FEAT_JOB
22363 EMSG(_("E911: Using a Job as a Float"));
22364 break;
22365# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022366 case VAR_CHANNEL:
22367# ifdef FEAT_CHANNEL
22368 EMSG(_("E914: Using a Channel as a Float"));
22369 break;
22370# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022371 case VAR_UNKNOWN:
22372 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022373 break;
22374 }
22375 return 0;
22376}
22377#endif
22378
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022380 * Get the lnum from the first argument.
22381 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022382 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 */
22384 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022385get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386{
Bram Moolenaar33570922005-01-25 22:26:29 +000022387 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 linenr_T lnum;
22389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022390 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 if (lnum == 0) /* no valid number, try using line() */
22392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022393 rettv.v_type = VAR_NUMBER;
22394 f_line(argvars, &rettv);
22395 lnum = rettv.vval.v_number;
22396 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 }
22398 return lnum;
22399}
22400
22401/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022402 * Get the lnum from the first argument.
22403 * Also accepts "$", then "buf" is used.
22404 * Returns 0 on error.
22405 */
22406 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022407get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022408{
22409 if (argvars[0].v_type == VAR_STRING
22410 && argvars[0].vval.v_string != NULL
22411 && argvars[0].vval.v_string[0] == '$'
22412 && buf != NULL)
22413 return buf->b_ml.ml_line_count;
22414 return get_tv_number_chk(&argvars[0], NULL);
22415}
22416
22417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 * Get the string value of a variable.
22419 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022420 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22421 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 * If the String variable has never been set, return an empty string.
22423 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022424 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22425 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 */
22427 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022428get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429{
22430 static char_u mybuf[NUMBUFLEN];
22431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433}
22434
22435 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022436get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022438 char_u *res = get_tv_string_buf_chk(varp, buf);
22439
22440 return res != NULL ? res : (char_u *)"";
22441}
22442
Bram Moolenaar7d647822014-04-05 21:28:56 +020022443/*
22444 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22445 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022448{
22449 static char_u mybuf[NUMBUFLEN];
22450
22451 return get_tv_string_buf_chk(varp, mybuf);
22452}
22453
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022455get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022456{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022457 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022459 case VAR_NUMBER:
22460 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22461 return buf;
22462 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022463 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022464 break;
22465 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022467 break;
22468 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022469 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022470 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022471 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022472#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022473 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022474 break;
22475#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022476 case VAR_STRING:
22477 if (varp->vval.v_string != NULL)
22478 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022480 case VAR_SPECIAL:
22481 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22482 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022483 case VAR_JOB:
22484#ifdef FEAT_JOB
22485 {
22486 job_T *job = varp->vval.v_job;
22487 char *status = job->jv_status == JOB_FAILED ? "fail"
22488 : job->jv_status == JOB_ENDED ? "dead"
22489 : "run";
22490# ifdef UNIX
22491 vim_snprintf((char *)buf, NUMBUFLEN,
22492 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022493# elif defined(WIN32)
22494 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022495 "process %ld %s",
22496 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022497 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022498# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022499 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022500 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22501# endif
22502 return buf;
22503 }
22504#endif
22505 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022506 case VAR_CHANNEL:
22507#ifdef FEAT_CHANNEL
22508 {
22509 channel_T *channel = varp->vval.v_channel;
22510 char *status = channel_status(channel);
22511
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022512 if (channel == NULL)
22513 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22514 else
22515 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022516 "channel %d %s", channel->ch_id, status);
22517 return buf;
22518 }
22519#endif
22520 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022521 case VAR_UNKNOWN:
22522 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022525 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526}
22527
22528/*
22529 * Find variable "name" in the list of variables.
22530 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022531 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022532 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022536find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540
Bram Moolenaara7043832005-01-21 11:56:39 +000022541 ht = find_var_ht(name, &varname);
22542 if (htp != NULL)
22543 *htp = ht;
22544 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022546 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547}
22548
22549/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022550 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022551 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022554find_var_in_ht(
22555 hashtab_T *ht,
22556 int htname,
22557 char_u *varname,
22558 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022559{
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 hashitem_T *hi;
22561
22562 if (*varname == NUL)
22563 {
22564 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022565 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022566 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022567 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 case 'g': return &globvars_var;
22569 case 'v': return &vimvars_var;
22570 case 'b': return &curbuf->b_bufvar;
22571 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022572#ifdef FEAT_WINDOWS
22573 case 't': return &curtab->tp_winvar;
22574#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022575 case 'l': return current_funccal == NULL
22576 ? NULL : &current_funccal->l_vars_var;
22577 case 'a': return current_funccal == NULL
22578 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 }
22580 return NULL;
22581 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022582
22583 hi = hash_find(ht, varname);
22584 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022585 {
22586 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022587 * worked find the variable again. Don't auto-load a script if it was
22588 * loaded already, otherwise it would be loaded every time when
22589 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022590 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022591 {
22592 /* Note: script_autoload() may make "hi" invalid. It must either
22593 * be obtained again or not used. */
22594 if (!script_autoload(varname, FALSE) || aborting())
22595 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022596 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022597 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022598 if (HASHITEM_EMPTY(hi))
22599 return NULL;
22600 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022602}
22603
22604/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022605 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022606 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022607 * Set "varname" to the start of name without ':'.
22608 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022609 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022610find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022612 hashitem_T *hi;
22613
Bram Moolenaar73627d02015-08-11 15:46:09 +020022614 if (name[0] == NUL)
22615 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 if (name[1] != ':')
22617 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022618 /* The name must not start with a colon or #. */
22619 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 return NULL;
22621 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022622
22623 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022624 hi = hash_find(&compat_hashtab, name);
22625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022626 return &compat_hashtab;
22627
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022629 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022630 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 }
22632 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022633 if (*name == 'g') /* global variable */
22634 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022635 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22636 */
22637 if (vim_strchr(name + 2, ':') != NULL
22638 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022641 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022643 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022644#ifdef FEAT_WINDOWS
22645 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022646 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022648 if (*name == 'v') /* v: variable */
22649 return &vimvarht;
22650 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022651 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022653 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 if (*name == 's' /* script variable */
22655 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22656 return &SCRIPT_VARS(current_SID);
22657 return NULL;
22658}
22659
22660/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022661 * Get function call environment based on bactrace debug level
22662 */
22663 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022664get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022665{
22666 int i;
22667 funccall_T *funccal;
22668 funccall_T *temp_funccal;
22669
22670 funccal = current_funccal;
22671 if (debug_backtrace_level > 0)
22672 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022673 for (i = 0; i < debug_backtrace_level; i++)
22674 {
22675 temp_funccal = funccal->caller;
22676 if (temp_funccal)
22677 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022678 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022679 /* backtrace level overflow. reset to max */
22680 debug_backtrace_level = i;
22681 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022682 }
22683 return funccal;
22684}
22685
22686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022688 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 * Returns NULL when it doesn't exist.
22690 */
22691 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022692get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693{
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022696 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 if (v == NULL)
22698 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022699 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700}
22701
22702/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 * sourcing this script and when executing functions defined in the script.
22705 */
22706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022707new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708{
Bram Moolenaara7043832005-01-21 11:56:39 +000022709 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022710 hashtab_T *ht;
22711 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022712
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022715 /* Re-allocating ga_data means that an ht_array pointing to
22716 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022718 for (i = 1; i <= ga_scripts.ga_len; ++i)
22719 {
22720 ht = &SCRIPT_VARS(i);
22721 if (ht->ht_mask == HT_INIT_SIZE - 1)
22722 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022723 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022725 }
22726
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 while (ga_scripts.ga_len < id)
22728 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022729 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022730 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022731 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 }
22734 }
22735}
22736
22737/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22739 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 */
22741 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743{
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022745 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022746 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022747 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022748 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 dict_var->di_tv.vval.v_dict = dict;
22750 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022751 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22753 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754}
22755
22756/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022757 * Unreference a dictionary initialized by init_var_dict().
22758 */
22759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022760unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022761{
22762 /* Now the dict needs to be freed if no one else is using it, go back to
22763 * normal reference counting. */
22764 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22765 dict_unref(dict);
22766}
22767
22768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022770 * Frees all allocated variables and the value they contain.
22771 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 */
22773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022774vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022775{
22776 vars_clear_ext(ht, TRUE);
22777}
22778
22779/*
22780 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22781 */
22782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022783vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784{
Bram Moolenaara7043832005-01-21 11:56:39 +000022785 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022786 hashitem_T *hi;
22787 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022790 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022791 for (hi = ht->ht_array; todo > 0; ++hi)
22792 {
22793 if (!HASHITEM_EMPTY(hi))
22794 {
22795 --todo;
22796
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022798 * ht_array might change then. hash_clear() takes care of it
22799 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 v = HI2DI(hi);
22801 if (free_val)
22802 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022803 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022804 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022805 }
22806 }
22807 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022808 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809}
22810
Bram Moolenaara7043832005-01-21 11:56:39 +000022811/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 * Delete a variable from hashtab "ht" at item "hi".
22813 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022816delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817{
Bram Moolenaar33570922005-01-25 22:26:29 +000022818 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022819
22820 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022821 clear_tv(&di->di_tv);
22822 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823}
22824
22825/*
22826 * List the value of one internal variable.
22827 */
22828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022829list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022831 char_u *tofree;
22832 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022833 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022834
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022835 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022837 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022838 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839}
22840
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022842list_one_var_a(
22843 char_u *prefix,
22844 char_u *name,
22845 int type,
22846 char_u *string,
22847 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848{
Bram Moolenaar31859182007-08-14 20:41:13 +000022849 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22850 msg_start();
22851 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 if (name != NULL) /* "a:" vars don't have a name stored */
22853 msg_puts(name);
22854 msg_putchar(' ');
22855 msg_advance(22);
22856 if (type == VAR_NUMBER)
22857 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022858 else if (type == VAR_FUNC)
22859 msg_putchar('*');
22860 else if (type == VAR_LIST)
22861 {
22862 msg_putchar('[');
22863 if (*string == '[')
22864 ++string;
22865 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022866 else if (type == VAR_DICT)
22867 {
22868 msg_putchar('{');
22869 if (*string == '{')
22870 ++string;
22871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 else
22873 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022874
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022876
22877 if (type == VAR_FUNC)
22878 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022879 if (*first)
22880 {
22881 msg_clr_eos();
22882 *first = FALSE;
22883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884}
22885
22886/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022887 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 * If the variable already exists, the value is updated.
22889 * Otherwise the variable is created.
22890 */
22891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022892set_var(
22893 char_u *name,
22894 typval_T *tv,
22895 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896{
Bram Moolenaar33570922005-01-25 22:26:29 +000022897 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022899 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022901 ht = find_var_ht(name, &varname);
22902 if (ht == NULL || *varname == NUL)
22903 {
22904 EMSG2(_(e_illvar), name);
22905 return;
22906 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022907 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022908
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022909 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22910 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022911
Bram Moolenaar33570922005-01-25 22:26:29 +000022912 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022915 if (var_check_ro(v->di_flags, name, FALSE)
22916 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 return;
22918 if (v->di_tv.v_type != tv->v_type
22919 && !((v->di_tv.v_type == VAR_STRING
22920 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022921 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022922 || tv->v_type == VAR_NUMBER))
22923#ifdef FEAT_FLOAT
22924 && !((v->di_tv.v_type == VAR_NUMBER
22925 || v->di_tv.v_type == VAR_FLOAT)
22926 && (tv->v_type == VAR_NUMBER
22927 || tv->v_type == VAR_FLOAT))
22928#endif
22929 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022930 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022931 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022932 return;
22933 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022934
22935 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022936 * Handle setting internal v: variables separately where needed to
22937 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022938 */
22939 if (ht == &vimvarht)
22940 {
22941 if (v->di_tv.v_type == VAR_STRING)
22942 {
22943 vim_free(v->di_tv.vval.v_string);
22944 if (copy || tv->v_type != VAR_STRING)
22945 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22946 else
22947 {
22948 /* Take over the string to avoid an extra alloc/free. */
22949 v->di_tv.vval.v_string = tv->vval.v_string;
22950 tv->vval.v_string = NULL;
22951 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022952 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022954 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022955 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022957 if (STRCMP(varname, "searchforward") == 0)
22958 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022959#ifdef FEAT_SEARCH_EXTRA
22960 else if (STRCMP(varname, "hlsearch") == 0)
22961 {
22962 no_hlsearch = !v->di_tv.vval.v_number;
22963 redraw_all_later(SOME_VALID);
22964 }
22965#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022966 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022967 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022968 else if (v->di_tv.v_type != tv->v_type)
22969 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 }
22971
22972 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 }
22974 else /* add a new variable */
22975 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022976 /* Can't add "v:" variable. */
22977 if (ht == &vimvarht)
22978 {
22979 EMSG2(_(e_illvar), name);
22980 return;
22981 }
22982
Bram Moolenaar92124a32005-06-17 22:03:40 +000022983 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022984 if (!valid_varname(varname))
22985 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022987 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22988 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022989 if (v == NULL)
22990 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022994 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 return;
22996 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022997 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022999
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023000 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023002 else
23003 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023004 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023005 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008}
23009
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023011 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023012 * Also give an error message.
23013 */
23014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023015var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023016{
23017 if (flags & DI_FLAGS_RO)
23018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023019 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023020 return TRUE;
23021 }
23022 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23023 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023024 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 return TRUE;
23026 }
23027 return FALSE;
23028}
23029
23030/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023031 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23032 * Also give an error message.
23033 */
23034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023036{
23037 if (flags & DI_FLAGS_FIX)
23038 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023039 EMSG2(_("E795: Cannot delete variable %s"),
23040 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023041 return TRUE;
23042 }
23043 return FALSE;
23044}
23045
23046/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023047 * Check if a funcref is assigned to a valid variable name.
23048 * Return TRUE and give an error if not.
23049 */
23050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023051var_check_func_name(
23052 char_u *name, /* points to start of variable name */
23053 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023054{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023055 /* Allow for w: b: s: and t:. */
23056 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023057 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23058 ? name[2] : name[0]))
23059 {
23060 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23061 name);
23062 return TRUE;
23063 }
23064 /* Don't allow hiding a function. When "v" is not NULL we might be
23065 * assigning another function to the same var, the type is checked
23066 * below. */
23067 if (new_var && function_exists(name))
23068 {
23069 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23070 name);
23071 return TRUE;
23072 }
23073 return FALSE;
23074}
23075
23076/*
23077 * Check if a variable name is valid.
23078 * Return FALSE and give an error if not.
23079 */
23080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023081valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023082{
23083 char_u *p;
23084
23085 for (p = varname; *p != NUL; ++p)
23086 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23087 && *p != AUTOLOAD_CHAR)
23088 {
23089 EMSG2(_(e_illvar), varname);
23090 return FALSE;
23091 }
23092 return TRUE;
23093}
23094
23095/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023096 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023097 * Also give an error message, using "name" or _("name") when use_gettext is
23098 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023099 */
23100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023101tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023102{
23103 if (lock & VAR_LOCKED)
23104 {
23105 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023106 name == NULL ? (char_u *)_("Unknown")
23107 : use_gettext ? (char_u *)_(name)
23108 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023109 return TRUE;
23110 }
23111 if (lock & VAR_FIXED)
23112 {
23113 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023114 name == NULL ? (char_u *)_("Unknown")
23115 : use_gettext ? (char_u *)_(name)
23116 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023117 return TRUE;
23118 }
23119 return FALSE;
23120}
23121
23122/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023124 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023125 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023126 * It is OK for "from" and "to" to point to the same item. This is used to
23127 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023128 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023130copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023132 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023133 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023134 switch (from->v_type)
23135 {
23136 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023137 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023138 to->vval.v_number = from->vval.v_number;
23139 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023140 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023141#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023142 to->vval.v_float = from->vval.v_float;
23143 break;
23144#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023145 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023146#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023147 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023148 if (to->vval.v_job != NULL)
23149 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023150 break;
23151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023152 case VAR_CHANNEL:
23153#ifdef FEAT_CHANNEL
23154 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023155 if (to->vval.v_channel != NULL)
23156 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023157 break;
23158#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023159 case VAR_STRING:
23160 case VAR_FUNC:
23161 if (from->vval.v_string == NULL)
23162 to->vval.v_string = NULL;
23163 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023164 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023165 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023166 if (from->v_type == VAR_FUNC)
23167 func_ref(to->vval.v_string);
23168 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023169 break;
23170 case VAR_LIST:
23171 if (from->vval.v_list == NULL)
23172 to->vval.v_list = NULL;
23173 else
23174 {
23175 to->vval.v_list = from->vval.v_list;
23176 ++to->vval.v_list->lv_refcount;
23177 }
23178 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023179 case VAR_DICT:
23180 if (from->vval.v_dict == NULL)
23181 to->vval.v_dict = NULL;
23182 else
23183 {
23184 to->vval.v_dict = from->vval.v_dict;
23185 ++to->vval.v_dict->dv_refcount;
23186 }
23187 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023188 case VAR_UNKNOWN:
23189 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023190 break;
23191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192}
23193
23194/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023195 * Make a copy of an item.
23196 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023197 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23198 * reference to an already copied list/dict can be used.
23199 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023200 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023202item_copy(
23203 typval_T *from,
23204 typval_T *to,
23205 int deep,
23206 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023207{
23208 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023209 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023210
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023212 {
23213 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023214 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023215 }
23216 ++recurse;
23217
23218 switch (from->v_type)
23219 {
23220 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023221 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023222 case VAR_STRING:
23223 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023224 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023225 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023226 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023227 copy_tv(from, to);
23228 break;
23229 case VAR_LIST:
23230 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023231 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023232 if (from->vval.v_list == NULL)
23233 to->vval.v_list = NULL;
23234 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23235 {
23236 /* use the copy made earlier */
23237 to->vval.v_list = from->vval.v_list->lv_copylist;
23238 ++to->vval.v_list->lv_refcount;
23239 }
23240 else
23241 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23242 if (to->vval.v_list == NULL)
23243 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023244 break;
23245 case VAR_DICT:
23246 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023247 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023248 if (from->vval.v_dict == NULL)
23249 to->vval.v_dict = NULL;
23250 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23251 {
23252 /* use the copy made earlier */
23253 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23254 ++to->vval.v_dict->dv_refcount;
23255 }
23256 else
23257 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23258 if (to->vval.v_dict == NULL)
23259 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023260 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023261 case VAR_UNKNOWN:
23262 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023263 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023264 }
23265 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023266 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023267}
23268
23269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 * ":echo expr1 ..." print each argument separated with a space, add a
23271 * newline at the end.
23272 * ":echon expr1 ..." print each argument plain.
23273 */
23274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023275ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276{
23277 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023278 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023279 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 char_u *p;
23281 int needclr = TRUE;
23282 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023283 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284
23285 if (eap->skip)
23286 ++emsg_skip;
23287 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23288 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023289 /* If eval1() causes an error message the text from the command may
23290 * still need to be cleared. E.g., "echo 22,44". */
23291 need_clr_eos = needclr;
23292
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023294 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 {
23296 /*
23297 * Report the invalid expression unless the expression evaluation
23298 * has been cancelled due to an aborting error, an interrupt, or an
23299 * exception.
23300 */
23301 if (!aborting())
23302 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023303 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 break;
23305 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023306 need_clr_eos = FALSE;
23307
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 if (!eap->skip)
23309 {
23310 if (atstart)
23311 {
23312 atstart = FALSE;
23313 /* Call msg_start() after eval1(), evaluating the expression
23314 * may cause a message to appear. */
23315 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023316 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023317 /* Mark the saved text as finishing the line, so that what
23318 * follows is displayed on a new line when scrolling back
23319 * at the more prompt. */
23320 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 }
23324 else if (eap->cmdidx == CMD_echo)
23325 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023326 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023327 if (p != NULL)
23328 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023330 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023332 if (*p != TAB && needclr)
23333 {
23334 /* remove any text still there from the command */
23335 msg_clr_eos();
23336 needclr = FALSE;
23337 }
23338 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 }
23340 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023341 {
23342#ifdef FEAT_MBYTE
23343 if (has_mbyte)
23344 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023345 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023346
23347 (void)msg_outtrans_len_attr(p, i, echo_attr);
23348 p += i - 1;
23349 }
23350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023352 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023355 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023357 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 arg = skipwhite(arg);
23359 }
23360 eap->nextcmd = check_nextcmd(arg);
23361
23362 if (eap->skip)
23363 --emsg_skip;
23364 else
23365 {
23366 /* remove text that may still be there from the command */
23367 if (needclr)
23368 msg_clr_eos();
23369 if (eap->cmdidx == CMD_echo)
23370 msg_end();
23371 }
23372}
23373
23374/*
23375 * ":echohl {name}".
23376 */
23377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023378ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379{
23380 int id;
23381
23382 id = syn_name2id(eap->arg);
23383 if (id == 0)
23384 echo_attr = 0;
23385 else
23386 echo_attr = syn_id2attr(id);
23387}
23388
23389/*
23390 * ":execute expr1 ..." execute the result of an expression.
23391 * ":echomsg expr1 ..." Print a message
23392 * ":echoerr expr1 ..." Print an error
23393 * Each gets spaces around each argument and a newline at the end for
23394 * echo commands
23395 */
23396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023397ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398{
23399 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 int ret = OK;
23402 char_u *p;
23403 garray_T ga;
23404 int len;
23405 int save_did_emsg;
23406
23407 ga_init2(&ga, 1, 80);
23408
23409 if (eap->skip)
23410 ++emsg_skip;
23411 while (*arg != NUL && *arg != '|' && *arg != '\n')
23412 {
23413 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023414 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 {
23416 /*
23417 * Report the invalid expression unless the expression evaluation
23418 * has been cancelled due to an aborting error, an interrupt, or an
23419 * exception.
23420 */
23421 if (!aborting())
23422 EMSG2(_(e_invexpr2), p);
23423 ret = FAIL;
23424 break;
23425 }
23426
23427 if (!eap->skip)
23428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023429 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 len = (int)STRLEN(p);
23431 if (ga_grow(&ga, len + 2) == FAIL)
23432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023433 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 ret = FAIL;
23435 break;
23436 }
23437 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 ga.ga_len += len;
23441 }
23442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023443 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 arg = skipwhite(arg);
23445 }
23446
23447 if (ret != FAIL && ga.ga_data != NULL)
23448 {
23449 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023452 out_flush();
23453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 else if (eap->cmdidx == CMD_echoerr)
23455 {
23456 /* We don't want to abort following commands, restore did_emsg. */
23457 save_did_emsg = did_emsg;
23458 EMSG((char_u *)ga.ga_data);
23459 if (!force_abort)
23460 did_emsg = save_did_emsg;
23461 }
23462 else if (eap->cmdidx == CMD_execute)
23463 do_cmdline((char_u *)ga.ga_data,
23464 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23465 }
23466
23467 ga_clear(&ga);
23468
23469 if (eap->skip)
23470 --emsg_skip;
23471
23472 eap->nextcmd = check_nextcmd(arg);
23473}
23474
23475/*
23476 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23477 * "arg" points to the "&" or '+' when called, to "option" when returning.
23478 * Returns NULL when no option name found. Otherwise pointer to the char
23479 * after the option name.
23480 */
23481 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023482find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483{
23484 char_u *p = *arg;
23485
23486 ++p;
23487 if (*p == 'g' && p[1] == ':')
23488 {
23489 *opt_flags = OPT_GLOBAL;
23490 p += 2;
23491 }
23492 else if (*p == 'l' && p[1] == ':')
23493 {
23494 *opt_flags = OPT_LOCAL;
23495 p += 2;
23496 }
23497 else
23498 *opt_flags = 0;
23499
23500 if (!ASCII_ISALPHA(*p))
23501 return NULL;
23502 *arg = p;
23503
23504 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23505 p += 4; /* termcap option */
23506 else
23507 while (ASCII_ISALPHA(*p))
23508 ++p;
23509 return p;
23510}
23511
23512/*
23513 * ":function"
23514 */
23515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023516ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517{
23518 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023519 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520 int j;
23521 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023523 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 char_u *name = NULL;
23525 char_u *p;
23526 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023527 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 garray_T newargs;
23529 garray_T newlines;
23530 int varargs = FALSE;
23531 int mustend = FALSE;
23532 int flags = 0;
23533 ufunc_T *fp;
23534 int indent;
23535 int nesting;
23536 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 dictitem_T *v;
23538 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539 static int func_nr = 0; /* number for nameless function */
23540 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023541 hashtab_T *ht;
23542 int todo;
23543 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023544 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545
23546 /*
23547 * ":function" without argument: list functions.
23548 */
23549 if (ends_excmd(*eap->arg))
23550 {
23551 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023552 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023553 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023554 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023555 {
23556 if (!HASHITEM_EMPTY(hi))
23557 {
23558 --todo;
23559 fp = HI2UF(hi);
23560 if (!isdigit(*fp->uf_name))
23561 list_func_head(fp, FALSE);
23562 }
23563 }
23564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 eap->nextcmd = check_nextcmd(eap->arg);
23566 return;
23567 }
23568
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023569 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023570 * ":function /pat": list functions matching pattern.
23571 */
23572 if (*eap->arg == '/')
23573 {
23574 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23575 if (!eap->skip)
23576 {
23577 regmatch_T regmatch;
23578
23579 c = *p;
23580 *p = NUL;
23581 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23582 *p = c;
23583 if (regmatch.regprog != NULL)
23584 {
23585 regmatch.rm_ic = p_ic;
23586
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023587 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023588 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23589 {
23590 if (!HASHITEM_EMPTY(hi))
23591 {
23592 --todo;
23593 fp = HI2UF(hi);
23594 if (!isdigit(*fp->uf_name)
23595 && vim_regexec(&regmatch, fp->uf_name, 0))
23596 list_func_head(fp, FALSE);
23597 }
23598 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023599 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023600 }
23601 }
23602 if (*p == '/')
23603 ++p;
23604 eap->nextcmd = check_nextcmd(p);
23605 return;
23606 }
23607
23608 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023609 * Get the function name. There are these situations:
23610 * func normal function name
23611 * "name" == func, "fudi.fd_dict" == NULL
23612 * dict.func new dictionary entry
23613 * "name" == NULL, "fudi.fd_dict" set,
23614 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23615 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023616 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023617 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23618 * dict.func existing dict entry that's not a Funcref
23619 * "name" == NULL, "fudi.fd_dict" set,
23620 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023621 * s:func script-local function name
23622 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023625 name = trans_function_name(&p, eap->skip, 0, &fudi);
23626 paren = (vim_strchr(p, '(') != NULL);
23627 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 {
23629 /*
23630 * Return on an invalid expression in braces, unless the expression
23631 * evaluation has been cancelled due to an aborting error, an
23632 * interrupt, or an exception.
23633 */
23634 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023636 if (!eap->skip && fudi.fd_newkey != NULL)
23637 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023638 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 else
23642 eap->skip = TRUE;
23643 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023644
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 /* An error in a function call during evaluation of an expression in magic
23646 * braces should not cause the function not to be defined. */
23647 saved_did_emsg = did_emsg;
23648 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649
23650 /*
23651 * ":function func" with only function name: list function.
23652 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023653 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 {
23655 if (!ends_excmd(*skipwhite(p)))
23656 {
23657 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023658 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 }
23660 eap->nextcmd = check_nextcmd(p);
23661 if (eap->nextcmd != NULL)
23662 *p = NUL;
23663 if (!eap->skip && !got_int)
23664 {
23665 fp = find_func(name);
23666 if (fp != NULL)
23667 {
23668 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023671 if (FUNCLINE(fp, j) == NULL)
23672 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 msg_putchar('\n');
23674 msg_outnum((long)(j + 1));
23675 if (j < 9)
23676 msg_putchar(' ');
23677 if (j < 99)
23678 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023679 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 out_flush(); /* show a line at a time */
23681 ui_breakcheck();
23682 }
23683 if (!got_int)
23684 {
23685 msg_putchar('\n');
23686 msg_puts((char_u *)" endfunction");
23687 }
23688 }
23689 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023690 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 }
23694
23695 /*
23696 * ":function name(arg1, arg2)" Define function.
23697 */
23698 p = skipwhite(p);
23699 if (*p != '(')
23700 {
23701 if (!eap->skip)
23702 {
23703 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023704 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 }
23706 /* attempt to continue by skipping some text */
23707 if (vim_strchr(p, '(') != NULL)
23708 p = vim_strchr(p, '(');
23709 }
23710 p = skipwhite(p + 1);
23711
23712 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23713 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23714
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023715 if (!eap->skip)
23716 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023717 /* Check the name of the function. Unless it's a dictionary function
23718 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023719 if (name != NULL)
23720 arg = name;
23721 else
23722 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023723 if (arg != NULL && (fudi.fd_di == NULL
23724 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023725 {
23726 if (*arg == K_SPECIAL)
23727 j = 3;
23728 else
23729 j = 0;
23730 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23731 : eval_isnamec(arg[j])))
23732 ++j;
23733 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023734 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023735 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023736 /* Disallow using the g: dict. */
23737 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23738 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023739 }
23740
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 /*
23742 * Isolate the arguments: "arg1, arg2, ...)"
23743 */
23744 while (*p != ')')
23745 {
23746 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23747 {
23748 varargs = TRUE;
23749 p += 3;
23750 mustend = TRUE;
23751 }
23752 else
23753 {
23754 arg = p;
23755 while (ASCII_ISALNUM(*p) || *p == '_')
23756 ++p;
23757 if (arg == p || isdigit(*arg)
23758 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23759 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23760 {
23761 if (!eap->skip)
23762 EMSG2(_("E125: Illegal argument: %s"), arg);
23763 break;
23764 }
23765 if (ga_grow(&newargs, 1) == FAIL)
23766 goto erret;
23767 c = *p;
23768 *p = NUL;
23769 arg = vim_strsave(arg);
23770 if (arg == NULL)
23771 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023772
23773 /* Check for duplicate argument name. */
23774 for (i = 0; i < newargs.ga_len; ++i)
23775 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23776 {
23777 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023778 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023779 goto erret;
23780 }
23781
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23783 *p = c;
23784 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 if (*p == ',')
23786 ++p;
23787 else
23788 mustend = TRUE;
23789 }
23790 p = skipwhite(p);
23791 if (mustend && *p != ')')
23792 {
23793 if (!eap->skip)
23794 EMSG2(_(e_invarg2), eap->arg);
23795 break;
23796 }
23797 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023798 if (*p != ')')
23799 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 ++p; /* skip the ')' */
23801
Bram Moolenaare9a41262005-01-15 22:18:47 +000023802 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 for (;;)
23804 {
23805 p = skipwhite(p);
23806 if (STRNCMP(p, "range", 5) == 0)
23807 {
23808 flags |= FC_RANGE;
23809 p += 5;
23810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023811 else if (STRNCMP(p, "dict", 4) == 0)
23812 {
23813 flags |= FC_DICT;
23814 p += 4;
23815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 else if (STRNCMP(p, "abort", 5) == 0)
23817 {
23818 flags |= FC_ABORT;
23819 p += 5;
23820 }
23821 else
23822 break;
23823 }
23824
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023825 /* When there is a line break use what follows for the function body.
23826 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23827 if (*p == '\n')
23828 line_arg = p + 1;
23829 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 EMSG(_(e_trailing));
23831
23832 /*
23833 * Read the body of the function, until ":endfunction" is found.
23834 */
23835 if (KeyTyped)
23836 {
23837 /* Check if the function already exists, don't let the user type the
23838 * whole function before telling him it doesn't work! For a script we
23839 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023840 if (!eap->skip && !eap->forceit)
23841 {
23842 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23843 EMSG(_(e_funcdict));
23844 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023845 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023848 if (!eap->skip && did_emsg)
23849 goto erret;
23850
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 msg_putchar('\n'); /* don't overwrite the function name */
23852 cmdline_row = msg_row;
23853 }
23854
23855 indent = 2;
23856 nesting = 0;
23857 for (;;)
23858 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023859 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023860 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023861 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023862 saved_wait_return = FALSE;
23863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023865 sourcing_lnum_off = sourcing_lnum;
23866
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023867 if (line_arg != NULL)
23868 {
23869 /* Use eap->arg, split up in parts by line breaks. */
23870 theline = line_arg;
23871 p = vim_strchr(theline, '\n');
23872 if (p == NULL)
23873 line_arg += STRLEN(line_arg);
23874 else
23875 {
23876 *p = NUL;
23877 line_arg = p + 1;
23878 }
23879 }
23880 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 theline = getcmdline(':', 0L, indent);
23882 else
23883 theline = eap->getline(':', eap->cookie, indent);
23884 if (KeyTyped)
23885 lines_left = Rows - 1;
23886 if (theline == NULL)
23887 {
23888 EMSG(_("E126: Missing :endfunction"));
23889 goto erret;
23890 }
23891
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023892 /* Detect line continuation: sourcing_lnum increased more than one. */
23893 if (sourcing_lnum > sourcing_lnum_off + 1)
23894 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23895 else
23896 sourcing_lnum_off = 0;
23897
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 if (skip_until != NULL)
23899 {
23900 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23901 * don't check for ":endfunc". */
23902 if (STRCMP(theline, skip_until) == 0)
23903 {
23904 vim_free(skip_until);
23905 skip_until = NULL;
23906 }
23907 }
23908 else
23909 {
23910 /* skip ':' and blanks*/
23911 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23912 ;
23913
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023914 /* Check for "endfunction". */
23915 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023917 if (line_arg == NULL)
23918 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 break;
23920 }
23921
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023922 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 * at "end". */
23924 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23925 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023926 else if (STRNCMP(p, "if", 2) == 0
23927 || STRNCMP(p, "wh", 2) == 0
23928 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 || STRNCMP(p, "try", 3) == 0)
23930 indent += 2;
23931
23932 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023933 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023935 if (*p == '!')
23936 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023938 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23939 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023941 ++nesting;
23942 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
23944 }
23945
23946 /* Check for ":append" or ":insert". */
23947 p = skip_range(p, NULL);
23948 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23949 || (p[0] == 'i'
23950 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23951 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23952 skip_until = vim_strsave((char_u *)".");
23953
23954 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23955 arg = skipwhite(skiptowhite(p));
23956 if (arg[0] == '<' && arg[1] =='<'
23957 && ((p[0] == 'p' && p[1] == 'y'
23958 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23959 || (p[0] == 'p' && p[1] == 'e'
23960 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23961 || (p[0] == 't' && p[1] == 'c'
23962 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023963 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23964 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23966 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023967 || (p[0] == 'm' && p[1] == 'z'
23968 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 ))
23970 {
23971 /* ":python <<" continues until a dot, like ":append" */
23972 p = skipwhite(arg + 2);
23973 if (*p == NUL)
23974 skip_until = vim_strsave((char_u *)".");
23975 else
23976 skip_until = vim_strsave(p);
23977 }
23978 }
23979
23980 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023981 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023982 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023983 if (line_arg == NULL)
23984 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023986 }
23987
23988 /* Copy the line to newly allocated memory. get_one_sourceline()
23989 * allocates 250 bytes per line, this saves 80% on average. The cost
23990 * is an extra alloc/free. */
23991 p = vim_strsave(theline);
23992 if (p != NULL)
23993 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023994 if (line_arg == NULL)
23995 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023996 theline = p;
23997 }
23998
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023999 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24000
24001 /* Add NULL lines for continuation lines, so that the line count is
24002 * equal to the index in the growarray. */
24003 while (sourcing_lnum_off-- > 0)
24004 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024005
24006 /* Check for end of eap->arg. */
24007 if (line_arg != NULL && *line_arg == NUL)
24008 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 }
24010
24011 /* Don't define the function when skipping commands or when an error was
24012 * detected. */
24013 if (eap->skip || did_emsg)
24014 goto erret;
24015
24016 /*
24017 * If there are no errors, add the function
24018 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024019 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024020 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024021 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024022 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024024 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024025 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024026 goto erret;
24027 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029 fp = find_func(name);
24030 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032 if (!eap->forceit)
24033 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024034 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024035 goto erret;
24036 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024037 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024038 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024039 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024040 name);
24041 goto erret;
24042 }
24043 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024044 ga_clear_strings(&(fp->uf_args));
24045 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024046 vim_free(name);
24047 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 }
24050 else
24051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 char numbuf[20];
24053
24054 fp = NULL;
24055 if (fudi.fd_newkey == NULL && !eap->forceit)
24056 {
24057 EMSG(_(e_funcdict));
24058 goto erret;
24059 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024060 if (fudi.fd_di == NULL)
24061 {
24062 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024063 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024064 goto erret;
24065 }
24066 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024067 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024068 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069
24070 /* Give the function a sequential number. Can only be used with a
24071 * Funcref! */
24072 vim_free(name);
24073 sprintf(numbuf, "%d", ++func_nr);
24074 name = vim_strsave((char_u *)numbuf);
24075 if (name == NULL)
24076 goto erret;
24077 }
24078
24079 if (fp == NULL)
24080 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024081 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024082 {
24083 int slen, plen;
24084 char_u *scriptname;
24085
24086 /* Check that the autoload name matches the script name. */
24087 j = FAIL;
24088 if (sourcing_name != NULL)
24089 {
24090 scriptname = autoload_name(name);
24091 if (scriptname != NULL)
24092 {
24093 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024094 plen = (int)STRLEN(p);
24095 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024096 if (slen > plen && fnamecmp(p,
24097 sourcing_name + slen - plen) == 0)
24098 j = OK;
24099 vim_free(scriptname);
24100 }
24101 }
24102 if (j == FAIL)
24103 {
24104 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24105 goto erret;
24106 }
24107 }
24108
24109 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 if (fp == NULL)
24111 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024112
24113 if (fudi.fd_dict != NULL)
24114 {
24115 if (fudi.fd_di == NULL)
24116 {
24117 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024118 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024119 if (fudi.fd_di == NULL)
24120 {
24121 vim_free(fp);
24122 goto erret;
24123 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024124 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24125 {
24126 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024127 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024128 goto erret;
24129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024130 }
24131 else
24132 /* overwrite existing dict entry */
24133 clear_tv(&fudi.fd_di->di_tv);
24134 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024135 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024136 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024137 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024138
24139 /* behave like "dict" was used */
24140 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024141 }
24142
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024144 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024145 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24146 {
24147 vim_free(fp);
24148 goto erret;
24149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024151 fp->uf_args = newargs;
24152 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024153#ifdef FEAT_PROFILE
24154 fp->uf_tml_count = NULL;
24155 fp->uf_tml_total = NULL;
24156 fp->uf_tml_self = NULL;
24157 fp->uf_profiling = FALSE;
24158 if (prof_def_func())
24159 func_do_profile(fp);
24160#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024161 fp->uf_varargs = varargs;
24162 fp->uf_flags = flags;
24163 fp->uf_calls = 0;
24164 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024165 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166
24167erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 ga_clear_strings(&newargs);
24169 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024170ret_free:
24171 vim_free(skip_until);
24172 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024175 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176}
24177
24178/*
24179 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024180 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024182 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024183 * TFN_INT: internal function name OK
24184 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024185 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 * Advances "pp" to just after the function name (if no error).
24187 */
24188 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024189trans_function_name(
24190 char_u **pp,
24191 int skip, /* only find the end, don't evaluate */
24192 int flags,
24193 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024195 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 char_u *start;
24197 char_u *end;
24198 int lead;
24199 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024201 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202
24203 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024204 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024206
24207 /* Check for hard coded <SNR>: already translated function ID (from a user
24208 * command). */
24209 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24210 && (*pp)[2] == (int)KE_SNR)
24211 {
24212 *pp += 3;
24213 len = get_id_len(pp) + 3;
24214 return vim_strnsave(start, len);
24215 }
24216
24217 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24218 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024219 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024220 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024222
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024223 /* Note that TFN_ flags use the same values as GLV_ flags. */
24224 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024225 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024226 if (end == start)
24227 {
24228 if (!skip)
24229 EMSG(_("E129: Function name required"));
24230 goto theend;
24231 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024232 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024233 {
24234 /*
24235 * Report an invalid expression in braces, unless the expression
24236 * evaluation has been cancelled due to an aborting error, an
24237 * interrupt, or an exception.
24238 */
24239 if (!aborting())
24240 {
24241 if (end != NULL)
24242 EMSG2(_(e_invarg2), start);
24243 }
24244 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024245 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024246 goto theend;
24247 }
24248
24249 if (lv.ll_tv != NULL)
24250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024251 if (fdp != NULL)
24252 {
24253 fdp->fd_dict = lv.ll_dict;
24254 fdp->fd_newkey = lv.ll_newkey;
24255 lv.ll_newkey = NULL;
24256 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024257 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024258 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24259 {
24260 name = vim_strsave(lv.ll_tv->vval.v_string);
24261 *pp = end;
24262 }
24263 else
24264 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024265 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24266 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267 EMSG(_(e_funcref));
24268 else
24269 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024270 name = NULL;
24271 }
24272 goto theend;
24273 }
24274
24275 if (lv.ll_name == NULL)
24276 {
24277 /* Error found, but continue after the function name. */
24278 *pp = end;
24279 goto theend;
24280 }
24281
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024282 /* Check if the name is a Funcref. If so, use the value. */
24283 if (lv.ll_exp_name != NULL)
24284 {
24285 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024286 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024287 if (name == lv.ll_exp_name)
24288 name = NULL;
24289 }
24290 else
24291 {
24292 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024293 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024294 if (name == *pp)
24295 name = NULL;
24296 }
24297 if (name != NULL)
24298 {
24299 name = vim_strsave(name);
24300 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024301 if (STRNCMP(name, "<SNR>", 5) == 0)
24302 {
24303 /* Change "<SNR>" to the byte sequence. */
24304 name[0] = K_SPECIAL;
24305 name[1] = KS_EXTRA;
24306 name[2] = (int)KE_SNR;
24307 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24308 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024309 goto theend;
24310 }
24311
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024312 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024313 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024314 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024315 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24316 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24317 {
24318 /* When there was "s:" already or the name expanded to get a
24319 * leading "s:" then remove it. */
24320 lv.ll_name += 2;
24321 len -= 2;
24322 lead = 2;
24323 }
24324 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024325 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024326 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024327 /* skip over "s:" and "g:" */
24328 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024329 lv.ll_name += 2;
24330 len = (int)(end - lv.ll_name);
24331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024332
24333 /*
24334 * Copy the function name to allocated memory.
24335 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24336 * Accept <SNR>123_name() outside a script.
24337 */
24338 if (skip)
24339 lead = 0; /* do nothing */
24340 else if (lead > 0)
24341 {
24342 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024343 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24344 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024345 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024346 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024347 if (current_SID <= 0)
24348 {
24349 EMSG(_(e_usingsid));
24350 goto theend;
24351 }
24352 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24353 lead += (int)STRLEN(sid_buf);
24354 }
24355 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024356 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024357 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024358 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024359 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024360 goto theend;
24361 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024362 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024363 {
24364 char_u *cp = vim_strchr(lv.ll_name, ':');
24365
24366 if (cp != NULL && cp < end)
24367 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024368 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024369 goto theend;
24370 }
24371 }
24372
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024373 name = alloc((unsigned)(len + lead + 1));
24374 if (name != NULL)
24375 {
24376 if (lead > 0)
24377 {
24378 name[0] = K_SPECIAL;
24379 name[1] = KS_EXTRA;
24380 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024381 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024382 STRCPY(name + 3, sid_buf);
24383 }
24384 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024385 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024386 }
24387 *pp = end;
24388
24389theend:
24390 clear_lval(&lv);
24391 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392}
24393
24394/*
24395 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24396 * Return 2 if "p" starts with "s:".
24397 * Return 0 otherwise.
24398 */
24399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024400eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024402 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24403 * the standard library function. */
24404 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24405 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 return 5;
24407 if (p[0] == 's' && p[1] == ':')
24408 return 2;
24409 return 0;
24410}
24411
24412/*
24413 * Return TRUE if "p" starts with "<SID>" or "s:".
24414 * Only works if eval_fname_script() returned non-zero for "p"!
24415 */
24416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024417eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418{
24419 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24420}
24421
24422/*
24423 * List the head of the function: "name(arg1, arg2)".
24424 */
24425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024426list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427{
24428 int j;
24429
24430 msg_start();
24431 if (indent)
24432 MSG_PUTS(" ");
24433 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024434 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 {
24436 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024437 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438 }
24439 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024440 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024442 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 {
24444 if (j)
24445 MSG_PUTS(", ");
24446 msg_puts(FUNCARG(fp, j));
24447 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024448 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 {
24450 if (j)
24451 MSG_PUTS(", ");
24452 MSG_PUTS("...");
24453 }
24454 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024455 if (fp->uf_flags & FC_ABORT)
24456 MSG_PUTS(" abort");
24457 if (fp->uf_flags & FC_RANGE)
24458 MSG_PUTS(" range");
24459 if (fp->uf_flags & FC_DICT)
24460 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024461 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024462 if (p_verbose > 0)
24463 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464}
24465
24466/*
24467 * Find a function by name, return pointer to it in ufuncs.
24468 * Return NULL for unknown function.
24469 */
24470 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024471find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024472{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024473 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024475 hi = hash_find(&func_hashtab, name);
24476 if (!HASHITEM_EMPTY(hi))
24477 return HI2UF(hi);
24478 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479}
24480
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024481#if defined(EXITFREE) || defined(PROTO)
24482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024483free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024484{
24485 hashitem_T *hi;
24486
24487 /* Need to start all over every time, because func_free() may change the
24488 * hash table. */
24489 while (func_hashtab.ht_used > 0)
24490 for (hi = func_hashtab.ht_array; ; ++hi)
24491 if (!HASHITEM_EMPTY(hi))
24492 {
24493 func_free(HI2UF(hi));
24494 break;
24495 }
24496}
24497#endif
24498
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024500translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024501{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024502 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024503 return find_internal_func(name) >= 0;
24504 return find_func(name) != NULL;
24505}
24506
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024507/*
24508 * Return TRUE if a function "name" exists.
24509 */
24510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024511function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024512{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024513 char_u *nm = name;
24514 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024515 int n = FALSE;
24516
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024517 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24518 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024519 nm = skipwhite(nm);
24520
24521 /* Only accept "funcname", "funcname ", "funcname (..." and
24522 * "funcname(...", not "funcname!...". */
24523 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024524 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024525 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024526 return n;
24527}
24528
Bram Moolenaara1544c02013-05-30 12:35:52 +020024529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024530get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024531{
24532 char_u *nm = name;
24533 char_u *p;
24534
24535 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24536
24537 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024538 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024539 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024540
Bram Moolenaara1544c02013-05-30 12:35:52 +020024541 vim_free(p);
24542 return NULL;
24543}
24544
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024545/*
24546 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024547 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24548 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024549 */
24550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024551builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024552{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024553 char_u *p;
24554
24555 if (!ASCII_ISLOWER(name[0]))
24556 return FALSE;
24557 p = vim_strchr(name, AUTOLOAD_CHAR);
24558 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024559}
24560
Bram Moolenaar05159a02005-02-26 23:04:13 +000024561#if defined(FEAT_PROFILE) || defined(PROTO)
24562/*
24563 * Start profiling function "fp".
24564 */
24565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024566func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024567{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024568 int len = fp->uf_lines.ga_len;
24569
24570 if (len == 0)
24571 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024572 fp->uf_tm_count = 0;
24573 profile_zero(&fp->uf_tm_self);
24574 profile_zero(&fp->uf_tm_total);
24575 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024576 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024577 if (fp->uf_tml_total == NULL)
24578 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024579 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024580 if (fp->uf_tml_self == NULL)
24581 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024582 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024583 fp->uf_tml_idx = -1;
24584 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24585 || fp->uf_tml_self == NULL)
24586 return; /* out of memory */
24587
24588 fp->uf_profiling = TRUE;
24589}
24590
24591/*
24592 * Dump the profiling results for all functions in file "fd".
24593 */
24594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024595func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024596{
24597 hashitem_T *hi;
24598 int todo;
24599 ufunc_T *fp;
24600 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024601 ufunc_T **sorttab;
24602 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024604 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024605 if (todo == 0)
24606 return; /* nothing to dump */
24607
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024608 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024609
Bram Moolenaar05159a02005-02-26 23:04:13 +000024610 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24611 {
24612 if (!HASHITEM_EMPTY(hi))
24613 {
24614 --todo;
24615 fp = HI2UF(hi);
24616 if (fp->uf_profiling)
24617 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024618 if (sorttab != NULL)
24619 sorttab[st_len++] = fp;
24620
Bram Moolenaar05159a02005-02-26 23:04:13 +000024621 if (fp->uf_name[0] == K_SPECIAL)
24622 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24623 else
24624 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24625 if (fp->uf_tm_count == 1)
24626 fprintf(fd, "Called 1 time\n");
24627 else
24628 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24629 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24630 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24631 fprintf(fd, "\n");
24632 fprintf(fd, "count total (s) self (s)\n");
24633
24634 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24635 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024636 if (FUNCLINE(fp, i) == NULL)
24637 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024638 prof_func_line(fd, fp->uf_tml_count[i],
24639 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024640 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24641 }
24642 fprintf(fd, "\n");
24643 }
24644 }
24645 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024646
24647 if (sorttab != NULL && st_len > 0)
24648 {
24649 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24650 prof_total_cmp);
24651 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24652 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24653 prof_self_cmp);
24654 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24655 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024656
24657 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024658}
Bram Moolenaar73830342005-02-28 22:48:19 +000024659
24660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024661prof_sort_list(
24662 FILE *fd,
24663 ufunc_T **sorttab,
24664 int st_len,
24665 char *title,
24666 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024667{
24668 int i;
24669 ufunc_T *fp;
24670
24671 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24672 fprintf(fd, "count total (s) self (s) function\n");
24673 for (i = 0; i < 20 && i < st_len; ++i)
24674 {
24675 fp = sorttab[i];
24676 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24677 prefer_self);
24678 if (fp->uf_name[0] == K_SPECIAL)
24679 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24680 else
24681 fprintf(fd, " %s()\n", fp->uf_name);
24682 }
24683 fprintf(fd, "\n");
24684}
24685
24686/*
24687 * Print the count and times for one function or function line.
24688 */
24689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024690prof_func_line(
24691 FILE *fd,
24692 int count,
24693 proftime_T *total,
24694 proftime_T *self,
24695 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024696{
24697 if (count > 0)
24698 {
24699 fprintf(fd, "%5d ", count);
24700 if (prefer_self && profile_equal(total, self))
24701 fprintf(fd, " ");
24702 else
24703 fprintf(fd, "%s ", profile_msg(total));
24704 if (!prefer_self && profile_equal(total, self))
24705 fprintf(fd, " ");
24706 else
24707 fprintf(fd, "%s ", profile_msg(self));
24708 }
24709 else
24710 fprintf(fd, " ");
24711}
24712
24713/*
24714 * Compare function for total time sorting.
24715 */
24716 static int
24717#ifdef __BORLANDC__
24718_RTLENTRYF
24719#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024720prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024721{
24722 ufunc_T *p1, *p2;
24723
24724 p1 = *(ufunc_T **)s1;
24725 p2 = *(ufunc_T **)s2;
24726 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24727}
24728
24729/*
24730 * Compare function for self time sorting.
24731 */
24732 static int
24733#ifdef __BORLANDC__
24734_RTLENTRYF
24735#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024736prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024737{
24738 ufunc_T *p1, *p2;
24739
24740 p1 = *(ufunc_T **)s1;
24741 p2 = *(ufunc_T **)s2;
24742 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24743}
24744
Bram Moolenaar05159a02005-02-26 23:04:13 +000024745#endif
24746
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024747/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024748 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024749 * Return TRUE if a package was loaded.
24750 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024752script_autoload(
24753 char_u *name,
24754 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024755{
24756 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024757 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024758 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024759 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024760
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024761 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024762 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024763 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024764 return FALSE;
24765
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024766 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024767
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024768 /* Find the name in the list of previously loaded package names. Skip
24769 * "autoload/", it's always the same. */
24770 for (i = 0; i < ga_loaded.ga_len; ++i)
24771 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24772 break;
24773 if (!reload && i < ga_loaded.ga_len)
24774 ret = FALSE; /* was loaded already */
24775 else
24776 {
24777 /* Remember the name if it wasn't loaded already. */
24778 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24779 {
24780 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24781 tofree = NULL;
24782 }
24783
24784 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024785 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024786 ret = TRUE;
24787 }
24788
24789 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024790 return ret;
24791}
24792
24793/*
24794 * Return the autoload script name for a function or variable name.
24795 * Returns NULL when out of memory.
24796 */
24797 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024798autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024799{
24800 char_u *p;
24801 char_u *scriptname;
24802
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024803 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024804 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24805 if (scriptname == NULL)
24806 return FALSE;
24807 STRCPY(scriptname, "autoload/");
24808 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024809 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024810 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024811 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024812 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024813 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024814}
24815
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24817
24818/*
24819 * Function given to ExpandGeneric() to obtain the list of user defined
24820 * function names.
24821 */
24822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024823get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024825 static long_u done;
24826 static hashitem_T *hi;
24827 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828
24829 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024831 done = 0;
24832 hi = func_hashtab.ht_array;
24833 }
24834 if (done < func_hashtab.ht_used)
24835 {
24836 if (done++ > 0)
24837 ++hi;
24838 while (HASHITEM_EMPTY(hi))
24839 ++hi;
24840 fp = HI2UF(hi);
24841
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024842 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024843 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024845 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24846 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847
24848 cat_func_name(IObuff, fp);
24849 if (xp->xp_context != EXPAND_USER_FUNC)
24850 {
24851 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024852 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 STRCAT(IObuff, ")");
24854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855 return IObuff;
24856 }
24857 return NULL;
24858}
24859
24860#endif /* FEAT_CMDL_COMPL */
24861
24862/*
24863 * Copy the function name of "fp" to buffer "buf".
24864 * "buf" must be able to hold the function name plus three bytes.
24865 * Takes care of script-local function names.
24866 */
24867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024868cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024870 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 {
24872 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024873 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 }
24875 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024876 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877}
24878
24879/*
24880 * ":delfunction {name}"
24881 */
24882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024883ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024885 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 char_u *p;
24887 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024888 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889
24890 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024891 name = trans_function_name(&p, eap->skip, 0, &fudi);
24892 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024893 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024894 {
24895 if (fudi.fd_dict != NULL && !eap->skip)
24896 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 if (!ends_excmd(*skipwhite(p)))
24900 {
24901 vim_free(name);
24902 EMSG(_(e_trailing));
24903 return;
24904 }
24905 eap->nextcmd = check_nextcmd(p);
24906 if (eap->nextcmd != NULL)
24907 *p = NUL;
24908
24909 if (!eap->skip)
24910 fp = find_func(name);
24911 vim_free(name);
24912
24913 if (!eap->skip)
24914 {
24915 if (fp == NULL)
24916 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024917 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 return;
24919 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024920 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 {
24922 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24923 return;
24924 }
24925
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024926 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024928 /* Delete the dict item that refers to the function, it will
24929 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024930 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024932 else
24933 func_free(fp);
24934 }
24935}
24936
24937/*
24938 * Free a function and remove it from the list of functions.
24939 */
24940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024941func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024943 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024944
24945 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024946 ga_clear_strings(&(fp->uf_args));
24947 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024948#ifdef FEAT_PROFILE
24949 vim_free(fp->uf_tml_count);
24950 vim_free(fp->uf_tml_total);
24951 vim_free(fp->uf_tml_self);
24952#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024953
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024954 /* remove the function from the function hashtable */
24955 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24956 if (HASHITEM_EMPTY(hi))
24957 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024958 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024959 hash_remove(&func_hashtab, hi);
24960
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024961 vim_free(fp);
24962}
24963
24964/*
24965 * Unreference a Function: decrement the reference count and free it when it
24966 * becomes zero. Only for numbered functions.
24967 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024970{
24971 ufunc_T *fp;
24972
24973 if (name != NULL && isdigit(*name))
24974 {
24975 fp = find_func(name);
24976 if (fp == NULL)
24977 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024978 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024979 {
24980 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024981 * when "uf_calls" becomes zero. */
24982 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024983 func_free(fp);
24984 }
24985 }
24986}
24987
24988/*
24989 * Count a reference to a Function.
24990 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024992func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024993{
24994 ufunc_T *fp;
24995
24996 if (name != NULL && isdigit(*name))
24997 {
24998 fp = find_func(name);
24999 if (fp == NULL)
25000 EMSG2(_(e_intern2), "func_ref()");
25001 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025002 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 }
25004}
25005
25006/*
25007 * Call a user function.
25008 */
25009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025010call_user_func(
25011 ufunc_T *fp, /* pointer to function */
25012 int argcount, /* nr of args */
25013 typval_T *argvars, /* arguments */
25014 typval_T *rettv, /* return value */
25015 linenr_T firstline, /* first line of range */
25016 linenr_T lastline, /* last line of range */
25017 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018{
Bram Moolenaar33570922005-01-25 22:26:29 +000025019 char_u *save_sourcing_name;
25020 linenr_T save_sourcing_lnum;
25021 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025022 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 int save_did_emsg;
25024 static int depth = 0;
25025 dictitem_T *v;
25026 int fixvar_idx = 0; /* index in fixvar[] */
25027 int i;
25028 int ai;
25029 char_u numbuf[NUMBUFLEN];
25030 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025031 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025032#ifdef FEAT_PROFILE
25033 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025034 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036
25037 /* If depth of calling is getting too high, don't execute the function */
25038 if (depth >= p_mfd)
25039 {
25040 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025041 rettv->v_type = VAR_NUMBER;
25042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 return;
25044 }
25045 ++depth;
25046
25047 line_breakcheck(); /* check for CTRL-C hit */
25048
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025049 fc = (funccall_T *)alloc(sizeof(funccall_T));
25050 fc->caller = current_funccal;
25051 current_funccal = fc;
25052 fc->func = fp;
25053 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025054 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025055 fc->linenr = 0;
25056 fc->returned = FALSE;
25057 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025059 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25060 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061
Bram Moolenaar33570922005-01-25 22:26:29 +000025062 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025063 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025064 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25065 * each argument variable and saves a lot of time.
25066 */
25067 /*
25068 * Init l: variables.
25069 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025070 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025071 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025072 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025073 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25074 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025075 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025076 name = v->di_key;
25077 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025078 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025079 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025080 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025081 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025082 v->di_tv.vval.v_dict = selfdict;
25083 ++selfdict->dv_refcount;
25084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025085
Bram Moolenaar33570922005-01-25 22:26:29 +000025086 /*
25087 * Init a: variables.
25088 * Set a:0 to "argcount".
25089 * Set a:000 to a list with room for the "..." arguments.
25090 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025091 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025092 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025093 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025094 /* Use "name" to avoid a warning from some compiler that checks the
25095 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025096 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025097 name = v->di_key;
25098 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025099 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025100 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025101 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025102 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025103 v->di_tv.vval.v_list = &fc->l_varlist;
25104 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25105 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25106 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025107
25108 /*
25109 * Set a:firstline to "firstline" and a:lastline to "lastline".
25110 * Set a:name to named arguments.
25111 * Set a:N to the "..." arguments.
25112 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025113 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025114 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025115 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025116 (varnumber_T)lastline);
25117 for (i = 0; i < argcount; ++i)
25118 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025119 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025120 if (ai < 0)
25121 /* named argument a:name */
25122 name = FUNCARG(fp, i);
25123 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025124 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025125 /* "..." argument a:1, a:2, etc. */
25126 sprintf((char *)numbuf, "%d", ai + 1);
25127 name = numbuf;
25128 }
25129 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25130 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025131 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025132 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25133 }
25134 else
25135 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025136 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25137 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025138 if (v == NULL)
25139 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025140 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025141 }
25142 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025143 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025144
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025145 /* Note: the values are copied directly to avoid alloc/free.
25146 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025147 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025148 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025149
25150 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25151 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025152 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25153 fc->l_listitems[ai].li_tv = argvars[i];
25154 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025155 }
25156 }
25157
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158 /* Don't redraw while executing the function. */
25159 ++RedrawingDisabled;
25160 save_sourcing_name = sourcing_name;
25161 save_sourcing_lnum = sourcing_lnum;
25162 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025163 /* need space for function name + ("function " + 3) or "[number]" */
25164 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25165 + STRLEN(fp->uf_name) + 20;
25166 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 if (sourcing_name != NULL)
25168 {
25169 if (save_sourcing_name != NULL
25170 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025171 sprintf((char *)sourcing_name, "%s[%d]..",
25172 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173 else
25174 STRCPY(sourcing_name, "function ");
25175 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25176
25177 if (p_verbose >= 12)
25178 {
25179 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025180 verbose_enter_scroll();
25181
Bram Moolenaar555b2802005-05-19 21:08:39 +000025182 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 if (p_verbose >= 14)
25184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025186 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025187 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025188 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189
25190 msg_puts((char_u *)"(");
25191 for (i = 0; i < argcount; ++i)
25192 {
25193 if (i > 0)
25194 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025195 if (argvars[i].v_type == VAR_NUMBER)
25196 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 else
25198 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025199 /* Do not want errors such as E724 here. */
25200 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025201 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025202 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025203 if (s != NULL)
25204 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025205 if (vim_strsize(s) > MSG_BUF_CLEN)
25206 {
25207 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25208 s = buf;
25209 }
25210 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025211 vim_free(tofree);
25212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 }
25214 }
25215 msg_puts((char_u *)")");
25216 }
25217 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025218
25219 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220 --no_wait_return;
25221 }
25222 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025223#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025224 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025225 {
25226 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25227 func_do_profile(fp);
25228 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025229 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025230 {
25231 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025232 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025233 profile_zero(&fp->uf_tm_children);
25234 }
25235 script_prof_save(&wait_start);
25236 }
25237#endif
25238
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025240 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 save_did_emsg = did_emsg;
25242 did_emsg = FALSE;
25243
25244 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025245 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25247
25248 --RedrawingDisabled;
25249
25250 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025251 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025253 clear_tv(rettv);
25254 rettv->v_type = VAR_NUMBER;
25255 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 }
25257
Bram Moolenaar05159a02005-02-26 23:04:13 +000025258#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025259 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025260 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025261 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025262 profile_end(&call_start);
25263 profile_sub_wait(&wait_start, &call_start);
25264 profile_add(&fp->uf_tm_total, &call_start);
25265 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025266 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025267 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025268 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25269 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025270 }
25271 }
25272#endif
25273
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 /* when being verbose, mention the return value */
25275 if (p_verbose >= 12)
25276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025278 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025281 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025282 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025283 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025284 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025285 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025287 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025288 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025289 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025290 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025291
Bram Moolenaar555b2802005-05-19 21:08:39 +000025292 /* The value may be very long. Skip the middle part, so that we
25293 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025294 * truncate it at the end. Don't want errors such as E724 here. */
25295 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025296 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025297 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025298 if (s != NULL)
25299 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025300 if (vim_strsize(s) > MSG_BUF_CLEN)
25301 {
25302 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25303 s = buf;
25304 }
25305 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025306 vim_free(tofree);
25307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 }
25309 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025310
25311 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 --no_wait_return;
25313 }
25314
25315 vim_free(sourcing_name);
25316 sourcing_name = save_sourcing_name;
25317 sourcing_lnum = save_sourcing_lnum;
25318 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025319#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025320 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025321 script_prof_restore(&wait_start);
25322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323
25324 if (p_verbose >= 12 && sourcing_name != NULL)
25325 {
25326 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025327 verbose_enter_scroll();
25328
Bram Moolenaar555b2802005-05-19 21:08:39 +000025329 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025331
25332 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 --no_wait_return;
25334 }
25335
25336 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025337 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025339
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025340 /* If the a:000 list and the l: and a: dicts are not referenced we can
25341 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025342 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25343 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25344 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25345 {
25346 free_funccal(fc, FALSE);
25347 }
25348 else
25349 {
25350 hashitem_T *hi;
25351 listitem_T *li;
25352 int todo;
25353
25354 /* "fc" is still in use. This can happen when returning "a:000" or
25355 * assigning "l:" to a global variable.
25356 * Link "fc" in the list for garbage collection later. */
25357 fc->caller = previous_funccal;
25358 previous_funccal = fc;
25359
25360 /* Make a copy of the a: variables, since we didn't do that above. */
25361 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25362 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25363 {
25364 if (!HASHITEM_EMPTY(hi))
25365 {
25366 --todo;
25367 v = HI2DI(hi);
25368 copy_tv(&v->di_tv, &v->di_tv);
25369 }
25370 }
25371
25372 /* Make a copy of the a:000 items, since we didn't do that above. */
25373 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25374 copy_tv(&li->li_tv, &li->li_tv);
25375 }
25376}
25377
25378/*
25379 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025380 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025381 */
25382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025383can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025384{
25385 return (fc->l_varlist.lv_copyID != copyID
25386 && fc->l_vars.dv_copyID != copyID
25387 && fc->l_avars.dv_copyID != copyID);
25388}
25389
25390/*
25391 * Free "fc" and what it contains.
25392 */
25393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025394free_funccal(
25395 funccall_T *fc,
25396 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025397{
25398 listitem_T *li;
25399
25400 /* The a: variables typevals may not have been allocated, only free the
25401 * allocated variables. */
25402 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25403
25404 /* free all l: variables */
25405 vars_clear(&fc->l_vars.dv_hashtab);
25406
25407 /* Free the a:000 variables if they were allocated. */
25408 if (free_val)
25409 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25410 clear_tv(&li->li_tv);
25411
25412 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413}
25414
25415/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025416 * Add a number variable "name" to dict "dp" with value "nr".
25417 */
25418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025419add_nr_var(
25420 dict_T *dp,
25421 dictitem_T *v,
25422 char *name,
25423 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025424{
25425 STRCPY(v->di_key, name);
25426 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25427 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25428 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025429 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025430 v->di_tv.vval.v_number = nr;
25431}
25432
25433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 * ":return [expr]"
25435 */
25436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025437ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438{
25439 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025440 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 int returning = FALSE;
25442
25443 if (current_funccal == NULL)
25444 {
25445 EMSG(_("E133: :return not inside a function"));
25446 return;
25447 }
25448
25449 if (eap->skip)
25450 ++emsg_skip;
25451
25452 eap->nextcmd = NULL;
25453 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025454 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 {
25456 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025457 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025459 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 }
25461 /* It's safer to return also on error. */
25462 else if (!eap->skip)
25463 {
25464 /*
25465 * Return unless the expression evaluation has been cancelled due to an
25466 * aborting error, an interrupt, or an exception.
25467 */
25468 if (!aborting())
25469 returning = do_return(eap, FALSE, TRUE, NULL);
25470 }
25471
25472 /* When skipping or the return gets pending, advance to the next command
25473 * in this line (!returning). Otherwise, ignore the rest of the line.
25474 * Following lines will be ignored by get_func_line(). */
25475 if (returning)
25476 eap->nextcmd = NULL;
25477 else if (eap->nextcmd == NULL) /* no argument */
25478 eap->nextcmd = check_nextcmd(arg);
25479
25480 if (eap->skip)
25481 --emsg_skip;
25482}
25483
25484/*
25485 * Return from a function. Possibly makes the return pending. Also called
25486 * for a pending return at the ":endtry" or after returning from an extra
25487 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025488 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025489 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 * FALSE when the return gets pending.
25491 */
25492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025493do_return(
25494 exarg_T *eap,
25495 int reanimate,
25496 int is_cmd,
25497 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498{
25499 int idx;
25500 struct condstack *cstack = eap->cstack;
25501
25502 if (reanimate)
25503 /* Undo the return. */
25504 current_funccal->returned = FALSE;
25505
25506 /*
25507 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25508 * not in its finally clause (which then is to be executed next) is found.
25509 * In this case, make the ":return" pending for execution at the ":endtry".
25510 * Otherwise, return normally.
25511 */
25512 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25513 if (idx >= 0)
25514 {
25515 cstack->cs_pending[idx] = CSTP_RETURN;
25516
25517 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025518 /* A pending return again gets pending. "rettv" points to an
25519 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025521 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 else
25523 {
25524 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025525 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025527 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025529 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 {
25531 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025532 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025533 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534 else
25535 EMSG(_(e_outofmem));
25536 }
25537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025538 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539
25540 if (reanimate)
25541 {
25542 /* The pending return value could be overwritten by a ":return"
25543 * without argument in a finally clause; reset the default
25544 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025545 current_funccal->rettv->v_type = VAR_NUMBER;
25546 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 }
25548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025549 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025550 }
25551 else
25552 {
25553 current_funccal->returned = TRUE;
25554
25555 /* If the return is carried out now, store the return value. For
25556 * a return immediately after reanimation, the value is already
25557 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025558 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025560 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025561 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025563 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 }
25565 }
25566
25567 return idx < 0;
25568}
25569
25570/*
25571 * Free the variable with a pending return value.
25572 */
25573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025574discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575{
Bram Moolenaar33570922005-01-25 22:26:29 +000025576 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577}
25578
25579/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025580 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 * is an allocated string. Used by report_pending() for verbose messages.
25582 */
25583 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025584get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025586 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025587 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025588 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025590 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025591 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025592 if (s == NULL)
25593 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025594
25595 STRCPY(IObuff, ":return ");
25596 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25597 if (STRLEN(s) + 8 >= IOSIZE)
25598 STRCPY(IObuff + IOSIZE - 4, "...");
25599 vim_free(tofree);
25600 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601}
25602
25603/*
25604 * Get next function line.
25605 * Called by do_cmdline() to get the next line.
25606 * Returns allocated string, or NULL for end of function.
25607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025609get_func_line(
25610 int c UNUSED,
25611 void *cookie,
25612 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613{
Bram Moolenaar33570922005-01-25 22:26:29 +000025614 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025615 ufunc_T *fp = fcp->func;
25616 char_u *retval;
25617 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 /* If breakpoints have been added/deleted need to check for it. */
25620 if (fcp->dbg_tick != debug_tick)
25621 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025622 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 sourcing_lnum);
25624 fcp->dbg_tick = debug_tick;
25625 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025626#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025627 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025628 func_line_end(cookie);
25629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630
Bram Moolenaar05159a02005-02-26 23:04:13 +000025631 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025632 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25633 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 retval = NULL;
25635 else
25636 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025637 /* Skip NULL lines (continuation lines). */
25638 while (fcp->linenr < gap->ga_len
25639 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25640 ++fcp->linenr;
25641 if (fcp->linenr >= gap->ga_len)
25642 retval = NULL;
25643 else
25644 {
25645 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25646 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025648 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025649 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025650#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 }
25653
25654 /* Did we encounter a breakpoint? */
25655 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25656 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025657 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025659 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 sourcing_lnum);
25661 fcp->dbg_tick = debug_tick;
25662 }
25663
25664 return retval;
25665}
25666
Bram Moolenaar05159a02005-02-26 23:04:13 +000025667#if defined(FEAT_PROFILE) || defined(PROTO)
25668/*
25669 * Called when starting to read a function line.
25670 * "sourcing_lnum" must be correct!
25671 * When skipping lines it may not actually be executed, but we won't find out
25672 * until later and we need to store the time now.
25673 */
25674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025675func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025676{
25677 funccall_T *fcp = (funccall_T *)cookie;
25678 ufunc_T *fp = fcp->func;
25679
25680 if (fp->uf_profiling && sourcing_lnum >= 1
25681 && sourcing_lnum <= fp->uf_lines.ga_len)
25682 {
25683 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025684 /* Skip continuation lines. */
25685 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25686 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025687 fp->uf_tml_execed = FALSE;
25688 profile_start(&fp->uf_tml_start);
25689 profile_zero(&fp->uf_tml_children);
25690 profile_get_wait(&fp->uf_tml_wait);
25691 }
25692}
25693
25694/*
25695 * Called when actually executing a function line.
25696 */
25697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025698func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699{
25700 funccall_T *fcp = (funccall_T *)cookie;
25701 ufunc_T *fp = fcp->func;
25702
25703 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25704 fp->uf_tml_execed = TRUE;
25705}
25706
25707/*
25708 * Called when done with a function line.
25709 */
25710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025711func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712{
25713 funccall_T *fcp = (funccall_T *)cookie;
25714 ufunc_T *fp = fcp->func;
25715
25716 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25717 {
25718 if (fp->uf_tml_execed)
25719 {
25720 ++fp->uf_tml_count[fp->uf_tml_idx];
25721 profile_end(&fp->uf_tml_start);
25722 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025723 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025724 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25725 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025726 }
25727 fp->uf_tml_idx = -1;
25728 }
25729}
25730#endif
25731
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732/*
25733 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025734 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735 */
25736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025737func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738{
Bram Moolenaar33570922005-01-25 22:26:29 +000025739 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
25741 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25742 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025743 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 || fcp->returned);
25745}
25746
25747/*
25748 * return TRUE if cookie indicates a function which "abort"s on errors.
25749 */
25750 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025751func_has_abort(
25752 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025754 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755}
25756
25757#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25758typedef enum
25759{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025760 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25761 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25762 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763} var_flavour_T;
25764
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025765static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766
25767 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025768var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769{
25770 char_u *p = varname;
25771
25772 if (ASCII_ISUPPER(*p))
25773 {
25774 while (*(++p))
25775 if (ASCII_ISLOWER(*p))
25776 return VAR_FLAVOUR_SESSION;
25777 return VAR_FLAVOUR_VIMINFO;
25778 }
25779 else
25780 return VAR_FLAVOUR_DEFAULT;
25781}
25782#endif
25783
25784#if defined(FEAT_VIMINFO) || defined(PROTO)
25785/*
25786 * Restore global vars that start with a capital from the viminfo file
25787 */
25788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025789read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790{
25791 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025792 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025793 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025794 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795
25796 if (!writing && (find_viminfo_parameter('!') != NULL))
25797 {
25798 tab = vim_strchr(virp->vir_line + 1, '\t');
25799 if (tab != NULL)
25800 {
25801 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025802 switch (*tab)
25803 {
25804 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025805#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025806 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025807#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025808 case 'D': type = VAR_DICT; break;
25809 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025810 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812
25813 tab = vim_strchr(tab, '\t');
25814 if (tab != NULL)
25815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025816 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025817 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025818 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025820#ifdef FEAT_FLOAT
25821 else if (type == VAR_FLOAT)
25822 (void)string2float(tab + 1, &tv.vval.v_float);
25823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025825 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025826 if (type == VAR_DICT || type == VAR_LIST)
25827 {
25828 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25829
25830 if (etv == NULL)
25831 /* Failed to parse back the dict or list, use it as a
25832 * string. */
25833 tv.v_type = VAR_STRING;
25834 else
25835 {
25836 vim_free(tv.vval.v_string);
25837 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025838 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025839 }
25840 }
25841
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025842 /* when in a function use global variables */
25843 save_funccal = current_funccal;
25844 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025845 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025846 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025847
25848 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025849 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025850 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25851 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 }
25853 }
25854 }
25855
25856 return viminfo_readline(virp);
25857}
25858
25859/*
25860 * Write global vars that start with a capital to the viminfo file
25861 */
25862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025863write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864{
Bram Moolenaar33570922005-01-25 22:26:29 +000025865 hashitem_T *hi;
25866 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025867 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025868 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025869 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025870 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025871 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872
25873 if (find_viminfo_parameter('!') == NULL)
25874 return;
25875
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025876 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025877
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025878 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025879 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025881 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025883 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025884 this_var = HI2DI(hi);
25885 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025886 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025887 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025888 {
25889 case VAR_STRING: s = "STR"; break;
25890 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025891 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025892 case VAR_DICT: s = "DIC"; break;
25893 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025894 case VAR_SPECIAL: s = "XPL"; break;
25895
25896 case VAR_UNKNOWN:
25897 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025898 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025899 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025900 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025901 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025902 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025903 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025904 if (p != NULL)
25905 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025906 vim_free(tofree);
25907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 }
25909 }
25910}
25911#endif
25912
25913#if defined(FEAT_SESSION) || defined(PROTO)
25914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025915store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025916{
Bram Moolenaar33570922005-01-25 22:26:29 +000025917 hashitem_T *hi;
25918 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025919 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 char_u *p, *t;
25921
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025922 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025923 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025925 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025927 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025928 this_var = HI2DI(hi);
25929 if ((this_var->di_tv.v_type == VAR_NUMBER
25930 || this_var->di_tv.v_type == VAR_STRING)
25931 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025932 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025933 /* Escape special characters with a backslash. Turn a LF and
25934 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025935 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025936 (char_u *)"\\\"\n\r");
25937 if (p == NULL) /* out of memory */
25938 break;
25939 for (t = p; *t != NUL; ++t)
25940 if (*t == '\n')
25941 *t = 'n';
25942 else if (*t == '\r')
25943 *t = 'r';
25944 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025945 this_var->di_key,
25946 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25947 : ' ',
25948 p,
25949 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25950 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025951 || put_eol(fd) == FAIL)
25952 {
25953 vim_free(p);
25954 return FAIL;
25955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 vim_free(p);
25957 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025958#ifdef FEAT_FLOAT
25959 else if (this_var->di_tv.v_type == VAR_FLOAT
25960 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25961 {
25962 float_T f = this_var->di_tv.vval.v_float;
25963 int sign = ' ';
25964
25965 if (f < 0)
25966 {
25967 f = -f;
25968 sign = '-';
25969 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025970 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025971 this_var->di_key, sign, f) < 0)
25972 || put_eol(fd) == FAIL)
25973 return FAIL;
25974 }
25975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 }
25977 }
25978 return OK;
25979}
25980#endif
25981
Bram Moolenaar661b1822005-07-28 22:36:45 +000025982/*
25983 * Display script name where an item was last set.
25984 * Should only be invoked when 'verbose' is non-zero.
25985 */
25986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025987last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025988{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025989 char_u *p;
25990
Bram Moolenaar661b1822005-07-28 22:36:45 +000025991 if (scriptID != 0)
25992 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025993 p = home_replace_save(NULL, get_scriptname(scriptID));
25994 if (p != NULL)
25995 {
25996 verbose_enter();
25997 MSG_PUTS(_("\n\tLast set from "));
25998 MSG_PUTS(p);
25999 vim_free(p);
26000 verbose_leave();
26001 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026002 }
26003}
26004
Bram Moolenaard812df62008-11-09 12:46:09 +000026005/*
26006 * List v:oldfiles in a nice way.
26007 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026009ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026010{
26011 list_T *l = vimvars[VV_OLDFILES].vv_list;
26012 listitem_T *li;
26013 int nr = 0;
26014
26015 if (l == NULL)
26016 msg((char_u *)_("No old files"));
26017 else
26018 {
26019 msg_start();
26020 msg_scroll = TRUE;
26021 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26022 {
26023 msg_outnum((long)++nr);
26024 MSG_PUTS(": ");
26025 msg_outtrans(get_tv_string(&li->li_tv));
26026 msg_putchar('\n');
26027 out_flush(); /* output one line at a time */
26028 ui_breakcheck();
26029 }
26030 /* Assume "got_int" was set to truncate the listing. */
26031 got_int = FALSE;
26032
26033#ifdef FEAT_BROWSE_CMD
26034 if (cmdmod.browse)
26035 {
26036 quit_more = FALSE;
26037 nr = prompt_for_number(FALSE);
26038 msg_starthere();
26039 if (nr > 0)
26040 {
26041 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26042 (long)nr);
26043
26044 if (p != NULL)
26045 {
26046 p = expand_env_save(p);
26047 eap->arg = p;
26048 eap->cmdidx = CMD_edit;
26049 cmdmod.browse = FALSE;
26050 do_exedit(eap, NULL);
26051 vim_free(p);
26052 }
26053 }
26054 }
26055#endif
26056 }
26057}
26058
Bram Moolenaar53744302015-07-17 17:38:22 +020026059/* reset v:option_new, v:option_old and v:option_type */
26060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026061reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026062{
26063 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26064 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26065 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26066}
26067
26068
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069#endif /* FEAT_EVAL */
26070
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026072#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073
26074#ifdef WIN3264
26075/*
26076 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26077 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026078static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26079static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26080static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081
26082/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026083 * Get the short path (8.3) for the filename in "fnamep".
26084 * Only works for a valid file name.
26085 * When the path gets longer "fnamep" is changed and the allocated buffer
26086 * is put in "bufp".
26087 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26088 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089 */
26090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026091get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026093 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 char_u *newbuf;
26095
26096 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026097 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098 if (l > len - 1)
26099 {
26100 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026101 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 newbuf = vim_strnsave(*fnamep, l);
26103 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026104 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105
26106 vim_free(*bufp);
26107 *fnamep = *bufp = newbuf;
26108
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026109 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026110 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 }
26112
26113 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026114 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115}
26116
26117/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026118 * Get the short path (8.3) for the filename in "fname". The converted
26119 * path is returned in "bufp".
26120 *
26121 * Some of the directories specified in "fname" may not exist. This function
26122 * will shorten the existing directories at the beginning of the path and then
26123 * append the remaining non-existing path.
26124 *
26125 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026126 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026127 * bufp - Pointer to an allocated buffer for the filename.
26128 * fnamelen - Length of the filename pointed to by fname
26129 *
26130 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 */
26132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026133shortpath_for_invalid_fname(
26134 char_u **fname,
26135 char_u **bufp,
26136 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026138 char_u *short_fname, *save_fname, *pbuf_unused;
26139 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026141 int old_len, len;
26142 int new_len, sfx_len;
26143 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026146 old_len = *fnamelen;
26147 save_fname = vim_strnsave(*fname, old_len);
26148 pbuf_unused = NULL;
26149 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026151 endp = save_fname + old_len - 1; /* Find the end of the copy */
26152 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026153
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026154 /*
26155 * Try shortening the supplied path till it succeeds by removing one
26156 * directory at a time from the tail of the path.
26157 */
26158 len = 0;
26159 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 /* go back one path-separator */
26162 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26163 --endp;
26164 if (endp <= save_fname)
26165 break; /* processed the complete path */
26166
26167 /*
26168 * Replace the path separator with a NUL and try to shorten the
26169 * resulting path.
26170 */
26171 ch = *endp;
26172 *endp = 0;
26173 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026174 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026175 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26176 {
26177 retval = FAIL;
26178 goto theend;
26179 }
26180 *endp = ch; /* preserve the string */
26181
26182 if (len > 0)
26183 break; /* successfully shortened the path */
26184
26185 /* failed to shorten the path. Skip the path separator */
26186 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187 }
26188
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026189 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026191 /*
26192 * Succeeded in shortening the path. Now concatenate the shortened
26193 * path with the remaining path at the tail.
26194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026196 /* Compute the length of the new path. */
26197 sfx_len = (int)(save_endp - endp) + 1;
26198 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026199
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026200 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026202 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026204 /* There is not enough space in the currently allocated string,
26205 * copy it to a buffer big enough. */
26206 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026208 {
26209 retval = FAIL;
26210 goto theend;
26211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212 }
26213 else
26214 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026215 /* Transfer short_fname to the main buffer (it's big enough),
26216 * unless get_short_pathname() did its work in-place. */
26217 *fname = *bufp = save_fname;
26218 if (short_fname != save_fname)
26219 vim_strncpy(save_fname, short_fname, len);
26220 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026222
26223 /* concat the not-shortened part of the path */
26224 vim_strncpy(*fname + len, endp, sfx_len);
26225 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026227
26228theend:
26229 vim_free(pbuf_unused);
26230 vim_free(save_fname);
26231
26232 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026233}
26234
26235/*
26236 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026237 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 */
26239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026240shortpath_for_partial(
26241 char_u **fnamep,
26242 char_u **bufp,
26243 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244{
26245 int sepcount, len, tflen;
26246 char_u *p;
26247 char_u *pbuf, *tfname;
26248 int hasTilde;
26249
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026250 /* Count up the path separators from the RHS.. so we know which part
26251 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026253 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026254 if (vim_ispathsep(*p))
26255 ++sepcount;
26256
26257 /* Need full path first (use expand_env() to remove a "~/") */
26258 hasTilde = (**fnamep == '~');
26259 if (hasTilde)
26260 pbuf = tfname = expand_env_save(*fnamep);
26261 else
26262 pbuf = tfname = FullName_save(*fnamep, FALSE);
26263
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026264 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026266 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026268
26269 if (len == 0)
26270 {
26271 /* Don't have a valid filename, so shorten the rest of the
26272 * path if we can. This CAN give us invalid 8.3 filenames, but
26273 * there's not a lot of point in guessing what it might be.
26274 */
26275 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026278 }
26279
26280 /* Count the paths backward to find the beginning of the desired string. */
26281 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026282 {
26283#ifdef FEAT_MBYTE
26284 if (has_mbyte)
26285 p -= mb_head_off(tfname, p);
26286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287 if (vim_ispathsep(*p))
26288 {
26289 if (sepcount == 0 || (hasTilde && sepcount == 1))
26290 break;
26291 else
26292 sepcount --;
26293 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 if (hasTilde)
26296 {
26297 --p;
26298 if (p >= tfname)
26299 *p = '~';
26300 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026301 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302 }
26303 else
26304 ++p;
26305
26306 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26307 vim_free(*bufp);
26308 *fnamelen = (int)STRLEN(p);
26309 *bufp = pbuf;
26310 *fnamep = p;
26311
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026312 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026313}
26314#endif /* WIN3264 */
26315
26316/*
26317 * Adjust a filename, according to a string of modifiers.
26318 * *fnamep must be NUL terminated when called. When returning, the length is
26319 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026320 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321 * When there is an error, *fnamep is set to NULL.
26322 */
26323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026324modify_fname(
26325 char_u *src, /* string with modifiers */
26326 int *usedlen, /* characters after src that are used */
26327 char_u **fnamep, /* file name so far */
26328 char_u **bufp, /* buffer for allocated file name or NULL */
26329 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330{
26331 int valid = 0;
26332 char_u *tail;
26333 char_u *s, *p, *pbuf;
26334 char_u dirname[MAXPATHL];
26335 int c;
26336 int has_fullname = 0;
26337#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026338 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 int has_shortname = 0;
26340#endif
26341
26342repeat:
26343 /* ":p" - full path/file_name */
26344 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26345 {
26346 has_fullname = 1;
26347
26348 valid |= VALID_PATH;
26349 *usedlen += 2;
26350
26351 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26352 if ((*fnamep)[0] == '~'
26353#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26354 && ((*fnamep)[1] == '/'
26355# ifdef BACKSLASH_IN_FILENAME
26356 || (*fnamep)[1] == '\\'
26357# endif
26358 || (*fnamep)[1] == NUL)
26359
26360#endif
26361 )
26362 {
26363 *fnamep = expand_env_save(*fnamep);
26364 vim_free(*bufp); /* free any allocated file name */
26365 *bufp = *fnamep;
26366 if (*fnamep == NULL)
26367 return -1;
26368 }
26369
26370 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026371 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372 {
26373 if (vim_ispathsep(*p)
26374 && p[1] == '.'
26375 && (p[2] == NUL
26376 || vim_ispathsep(p[2])
26377 || (p[2] == '.'
26378 && (p[3] == NUL || vim_ispathsep(p[3])))))
26379 break;
26380 }
26381
26382 /* FullName_save() is slow, don't use it when not needed. */
26383 if (*p != NUL || !vim_isAbsName(*fnamep))
26384 {
26385 *fnamep = FullName_save(*fnamep, *p != NUL);
26386 vim_free(*bufp); /* free any allocated file name */
26387 *bufp = *fnamep;
26388 if (*fnamep == NULL)
26389 return -1;
26390 }
26391
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026392#ifdef WIN3264
26393# if _WIN32_WINNT >= 0x0500
26394 if (vim_strchr(*fnamep, '~') != NULL)
26395 {
26396 /* Expand 8.3 filename to full path. Needed to make sure the same
26397 * file does not have two different names.
26398 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26399 p = alloc(_MAX_PATH + 1);
26400 if (p != NULL)
26401 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026402 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026403 {
26404 vim_free(*bufp);
26405 *bufp = *fnamep = p;
26406 }
26407 else
26408 vim_free(p);
26409 }
26410 }
26411# endif
26412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413 /* Append a path separator to a directory. */
26414 if (mch_isdir(*fnamep))
26415 {
26416 /* Make room for one or two extra characters. */
26417 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26418 vim_free(*bufp); /* free any allocated file name */
26419 *bufp = *fnamep;
26420 if (*fnamep == NULL)
26421 return -1;
26422 add_pathsep(*fnamep);
26423 }
26424 }
26425
26426 /* ":." - path relative to the current directory */
26427 /* ":~" - path relative to the home directory */
26428 /* ":8" - shortname path - postponed till after */
26429 while (src[*usedlen] == ':'
26430 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26431 {
26432 *usedlen += 2;
26433 if (c == '8')
26434 {
26435#ifdef WIN3264
26436 has_shortname = 1; /* Postpone this. */
26437#endif
26438 continue;
26439 }
26440 pbuf = NULL;
26441 /* Need full path first (use expand_env() to remove a "~/") */
26442 if (!has_fullname)
26443 {
26444 if (c == '.' && **fnamep == '~')
26445 p = pbuf = expand_env_save(*fnamep);
26446 else
26447 p = pbuf = FullName_save(*fnamep, FALSE);
26448 }
26449 else
26450 p = *fnamep;
26451
26452 has_fullname = 0;
26453
26454 if (p != NULL)
26455 {
26456 if (c == '.')
26457 {
26458 mch_dirname(dirname, MAXPATHL);
26459 s = shorten_fname(p, dirname);
26460 if (s != NULL)
26461 {
26462 *fnamep = s;
26463 if (pbuf != NULL)
26464 {
26465 vim_free(*bufp); /* free any allocated file name */
26466 *bufp = pbuf;
26467 pbuf = NULL;
26468 }
26469 }
26470 }
26471 else
26472 {
26473 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26474 /* Only replace it when it starts with '~' */
26475 if (*dirname == '~')
26476 {
26477 s = vim_strsave(dirname);
26478 if (s != NULL)
26479 {
26480 *fnamep = s;
26481 vim_free(*bufp);
26482 *bufp = s;
26483 }
26484 }
26485 }
26486 vim_free(pbuf);
26487 }
26488 }
26489
26490 tail = gettail(*fnamep);
26491 *fnamelen = (int)STRLEN(*fnamep);
26492
26493 /* ":h" - head, remove "/file_name", can be repeated */
26494 /* Don't remove the first "/" or "c:\" */
26495 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26496 {
26497 valid |= VALID_HEAD;
26498 *usedlen += 2;
26499 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026500 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026501 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026502 *fnamelen = (int)(tail - *fnamep);
26503#ifdef VMS
26504 if (*fnamelen > 0)
26505 *fnamelen += 1; /* the path separator is part of the path */
26506#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026507 if (*fnamelen == 0)
26508 {
26509 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26510 p = vim_strsave((char_u *)".");
26511 if (p == NULL)
26512 return -1;
26513 vim_free(*bufp);
26514 *bufp = *fnamep = tail = p;
26515 *fnamelen = 1;
26516 }
26517 else
26518 {
26519 while (tail > s && !after_pathsep(s, tail))
26520 mb_ptr_back(*fnamep, tail);
26521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 }
26523
26524 /* ":8" - shortname */
26525 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26526 {
26527 *usedlen += 2;
26528#ifdef WIN3264
26529 has_shortname = 1;
26530#endif
26531 }
26532
26533#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026534 /*
26535 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536 */
26537 if (has_shortname)
26538 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026539 /* Copy the string if it is shortened by :h and when it wasn't copied
26540 * yet, because we are going to change it in place. Avoids changing
26541 * the buffer name for "%:8". */
26542 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543 {
26544 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026545 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 return -1;
26547 vim_free(*bufp);
26548 *bufp = *fnamep = p;
26549 }
26550
26551 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026552 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553 if (!has_fullname && !vim_isAbsName(*fnamep))
26554 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026555 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026556 return -1;
26557 }
26558 else
26559 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026560 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561
Bram Moolenaardc935552011-08-17 15:23:23 +020026562 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026564 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026565 return -1;
26566
26567 if (l == 0)
26568 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026569 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026570 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026571 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572 return -1;
26573 }
26574 *fnamelen = l;
26575 }
26576 }
26577#endif /* WIN3264 */
26578
26579 /* ":t" - tail, just the basename */
26580 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26581 {
26582 *usedlen += 2;
26583 *fnamelen -= (int)(tail - *fnamep);
26584 *fnamep = tail;
26585 }
26586
26587 /* ":e" - extension, can be repeated */
26588 /* ":r" - root, without extension, can be repeated */
26589 while (src[*usedlen] == ':'
26590 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26591 {
26592 /* find a '.' in the tail:
26593 * - for second :e: before the current fname
26594 * - otherwise: The last '.'
26595 */
26596 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26597 s = *fnamep - 2;
26598 else
26599 s = *fnamep + *fnamelen - 1;
26600 for ( ; s > tail; --s)
26601 if (s[0] == '.')
26602 break;
26603 if (src[*usedlen + 1] == 'e') /* :e */
26604 {
26605 if (s > tail)
26606 {
26607 *fnamelen += (int)(*fnamep - (s + 1));
26608 *fnamep = s + 1;
26609#ifdef VMS
26610 /* cut version from the extension */
26611 s = *fnamep + *fnamelen - 1;
26612 for ( ; s > *fnamep; --s)
26613 if (s[0] == ';')
26614 break;
26615 if (s > *fnamep)
26616 *fnamelen = s - *fnamep;
26617#endif
26618 }
26619 else if (*fnamep <= tail)
26620 *fnamelen = 0;
26621 }
26622 else /* :r */
26623 {
26624 if (s > tail) /* remove one extension */
26625 *fnamelen = (int)(s - *fnamep);
26626 }
26627 *usedlen += 2;
26628 }
26629
26630 /* ":s?pat?foo?" - substitute */
26631 /* ":gs?pat?foo?" - global substitute */
26632 if (src[*usedlen] == ':'
26633 && (src[*usedlen + 1] == 's'
26634 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26635 {
26636 char_u *str;
26637 char_u *pat;
26638 char_u *sub;
26639 int sep;
26640 char_u *flags;
26641 int didit = FALSE;
26642
26643 flags = (char_u *)"";
26644 s = src + *usedlen + 2;
26645 if (src[*usedlen + 1] == 'g')
26646 {
26647 flags = (char_u *)"g";
26648 ++s;
26649 }
26650
26651 sep = *s++;
26652 if (sep)
26653 {
26654 /* find end of pattern */
26655 p = vim_strchr(s, sep);
26656 if (p != NULL)
26657 {
26658 pat = vim_strnsave(s, (int)(p - s));
26659 if (pat != NULL)
26660 {
26661 s = p + 1;
26662 /* find end of substitution */
26663 p = vim_strchr(s, sep);
26664 if (p != NULL)
26665 {
26666 sub = vim_strnsave(s, (int)(p - s));
26667 str = vim_strnsave(*fnamep, *fnamelen);
26668 if (sub != NULL && str != NULL)
26669 {
26670 *usedlen = (int)(p + 1 - src);
26671 s = do_string_sub(str, pat, sub, flags);
26672 if (s != NULL)
26673 {
26674 *fnamep = s;
26675 *fnamelen = (int)STRLEN(s);
26676 vim_free(*bufp);
26677 *bufp = s;
26678 didit = TRUE;
26679 }
26680 }
26681 vim_free(sub);
26682 vim_free(str);
26683 }
26684 vim_free(pat);
26685 }
26686 }
26687 /* after using ":s", repeat all the modifiers */
26688 if (didit)
26689 goto repeat;
26690 }
26691 }
26692
Bram Moolenaar26df0922014-02-23 23:39:13 +010026693 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26694 {
26695 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26696 if (p == NULL)
26697 return -1;
26698 vim_free(*bufp);
26699 *bufp = *fnamep = p;
26700 *fnamelen = (int)STRLEN(p);
26701 *usedlen += 2;
26702 }
26703
Bram Moolenaar071d4272004-06-13 20:20:40 +000026704 return valid;
26705}
26706
26707/*
26708 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26709 * "flags" can be "g" to do a global substitute.
26710 * Returns an allocated string, NULL for error.
26711 */
26712 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026713do_string_sub(
26714 char_u *str,
26715 char_u *pat,
26716 char_u *sub,
26717 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026718{
26719 int sublen;
26720 regmatch_T regmatch;
26721 int i;
26722 int do_all;
26723 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026724 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 garray_T ga;
26726 char_u *ret;
26727 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026728 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026729
26730 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26731 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026732 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026733
26734 ga_init2(&ga, 1, 200);
26735
26736 do_all = (flags[0] == 'g');
26737
26738 regmatch.rm_ic = p_ic;
26739 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26740 if (regmatch.regprog != NULL)
26741 {
26742 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026743 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026744 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26745 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026746 /* Skip empty match except for first match. */
26747 if (regmatch.startp[0] == regmatch.endp[0])
26748 {
26749 if (zero_width == regmatch.startp[0])
26750 {
26751 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026752 i = MB_PTR2LEN(tail);
26753 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26754 (size_t)i);
26755 ga.ga_len += i;
26756 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026757 continue;
26758 }
26759 zero_width = regmatch.startp[0];
26760 }
26761
Bram Moolenaar071d4272004-06-13 20:20:40 +000026762 /*
26763 * Get some space for a temporary buffer to do the substitution
26764 * into. It will contain:
26765 * - The text up to where the match is.
26766 * - The substituted text.
26767 * - The text after the match.
26768 */
26769 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026770 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26772 {
26773 ga_clear(&ga);
26774 break;
26775 }
26776
26777 /* copy the text up to where the match is */
26778 i = (int)(regmatch.startp[0] - tail);
26779 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26780 /* add the substituted text */
26781 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26782 + ga.ga_len + i, TRUE, TRUE, FALSE);
26783 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026784 tail = regmatch.endp[0];
26785 if (*tail == NUL)
26786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026787 if (!do_all)
26788 break;
26789 }
26790
26791 if (ga.ga_data != NULL)
26792 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26793
Bram Moolenaar473de612013-06-08 18:19:48 +020026794 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026795 }
26796
26797 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26798 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026799 if (p_cpo == empty_option)
26800 p_cpo = save_cpo;
26801 else
26802 /* Darn, evaluating {sub} expression changed the value. */
26803 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026804
26805 return ret;
26806}
26807
26808#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */