blob: 69238c18131f90f51aab2d2683f2e23357868ed8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
560static void f_exepath(typval_T *argvars, typval_T *rettv);
561static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_expand(typval_T *argvars, typval_T *rettv);
566static void f_extend(typval_T *argvars, typval_T *rettv);
567static void f_feedkeys(typval_T *argvars, typval_T *rettv);
568static void f_filereadable(typval_T *argvars, typval_T *rettv);
569static void f_filewritable(typval_T *argvars, typval_T *rettv);
570static void f_filter(typval_T *argvars, typval_T *rettv);
571static void f_finddir(typval_T *argvars, typval_T *rettv);
572static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_float2nr(typval_T *argvars, typval_T *rettv);
575static void f_floor(typval_T *argvars, typval_T *rettv);
576static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000577#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100578static void f_fnameescape(typval_T *argvars, typval_T *rettv);
579static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
580static void f_foldclosed(typval_T *argvars, typval_T *rettv);
581static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
582static void f_foldlevel(typval_T *argvars, typval_T *rettv);
583static void f_foldtext(typval_T *argvars, typval_T *rettv);
584static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
585static void f_foreground(typval_T *argvars, typval_T *rettv);
586static void f_function(typval_T *argvars, typval_T *rettv);
587static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
588static void f_get(typval_T *argvars, typval_T *rettv);
589static void f_getbufline(typval_T *argvars, typval_T *rettv);
590static void f_getbufvar(typval_T *argvars, typval_T *rettv);
591static void f_getchar(typval_T *argvars, typval_T *rettv);
592static void f_getcharmod(typval_T *argvars, typval_T *rettv);
593static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
594static void f_getcmdline(typval_T *argvars, typval_T *rettv);
595static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
596static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
597static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
598static void f_getcwd(typval_T *argvars, typval_T *rettv);
599static void f_getfontname(typval_T *argvars, typval_T *rettv);
600static void f_getfperm(typval_T *argvars, typval_T *rettv);
601static void f_getfsize(typval_T *argvars, typval_T *rettv);
602static void f_getftime(typval_T *argvars, typval_T *rettv);
603static void f_getftype(typval_T *argvars, typval_T *rettv);
604static void f_getline(typval_T *argvars, typval_T *rettv);
605static void f_getmatches(typval_T *argvars, typval_T *rettv);
606static void f_getpid(typval_T *argvars, typval_T *rettv);
607static void f_getcurpos(typval_T *argvars, typval_T *rettv);
608static void f_getpos(typval_T *argvars, typval_T *rettv);
609static void f_getqflist(typval_T *argvars, typval_T *rettv);
610static void f_getreg(typval_T *argvars, typval_T *rettv);
611static void f_getregtype(typval_T *argvars, typval_T *rettv);
612static void f_gettabvar(typval_T *argvars, typval_T *rettv);
613static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
614static void f_getwinposx(typval_T *argvars, typval_T *rettv);
615static void f_getwinposy(typval_T *argvars, typval_T *rettv);
616static void f_getwinvar(typval_T *argvars, typval_T *rettv);
617static void f_glob(typval_T *argvars, typval_T *rettv);
618static void f_globpath(typval_T *argvars, typval_T *rettv);
619static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
620static void f_has(typval_T *argvars, typval_T *rettv);
621static void f_has_key(typval_T *argvars, typval_T *rettv);
622static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
623static void f_hasmapto(typval_T *argvars, typval_T *rettv);
624static void f_histadd(typval_T *argvars, typval_T *rettv);
625static void f_histdel(typval_T *argvars, typval_T *rettv);
626static void f_histget(typval_T *argvars, typval_T *rettv);
627static void f_histnr(typval_T *argvars, typval_T *rettv);
628static void f_hlID(typval_T *argvars, typval_T *rettv);
629static void f_hlexists(typval_T *argvars, typval_T *rettv);
630static void f_hostname(typval_T *argvars, typval_T *rettv);
631static void f_iconv(typval_T *argvars, typval_T *rettv);
632static void f_indent(typval_T *argvars, typval_T *rettv);
633static void f_index(typval_T *argvars, typval_T *rettv);
634static void f_input(typval_T *argvars, typval_T *rettv);
635static void f_inputdialog(typval_T *argvars, typval_T *rettv);
636static void f_inputlist(typval_T *argvars, typval_T *rettv);
637static void f_inputrestore(typval_T *argvars, typval_T *rettv);
638static void f_inputsave(typval_T *argvars, typval_T *rettv);
639static void f_inputsecret(typval_T *argvars, typval_T *rettv);
640static void f_insert(typval_T *argvars, typval_T *rettv);
641static void f_invert(typval_T *argvars, typval_T *rettv);
642static void f_isdirectory(typval_T *argvars, typval_T *rettv);
643static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100644#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
645static void f_isnan(typval_T *argvars, typval_T *rettv);
646#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100648#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100649static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100650static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100651static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100652static void f_job_start(typval_T *argvars, typval_T *rettv);
653static void f_job_stop(typval_T *argvars, typval_T *rettv);
654static void f_job_status(typval_T *argvars, typval_T *rettv);
655#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100657static void f_js_decode(typval_T *argvars, typval_T *rettv);
658static void f_js_encode(typval_T *argvars, typval_T *rettv);
659static void f_json_decode(typval_T *argvars, typval_T *rettv);
660static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_keys(typval_T *argvars, typval_T *rettv);
662static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
663static void f_len(typval_T *argvars, typval_T *rettv);
664static void f_libcall(typval_T *argvars, typval_T *rettv);
665static void f_libcallnr(typval_T *argvars, typval_T *rettv);
666static void f_line(typval_T *argvars, typval_T *rettv);
667static void f_line2byte(typval_T *argvars, typval_T *rettv);
668static void f_lispindent(typval_T *argvars, typval_T *rettv);
669static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_log(typval_T *argvars, typval_T *rettv);
672static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000673#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200674#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_map(typval_T *argvars, typval_T *rettv);
678static void f_maparg(typval_T *argvars, typval_T *rettv);
679static void f_mapcheck(typval_T *argvars, typval_T *rettv);
680static void f_match(typval_T *argvars, typval_T *rettv);
681static void f_matchadd(typval_T *argvars, typval_T *rettv);
682static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
683static void f_matcharg(typval_T *argvars, typval_T *rettv);
684static void f_matchdelete(typval_T *argvars, typval_T *rettv);
685static void f_matchend(typval_T *argvars, typval_T *rettv);
686static void f_matchlist(typval_T *argvars, typval_T *rettv);
687static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200688static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_max(typval_T *argvars, typval_T *rettv);
690static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100695#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
699static void f_nr2char(typval_T *argvars, typval_T *rettv);
700static void f_or(typval_T *argvars, typval_T *rettv);
701static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100702#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100704#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
709static void f_printf(typval_T *argvars, typval_T *rettv);
710static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
714#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200716#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_range(typval_T *argvars, typval_T *rettv);
718static void f_readfile(typval_T *argvars, typval_T *rettv);
719static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100720#ifdef FEAT_FLOAT
721static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
722#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_reltimestr(typval_T *argvars, typval_T *rettv);
724static void f_remote_expr(typval_T *argvars, typval_T *rettv);
725static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
726static void f_remote_peek(typval_T *argvars, typval_T *rettv);
727static void f_remote_read(typval_T *argvars, typval_T *rettv);
728static void f_remote_send(typval_T *argvars, typval_T *rettv);
729static void f_remove(typval_T *argvars, typval_T *rettv);
730static void f_rename(typval_T *argvars, typval_T *rettv);
731static void f_repeat(typval_T *argvars, typval_T *rettv);
732static void f_resolve(typval_T *argvars, typval_T *rettv);
733static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_screenattr(typval_T *argvars, typval_T *rettv);
738static void f_screenchar(typval_T *argvars, typval_T *rettv);
739static void f_screencol(typval_T *argvars, typval_T *rettv);
740static void f_screenrow(typval_T *argvars, typval_T *rettv);
741static void f_search(typval_T *argvars, typval_T *rettv);
742static void f_searchdecl(typval_T *argvars, typval_T *rettv);
743static void f_searchpair(typval_T *argvars, typval_T *rettv);
744static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
745static void f_searchpos(typval_T *argvars, typval_T *rettv);
746static void f_server2client(typval_T *argvars, typval_T *rettv);
747static void f_serverlist(typval_T *argvars, typval_T *rettv);
748static void f_setbufvar(typval_T *argvars, typval_T *rettv);
749static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
750static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100751static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_setline(typval_T *argvars, typval_T *rettv);
753static void f_setloclist(typval_T *argvars, typval_T *rettv);
754static void f_setmatches(typval_T *argvars, typval_T *rettv);
755static void f_setpos(typval_T *argvars, typval_T *rettv);
756static void f_setqflist(typval_T *argvars, typval_T *rettv);
757static void f_setreg(typval_T *argvars, typval_T *rettv);
758static void f_settabvar(typval_T *argvars, typval_T *rettv);
759static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
760static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100761#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100763#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_shellescape(typval_T *argvars, typval_T *rettv);
765static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
766static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sin(typval_T *argvars, typval_T *rettv);
769static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_sort(typval_T *argvars, typval_T *rettv);
772static void f_soundfold(typval_T *argvars, typval_T *rettv);
773static void f_spellbadword(typval_T *argvars, typval_T *rettv);
774static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
775static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_sqrt(typval_T *argvars, typval_T *rettv);
778static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_str2nr(typval_T *argvars, typval_T *rettv);
781static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000782#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000784#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200785static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_stridx(typval_T *argvars, typval_T *rettv);
787static void f_string(typval_T *argvars, typval_T *rettv);
788static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_strpart(typval_T *argvars, typval_T *rettv);
791static void f_strridx(typval_T *argvars, typval_T *rettv);
792static void f_strtrans(typval_T *argvars, typval_T *rettv);
793static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
794static void f_strwidth(typval_T *argvars, typval_T *rettv);
795static void f_submatch(typval_T *argvars, typval_T *rettv);
796static void f_substitute(typval_T *argvars, typval_T *rettv);
797static void f_synID(typval_T *argvars, typval_T *rettv);
798static void f_synIDattr(typval_T *argvars, typval_T *rettv);
799static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
800static void f_synstack(typval_T *argvars, typval_T *rettv);
801static void f_synconcealed(typval_T *argvars, typval_T *rettv);
802static void f_system(typval_T *argvars, typval_T *rettv);
803static void f_systemlist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
805static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
806static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
807static void f_taglist(typval_T *argvars, typval_T *rettv);
808static void f_tagfiles(typval_T *argvars, typval_T *rettv);
809static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200810static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
811static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200812static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
817#ifdef FEAT_JOB_CHANNEL
818static void f_test_null_job(typval_T *argvars, typval_T *rettv);
819#endif
820static void f_test_null_list(typval_T *argvars, typval_T *rettv);
821static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
822static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200823static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200824#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100825static void f_tan(typval_T *argvars, typval_T *rettv);
826static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200827#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100828#ifdef FEAT_TIMERS
829static void f_timer_start(typval_T *argvars, typval_T *rettv);
830static void f_timer_stop(typval_T *argvars, typval_T *rettv);
831#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static void f_tolower(typval_T *argvars, typval_T *rettv);
833static void f_toupper(typval_T *argvars, typval_T *rettv);
834static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000835#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000837#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100838static void f_type(typval_T *argvars, typval_T *rettv);
839static void f_undofile(typval_T *argvars, typval_T *rettv);
840static void f_undotree(typval_T *argvars, typval_T *rettv);
841static void f_uniq(typval_T *argvars, typval_T *rettv);
842static void f_values(typval_T *argvars, typval_T *rettv);
843static void f_virtcol(typval_T *argvars, typval_T *rettv);
844static void f_visualmode(typval_T *argvars, typval_T *rettv);
845static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100846static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100847static void f_win_getid(typval_T *argvars, typval_T *rettv);
848static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
849static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
850static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static void f_winbufnr(typval_T *argvars, typval_T *rettv);
852static void f_wincol(typval_T *argvars, typval_T *rettv);
853static void f_winheight(typval_T *argvars, typval_T *rettv);
854static void f_winline(typval_T *argvars, typval_T *rettv);
855static void f_winnr(typval_T *argvars, typval_T *rettv);
856static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
857static void f_winrestview(typval_T *argvars, typval_T *rettv);
858static void f_winsaveview(typval_T *argvars, typval_T *rettv);
859static void f_winwidth(typval_T *argvars, typval_T *rettv);
860static void f_writefile(typval_T *argvars, typval_T *rettv);
861static void f_wordcount(typval_T *argvars, typval_T *rettv);
862static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
865static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
866static int get_env_len(char_u **arg);
867static int get_id_len(char_u **arg);
868static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
869static 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 +0000870#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
871#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
872 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
874static int eval_isnamec(int c);
875static int eval_isnamec1(int c);
876static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
877static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static typval_T *alloc_string_tv(char_u *string);
879static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100880#ifdef FEAT_FLOAT
881static float_T get_tv_float(typval_T *varp);
882#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static linenr_T get_tv_lnum(typval_T *argvars);
884static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
886static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
887static hashtab_T *find_var_ht(char_u *name, char_u **varname);
888static funccall_T *get_funccal(void);
889static void vars_clear_ext(hashtab_T *ht, int free_val);
890static void delete_var(hashtab_T *ht, hashitem_T *hi);
891static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
892static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
893static void set_var(char_u *name, typval_T *varp, int copy);
894static int var_check_ro(int flags, char_u *name, int use_gettext);
895static int var_check_fixed(int flags, char_u *name, int use_gettext);
896static int var_check_func_name(char_u *name, int new_var);
897static int valid_varname(char_u *varname);
898static int tv_check_lock(int lock, char_u *name, int use_gettext);
899static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
900static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100901static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902static int eval_fname_script(char_u *p);
903static int eval_fname_sid(char_u *p);
904static void list_func_head(ufunc_T *fp, int indent);
905static ufunc_T *find_func(char_u *name);
906static int function_exists(char_u *name);
907static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100909static void func_do_profile(ufunc_T *fp);
910static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
911static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000917static int
918# ifdef __BORLANDC__
919 _RTLENTRYF
920# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100921 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000922#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100923static int script_autoload(char_u *name, int reload);
924static char_u *autoload_name(char_u *name);
925static void cat_func_name(char_u *buf, ufunc_T *fp);
926static void func_free(ufunc_T *fp);
927static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
928static int can_free_funccal(funccall_T *fc, int copyID) ;
929static void free_funccal(funccall_T *fc, int free_val);
930static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
931static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
932static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
933static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
934static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
935static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
936static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
937static int write_list(FILE *fd, list_T *list, int binary);
938static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000939
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940
941#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100942static int compare_func_name(const void *s1, const void *s2);
943static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200944#endif
945
Bram Moolenaar33570922005-01-25 22:26:29 +0000946/*
947 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000948 */
949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100950eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 int i;
953 struct vimvar *p;
954
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200955 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
956 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200957 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000958 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000960
961 for (i = 0; i < VV_LEN; ++i)
962 {
963 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200964 if (STRLEN(p->vv_name) > 16)
965 {
966 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
967 getout(1);
968 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 STRCPY(p->vv_di.di_key, p->vv_name);
970 if (p->vv_flags & VV_RO)
971 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
972 else if (p->vv_flags & VV_RO_SBX)
973 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
974 else
975 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000976
977 /* add to v: scope dict, unless the value is not always available */
978 if (p->vv_type != VAR_UNKNOWN)
979 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000981 /* add to compat scope dict */
982 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000983 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100984 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
985
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100987 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200988 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100989 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100990
991 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
992 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
993 set_vim_var_nr(VV_NONE, VVAL_NONE);
994 set_vim_var_nr(VV_NULL, VVAL_NULL);
995
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200996 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200997
998#ifdef EBCDIC
999 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001000 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001001 */
1002 sortFunctions();
1003#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001004}
1005
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001006#if defined(EXITFREE) || defined(PROTO)
1007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001009{
1010 int i;
1011 struct vimvar *p;
1012
1013 for (i = 0; i < VV_LEN; ++i)
1014 {
1015 p = &vimvars[i];
1016 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001017 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001018 vim_free(p->vv_str);
1019 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001020 }
1021 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1022 {
1023 list_unref(p->vv_list);
1024 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001025 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001026 }
1027 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001028 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001029 hash_clear(&compat_hashtab);
1030
Bram Moolenaard9fba312005-06-26 22:34:35 +00001031 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001032# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001033 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001034# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001035
1036 /* global variables */
1037 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001038
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001039 /* autoloaded script names */
1040 ga_clear_strings(&ga_loaded);
1041
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 /* Script-local variables. First clear all the variables and in a second
1043 * loop free the scriptvar_T, because a variable in one script might hold
1044 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001047 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001048 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 ga_clear(&ga_scripts);
1050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001051 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001052 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001053
1054 /* functions */
1055 free_all_functions();
1056 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001057}
1058#endif
1059
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * Return the name of the executed function.
1062 */
1063 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067}
1068
1069/*
1070 * Return the address holding the next breakpoint line for a funccall cookie.
1071 */
1072 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076}
1077
1078/*
1079 * Return the address holding the debug tick for a funccall cookie.
1080 */
1081 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085}
1086
1087/*
1088 * Return the nesting level for a funccall cookie.
1089 */
1090 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001091func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar33570922005-01-25 22:26:29 +00001093 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
1096/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001097funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001099/* pointer to list of previously used funccal, still around because some
1100 * item in it is still being used. */
1101funccall_T *previous_funccal = NULL;
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Return TRUE when a function was ended by a ":return" command.
1105 */
1106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 return current_funccal->returned;
1110}
1111
1112
1113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * Set an internal variable to a string value. Creates the variable if it does
1115 * not already exist.
1116 */
1117 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001118set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001120 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
1123 val = vim_strsave(value);
1124 if (val != NULL)
1125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001127 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001129 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 }
1133}
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137static char_u *redir_endp = NULL;
1138static char_u *redir_varname = NULL;
1139
1140/*
1141 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001142 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 * Returns OK if successfully completed the setup. FAIL otherwise.
1144 */
1145 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001146var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147{
1148 int save_emsg;
1149 int err;
1150 typval_T tv;
1151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001153 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 {
1155 EMSG(_(e_invarg));
1156 return FAIL;
1157 }
1158
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 redir_varname = vim_strsave(name);
1161 if (redir_varname == NULL)
1162 return FAIL;
1163
1164 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1165 if (redir_lval == NULL)
1166 {
1167 var_redir_stop();
1168 return FAIL;
1169 }
1170
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 /* The output is stored in growarray "redir_ga" until redirection ends. */
1172 ga_init2(&redir_ga, (int)sizeof(char), 500);
1173
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001175 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001176 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1178 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001179 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 if (redir_endp != NULL && *redir_endp != NUL)
1181 /* Trailing characters are present after the variable name */
1182 EMSG(_(e_trailing));
1183 else
1184 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001185 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 var_redir_stop();
1187 return FAIL;
1188 }
1189
1190 /* check if we can write to the variable: set it to or append an empty
1191 * string */
1192 save_emsg = did_emsg;
1193 did_emsg = FALSE;
1194 tv.v_type = VAR_STRING;
1195 tv.vval.v_string = (char_u *)"";
1196 if (append)
1197 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1198 else
1199 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001200 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001202 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 if (err)
1204 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 var_redir_stop();
1207 return FAIL;
1208 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 * Append "value[value_len]" to the variable set by var_redir_start().
1215 * The actual appending is postponed until redirection ends, because the value
1216 * appended may in fact be the string we write to, changing it may cause freed
1217 * memory to be used:
1218 * :redir => foo
1219 * :let foo
1220 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 */
1222 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001223var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001225 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226
1227 if (redir_lval == NULL)
1228 return;
1229
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 {
1237 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001238 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001239 }
1240 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001241 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242}
1243
1244/*
1245 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001246 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 */
1248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001249var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001251 typval_T tv;
1252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 if (redir_lval != NULL)
1254 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001255 /* If there was no error: assign the text to the variable. */
1256 if (redir_endp != NULL)
1257 {
1258 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1259 tv.v_type = VAR_STRING;
1260 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001261 /* Call get_lval() again, if it's inside a Dict or List it may
1262 * have changed. */
1263 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001264 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001265 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1266 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1267 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001268 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001269
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001270 /* free the collected output */
1271 vim_free(redir_ga.ga_data);
1272 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001273
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001274 vim_free(redir_lval);
1275 redir_lval = NULL;
1276 }
1277 vim_free(redir_varname);
1278 redir_varname = NULL;
1279}
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281# if defined(FEAT_MBYTE) || defined(PROTO)
1282 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001283eval_charconvert(
1284 char_u *enc_from,
1285 char_u *enc_to,
1286 char_u *fname_from,
1287 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289 int err = FALSE;
1290
1291 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1292 set_vim_var_string(VV_CC_TO, enc_to, -1);
1293 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1294 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1295 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1296 err = TRUE;
1297 set_vim_var_string(VV_CC_FROM, NULL, -1);
1298 set_vim_var_string(VV_CC_TO, NULL, -1);
1299 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301
1302 if (err)
1303 return FAIL;
1304 return OK;
1305}
1306# endif
1307
1308# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err = FALSE;
1313
1314 set_vim_var_string(VV_FNAME_IN, fname, -1);
1315 set_vim_var_string(VV_CMDARG, args, -1);
1316 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1317 err = TRUE;
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_CMDARG, NULL, -1);
1320
1321 if (err)
1322 {
1323 mch_remove(fname);
1324 return FAIL;
1325 }
1326 return OK;
1327}
1328# endif
1329
1330# if defined(FEAT_DIFF) || defined(PROTO)
1331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_diff(
1333 char_u *origfile,
1334 char_u *newfile,
1335 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int err = FALSE;
1338
1339 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1340 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1341 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1342 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1343 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1344 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1345 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1346}
1347
1348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001349eval_patch(
1350 char_u *origfile,
1351 char_u *difffile,
1352 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 int err;
1355
1356 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1357 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1358 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1359 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1360 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1361 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1362 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1363}
1364# endif
1365
1366/*
1367 * Top level evaluation function, returning a boolean.
1368 * Sets "error" to TRUE if there was an error.
1369 * Return TRUE or FALSE.
1370 */
1371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372eval_to_bool(
1373 char_u *arg,
1374 int *error,
1375 char_u **nextcmd,
1376 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001379 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381 if (skip)
1382 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 else
1386 {
1387 *error = FALSE;
1388 if (!skip)
1389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001390 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 }
1394 if (skip)
1395 --emsg_skip;
1396
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001397 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
1400/*
1401 * Top level evaluation function, returning a string. If "skip" is TRUE,
1402 * only parsing to "nextcmd" is done, without reporting errors. Return
1403 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1404 */
1405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_to_string_skip(
1407 char_u *arg,
1408 char_u **nextcmd,
1409 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *retval;
1413
1414 if (skip)
1415 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = NULL;
1418 else
1419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 retval = vim_strsave(get_tv_string(&tv));
1421 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
1423 if (skip)
1424 --emsg_skip;
1425
1426 return retval;
1427}
1428
1429/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430 * Skip over an expression at "*pp".
1431 * Return FAIL for an error, OK otherwise.
1432 */
1433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001434skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435{
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001437
1438 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001440}
1441
1442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444 * When "convert" is TRUE convert a List into a sequence of lines and convert
1445 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 * Return pointer to allocated memory, or NULL for failure.
1447 */
1448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001449eval_to_string(
1450 char_u *arg,
1451 char_u **nextcmd,
1452 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
Bram Moolenaar33570922005-01-25 22:26:29 +00001454 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001457#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001461 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 retval = NULL;
1463 else
1464 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001465 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 {
1467 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001468 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001469 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001470 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001471 if (tv.vval.v_list->lv_len > 0)
1472 ga_append(&ga, NL);
1473 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 ga_append(&ga, NUL);
1475 retval = (char_u *)ga.ga_data;
1476 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001477#ifdef FEAT_FLOAT
1478 else if (convert && tv.v_type == VAR_FLOAT)
1479 {
1480 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1481 retval = vim_strsave(numbuf);
1482 }
1483#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 else
1485 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488
1489 return retval;
1490}
1491
1492/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001493 * Call eval_to_string() without using current local variables and using
1494 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497eval_to_string_safe(
1498 char_u *arg,
1499 char_u **nextcmd,
1500 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 char_u *retval;
1503 void *save_funccalp;
1504
1505 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001506 if (use_sandbox)
1507 ++sandbox;
1508 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001510 if (use_sandbox)
1511 --sandbox;
1512 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 restore_funccal(save_funccalp);
1514 return retval;
1515}
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
1518 * Top level evaluation function, returning a number.
1519 * Evaluates "expr" silently.
1520 * Returns -1 for an error.
1521 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001522 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
Bram Moolenaar33570922005-01-25 22:26:29 +00001525 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001526 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001527 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529 ++emsg_off;
1530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 retval = -1;
1533 else
1534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001535 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 --emsg_off;
1539
1540 return retval;
1541}
1542
Bram Moolenaara40058a2005-07-11 22:42:07 +00001543/*
1544 * Prepare v: variable "idx" to be used.
1545 * Save the current typeval in "save_tv".
1546 * When not used yet add the variable to the v: hashtable.
1547 */
1548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001550{
1551 *save_tv = vimvars[idx].vv_tv;
1552 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1553 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1554}
1555
1556/*
1557 * Restore v: variable "idx" to typeval "save_tv".
1558 * When no longer defined, remove the variable from the v: hashtable.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562{
1563 hashitem_T *hi;
1564
Bram Moolenaara40058a2005-07-11 22:42:07 +00001565 vimvars[idx].vv_tv = *save_tv;
1566 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1567 {
1568 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1569 if (HASHITEM_EMPTY(hi))
1570 EMSG2(_(e_intern2), "restore_vimvar()");
1571 else
1572 hash_remove(&vimvarht, hi);
1573 }
1574}
1575
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001576#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577/*
1578 * Evaluate an expression to a list with suggestions.
1579 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001580 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581 */
1582 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001583eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001584{
1585 typval_T save_val;
1586 typval_T rettv;
1587 list_T *list = NULL;
1588 char_u *p = skipwhite(expr);
1589
1590 /* Set "v:val" to the bad word. */
1591 prepare_vimvar(VV_VAL, &save_val);
1592 vimvars[VV_VAL].vv_type = VAR_STRING;
1593 vimvars[VV_VAL].vv_str = badword;
1594 if (p_verbose == 0)
1595 ++emsg_off;
1596
1597 if (eval1(&p, &rettv, TRUE) == OK)
1598 {
1599 if (rettv.v_type != VAR_LIST)
1600 clear_tv(&rettv);
1601 else
1602 list = rettv.vval.v_list;
1603 }
1604
1605 if (p_verbose == 0)
1606 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001607 restore_vimvar(VV_VAL, &save_val);
1608
1609 return list;
1610}
1611
1612/*
1613 * "list" is supposed to contain two items: a word and a number. Return the
1614 * word in "pp" and the number as the return value.
1615 * Return -1 if anything isn't right.
1616 * Used to get the good word and score from the eval_spell_expr() result.
1617 */
1618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001619get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001620{
1621 listitem_T *li;
1622
1623 li = list->lv_first;
1624 if (li == NULL)
1625 return -1;
1626 *pp = get_tv_string(&li->li_tv);
1627
1628 li = li->li_next;
1629 if (li == NULL)
1630 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001631 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001632}
1633#endif
1634
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001636 * Top level evaluation function.
1637 * Returns an allocated typval_T with the result.
1638 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001639 */
1640 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642{
1643 typval_T *tv;
1644
1645 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001646 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 {
1648 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001649 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650 }
1651
1652 return tv;
1653}
1654
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658 * Uses argv[argc] for the function arguments. Only Number and String
1659 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001663call_vim_function(
1664 char_u *func,
1665 int argc,
1666 char_u **argv,
1667 int safe, /* use the sandbox */
1668 int str_arg_only, /* all arguments are strings */
1669 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001672 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 int len;
1674 int i;
1675 int doesrange;
1676 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001679 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 for (i = 0; i < argc; i++)
1684 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 /* Pass a NULL or empty argument as an empty string */
1686 if (argv[i] == NULL || *argv[i] == NUL)
1687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001688 argvars[i].v_type = VAR_STRING;
1689 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001690 continue;
1691 }
1692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 if (str_arg_only)
1694 len = 0;
1695 else
1696 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001697 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (len != 0 && len == (int)STRLEN(argv[i]))
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_NUMBER;
1701 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 else
1704 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001705 argvars[i].v_type = VAR_STRING;
1706 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 }
1709
1710 if (safe)
1711 {
1712 save_funccalp = save_funccal();
1713 ++sandbox;
1714 }
1715
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1717 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001719 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (safe)
1721 {
1722 --sandbox;
1723 restore_funccal(save_funccalp);
1724 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 vim_free(argvars);
1726
1727 if (ret == FAIL)
1728 clear_tv(rettv);
1729
1730 return ret;
1731}
1732
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001733/*
1734 * Call vimL function "func" and return the result as a number.
1735 * Returns -1 when calling the function fails.
1736 * Uses argv[argc] for the function arguments.
1737 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001738 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739call_func_retnr(
1740 char_u *func,
1741 int argc,
1742 char_u **argv,
1743 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001744{
1745 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001746 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001747
1748 /* All arguments are passed as strings, no conversion to number. */
1749 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1750 return -1;
1751
1752 retval = get_tv_number_chk(&rettv, NULL);
1753 clear_tv(&rettv);
1754 return retval;
1755}
1756
1757#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1758 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1759
Bram Moolenaar4f688582007-07-24 12:34:30 +00001760# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001762 * Call vimL function "func" and return the result as a string.
1763 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 * Uses argv[argc] for the function arguments.
1765 */
1766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767call_func_retstr(
1768 char_u *func,
1769 int argc,
1770 char_u **argv,
1771 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772{
1773 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001774 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001775
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001776 /* All arguments are passed as strings, no conversion to number. */
1777 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001778 return NULL;
1779
1780 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 return retval;
1783}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001784# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001786/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001787 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001789 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 */
1791 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792call_func_retlist(
1793 char_u *func,
1794 int argc,
1795 char_u **argv,
1796 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797{
1798 typval_T rettv;
1799
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001800 /* All arguments are passed as strings, no conversion to number. */
1801 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001802 return NULL;
1803
1804 if (rettv.v_type != VAR_LIST)
1805 {
1806 clear_tv(&rettv);
1807 return NULL;
1808 }
1809
1810 return rettv.vval.v_list;
1811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813
1814/*
1815 * Save the current function call pointer, and set it to NULL.
1816 * Used when executing autocommands and for ":source".
1817 */
1818 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001821 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 current_funccal = NULL;
1824 return (void *)fc;
1825}
1826
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830 funccall_T *fc = (funccall_T *)vfc;
1831
1832 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833}
1834
Bram Moolenaar05159a02005-02-26 23:04:13 +00001835#if defined(FEAT_PROFILE) || defined(PROTO)
1836/*
1837 * Prepare profiling for entering a child or something else that is not
1838 * counted for the script/function itself.
1839 * Should always be called in pair with prof_child_exit().
1840 */
1841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842prof_child_enter(
1843 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001844{
1845 funccall_T *fc = current_funccal;
1846
1847 if (fc != NULL && fc->func->uf_profiling)
1848 profile_start(&fc->prof_child);
1849 script_prof_save(tm);
1850}
1851
1852/*
1853 * Take care of time spent in a child.
1854 * Should always be called after prof_child_enter().
1855 */
1856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857prof_child_exit(
1858 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001859{
1860 funccall_T *fc = current_funccal;
1861
1862 if (fc != NULL && fc->func->uf_profiling)
1863 {
1864 profile_end(&fc->prof_child);
1865 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1866 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1867 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1868 }
1869 script_prof_restore(tm);
1870}
1871#endif
1872
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#ifdef FEAT_FOLDING
1875/*
1876 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1877 * it in "*cp". Doesn't give error messages.
1878 */
1879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001883 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001885 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1886 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 if (use_sandbox)
1890 ++sandbox;
1891 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 retval = 0;
1895 else
1896 {
1897 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 if (tv.v_type == VAR_NUMBER)
1899 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001900 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 retval = 0;
1902 else
1903 {
1904 /* If the result is a string, check if there is a non-digit before
1905 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (!VIM_ISDIGIT(*s) && *s != '-')
1908 *cp = *s++;
1909 retval = atol((char *)s);
1910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001914 if (use_sandbox)
1915 --sandbox;
1916 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001918 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919}
1920#endif
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 * ":let" list all variable values
1924 * ":let var1 var2" list variable values
1925 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 * ":let var += expr" assignment command.
1927 * ":let var -= expr" assignment command.
1928 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001932ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
1934 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 int var_count = 0;
1939 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001941 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001942 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaardb552d602006-03-23 22:59:57 +00001944 argend = skip_var_list(arg, &var_count, &semicolon);
1945 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001947 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1948 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 expr = skipwhite(argend);
1950 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1951 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001953 /*
1954 * ":let" without "=": list variables
1955 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 if (*arg == '[')
1957 EMSG(_(e_invarg));
1958 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001960 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964 list_glob_vars(&first);
1965 list_buf_vars(&first);
1966 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001967#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001969#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001970 list_script_vars(&first);
1971 list_func_vars(&first);
1972 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 eap->nextcmd = check_nextcmd(arg);
1975 }
1976 else
1977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 op[0] = '=';
1979 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001980 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001982 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1983 op[0] = *expr; /* +=, -= or .= */
1984 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001986 else
1987 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (eap->skip)
1990 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (eap->skip)
1993 {
1994 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 --emsg_skip;
1997 }
1998 else if (i != FAIL)
1999 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
2005}
2006
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007/*
2008 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2009 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 * When "nextchars" is not NULL it points to a string with characters that
2011 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2012 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 * Returns OK or FAIL;
2014 */
2015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002016ex_let_vars(
2017 char_u *arg_start,
2018 typval_T *tv,
2019 int copy, /* copy values from "tv", don't move */
2020 int semicolon, /* from skip_var_list() */
2021 int var_count, /* from skip_var_list() */
2022 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023{
2024 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002025 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002027 listitem_T *item;
2028 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029
2030 if (*arg != '[')
2031 {
2032 /*
2033 * ":let var = expr" or ":for var in list"
2034 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 return FAIL;
2037 return OK;
2038 }
2039
2040 /*
2041 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2042 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002043 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 {
2045 EMSG(_(e_listreq));
2046 return FAIL;
2047 }
2048
2049 i = list_len(l);
2050 if (semicolon == 0 && var_count < i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055 if (var_count - semicolon > i)
2056 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002057 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 return FAIL;
2059 }
2060
2061 item = l->lv_first;
2062 while (*arg != ']')
2063 {
2064 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 item = item->li_next;
2067 if (arg == NULL)
2068 return FAIL;
2069
2070 arg = skipwhite(arg);
2071 if (*arg == ';')
2072 {
2073 /* Put the rest of the list (may be empty) in the var after ';'.
2074 * Create a new list for this. */
2075 l = list_alloc();
2076 if (l == NULL)
2077 return FAIL;
2078 while (item != NULL)
2079 {
2080 list_append_tv(l, &item->li_tv);
2081 item = item->li_next;
2082 }
2083
2084 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002085 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 ltv.vval.v_list = l;
2087 l->lv_refcount = 1;
2088
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2090 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 clear_tv(&ltv);
2092 if (arg == NULL)
2093 return FAIL;
2094 break;
2095 }
2096 else if (*arg != ',' && *arg != ']')
2097 {
2098 EMSG2(_(e_intern2), "ex_let_vars()");
2099 return FAIL;
2100 }
2101 }
2102
2103 return OK;
2104}
2105
2106/*
2107 * Skip over assignable variable "var" or list of variables "[var, var]".
2108 * Used for ":let varvar = expr" and ":for varvar in expr".
2109 * For "[var, var]" increment "*var_count" for each variable.
2110 * for "[var, var; var]" set "semicolon".
2111 * Return NULL for an error.
2112 */
2113 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114skip_var_list(
2115 char_u *arg,
2116 int *var_count,
2117 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118{
2119 char_u *p, *s;
2120
2121 if (*arg == '[')
2122 {
2123 /* "[var, var]": find the matching ']'. */
2124 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002125 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126 {
2127 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2128 s = skip_var_one(p);
2129 if (s == p)
2130 {
2131 EMSG2(_(e_invarg2), p);
2132 return NULL;
2133 }
2134 ++*var_count;
2135
2136 p = skipwhite(s);
2137 if (*p == ']')
2138 break;
2139 else if (*p == ';')
2140 {
2141 if (*semicolon == 1)
2142 {
2143 EMSG(_("Double ; in list of variables"));
2144 return NULL;
2145 }
2146 *semicolon = 1;
2147 }
2148 else if (*p != ',')
2149 {
2150 EMSG2(_(e_invarg2), p);
2151 return NULL;
2152 }
2153 }
2154 return p + 1;
2155 }
2156 else
2157 return skip_var_one(arg);
2158}
2159
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002161 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002167 if (*arg == '@' && arg[1] != NUL)
2168 return arg + 2;
2169 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2170 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002171}
2172
Bram Moolenaara7043832005-01-21 11:56:39 +00002173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 * List variables for hashtab "ht" with prefix "prefix".
2175 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_hashtable_vars(
2179 hashtab_T *ht,
2180 char_u *prefix,
2181 int empty,
2182 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183{
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 hashitem_T *hi;
2185 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 int todo;
2187
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002189 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2190 {
2191 if (!HASHITEM_EMPTY(hi))
2192 {
2193 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002194 di = HI2DI(hi);
2195 if (empty || di->di_tv.v_type != VAR_STRING
2196 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 }
2199 }
2200}
2201
2202/*
2203 * List global variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002207{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002209}
2210
2211/*
2212 * List buffer variables.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002216{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 char_u numbuf[NUMBUFLEN];
2218
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221
2222 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2224 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002225}
2226
2227/*
2228 * List window variables.
2229 */
2230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002232{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002233 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002235}
2236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237#ifdef FEAT_WINDOWS
2238/*
2239 * List tab page variables.
2240 */
2241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002244 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002246}
2247#endif
2248
Bram Moolenaara7043832005-01-21 11:56:39 +00002249/*
2250 * List Vim variables.
2251 */
2252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256}
2257
2258/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002259 * List script-local variables, if there is a script.
2260 */
2261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002262list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002263{
2264 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002265 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2266 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002267}
2268
2269/*
2270 * List function variables, if there is a function.
2271 */
2272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002273list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274{
2275 if (current_funccal != NULL)
2276 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002278}
2279
2280/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 * List variables in "arg".
2282 */
2283 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002284list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285{
2286 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 char_u *name_start;
2290 char_u *arg_subsc;
2291 char_u *tofree;
2292 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293
2294 while (!ends_excmd(*arg) && !got_int)
2295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002298 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2300 {
2301 emsg_severe = TRUE;
2302 EMSG(_(e_trailing));
2303 break;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* get_name_len() takes care of expanding curly braces */
2309 name_start = name = arg;
2310 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2311 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 /* This is mainly to keep test 49 working: when expanding
2314 * curly braces fails overrule the exception error message. */
2315 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 emsg_severe = TRUE;
2318 EMSG2(_(e_invarg2), arg);
2319 break;
2320 }
2321 error = TRUE;
2322 }
2323 else
2324 {
2325 if (tofree != NULL)
2326 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002327 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 else
2330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331 /* handle d.key, l[idx], f(expr) */
2332 arg_subsc = arg;
2333 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002340 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 case 'g': list_glob_vars(first); break;
2342 case 'b': list_buf_vars(first); break;
2343 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002344#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002345 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002346#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002347 case 'v': list_vim_vars(first); break;
2348 case 's': list_script_vars(first); break;
2349 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 default:
2351 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002353 }
2354 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355 {
2356 char_u numbuf[NUMBUFLEN];
2357 char_u *tf;
2358 int c;
2359 char_u *s;
2360
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002361 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362 c = *arg;
2363 *arg = NUL;
2364 list_one_var_a((char_u *)"",
2365 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002366 tv.v_type,
2367 s == NULL ? (char_u *)"" : s,
2368 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002369 *arg = c;
2370 vim_free(tf);
2371 }
2372 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
2375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002376
2377 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002379
2380 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 }
2382
2383 return arg;
2384}
2385
2386/*
2387 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2388 * Returns a pointer to the char just after the var name.
2389 * Returns NULL if there is an error.
2390 */
2391 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002392ex_let_one(
2393 char_u *arg, /* points to variable name */
2394 typval_T *tv, /* value to assign to variable */
2395 int copy, /* copy value from "tv" */
2396 char_u *endchars, /* valid chars after variable name or NULL */
2397 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398{
2399 int c1;
2400 char_u *name;
2401 char_u *p;
2402 char_u *arg_end = NULL;
2403 int len;
2404 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406
2407 /*
2408 * ":let $VAR = expr": Set environment variable.
2409 */
2410 if (*arg == '$')
2411 {
2412 /* Find the end of the name. */
2413 ++arg;
2414 name = arg;
2415 len = get_env_len(&arg);
2416 if (len == 0)
2417 EMSG2(_(e_invarg2), name - 1);
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 if (op != NULL && (*op == '+' || *op == '-'))
2421 EMSG2(_(e_letwrong), op);
2422 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002425 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
2427 c1 = name[len];
2428 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 p = get_tv_string_chk(tv);
2430 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
2432 int mustfree = FALSE;
2433 char_u *s = vim_getenv(name, &mustfree);
2434
2435 if (s != NULL)
2436 {
2437 p = tofree = concat_str(s, p);
2438 if (mustfree)
2439 vim_free(s);
2440 }
2441 }
2442 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 if (STRICMP(name, "HOME") == 0)
2446 init_homedir();
2447 else if (didset_vim && STRICMP(name, "VIM") == 0)
2448 didset_vim = FALSE;
2449 else if (didset_vimruntime
2450 && STRICMP(name, "VIMRUNTIME") == 0)
2451 didset_vimruntime = FALSE;
2452 arg_end = arg;
2453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458 }
2459
2460 /*
2461 * ":let &option = expr": Set option value.
2462 * ":let &l:option = expr": Set local option value.
2463 * ":let &g:option = expr": Set global option value.
2464 */
2465 else if (*arg == '&')
2466 {
2467 /* Find the end of the name. */
2468 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 if (p == NULL || (endchars != NULL
2470 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 long n;
2475 int opt_type;
2476 long numval;
2477 char_u *stringval = NULL;
2478 char_u *s;
2479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 c1 = *p;
2481 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002483 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 s = get_tv_string_chk(tv); /* != NULL if number or string */
2485 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
2487 opt_type = get_option_value(arg, &numval,
2488 &stringval, opt_flags);
2489 if ((opt_type == 1 && *op == '.')
2490 || (opt_type == 0 && *op != '.'))
2491 EMSG2(_(e_letwrong), op);
2492 else
2493 {
2494 if (opt_type == 1) /* number */
2495 {
2496 if (*op == '+')
2497 n = numval + n;
2498 else
2499 n = numval - n;
2500 }
2501 else if (opt_type == 0 && stringval != NULL) /* string */
2502 {
2503 s = concat_str(stringval, s);
2504 vim_free(stringval);
2505 stringval = s;
2506 }
2507 }
2508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 if (s != NULL)
2510 {
2511 set_option_value(arg, n, s, opt_flags);
2512 arg_end = p;
2513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 }
2517 }
2518
2519 /*
2520 * ":let @r = expr": Set register contents.
2521 */
2522 else if (*arg == '@')
2523 {
2524 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 if (op != NULL && (*op == '+' || *op == '-'))
2526 EMSG2(_(e_letwrong), op);
2527 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 EMSG(_(e_letunexp));
2530 else
2531 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002532 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 char_u *s;
2534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 p = get_tv_string_chk(tv);
2536 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002538 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 if (s != NULL)
2540 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002541 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 vim_free(s);
2543 }
2544 }
2545 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 arg_end = arg + 1;
2549 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002550 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 }
2552 }
2553
2554 /*
2555 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002558 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2566 EMSG(_(e_letunexp));
2567 else
2568 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 arg_end = p;
2571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
2575
2576 else
2577 EMSG2(_(e_invarg2), arg);
2578
2579 return arg_end;
2580}
2581
2582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2584 */
2585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002586check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587{
2588 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2589 {
2590 EMSG2(_(e_readonlyvar), arg);
2591 return TRUE;
2592 }
2593 return FALSE;
2594}
2595
2596/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 * Get an lval: variable, Dict item or List item that can be assigned a value
2598 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2599 * "name.key", "name.key[expr]" etc.
2600 * Indexing only works if "name" is an existing List or Dictionary.
2601 * "name" points to the start of the name.
2602 * If "rettv" is not NULL it points to the value to be assigned.
2603 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2604 * wrong; must end in space or cmd separator.
2605 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002606 * flags:
2607 * GLV_QUIET: do not give error messages
2608 * GLV_NO_AUTOLOAD: do not use script autoloading
2609 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002611 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 */
2614 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002615get_lval(
2616 char_u *name,
2617 typval_T *rettv,
2618 lval_T *lp,
2619 int unlet,
2620 int skip,
2621 int flags, /* GLV_ values */
2622 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 char_u *expr_start, *expr_end;
2626 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 dictitem_T *v;
2628 typval_T var1;
2629 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002635 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639
2640 if (skip)
2641 {
2642 /* When skipping just find the end of the name. */
2643 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002644 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 }
2646
2647 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002648 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (expr_start != NULL)
2650 {
2651 /* Don't expand the name when we already know there is an error. */
2652 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2653 && *p != '[' && *p != '.')
2654 {
2655 EMSG(_(e_trailing));
2656 return NULL;
2657 }
2658
2659 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2660 if (lp->ll_exp_name == NULL)
2661 {
2662 /* Report an invalid expression in braces, unless the
2663 * expression evaluation has been cancelled due to an
2664 * aborting error, an interrupt, or an exception. */
2665 if (!aborting() && !quiet)
2666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002667 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 EMSG2(_(e_invarg2), name);
2669 return NULL;
2670 }
2671 }
2672 lp->ll_name = lp->ll_exp_name;
2673 }
2674 else
2675 lp->ll_name = name;
2676
2677 /* Without [idx] or .key we are done. */
2678 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2679 return p;
2680
2681 cc = *p;
2682 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002683 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (v == NULL && !quiet)
2685 EMSG2(_(e_undefvar), lp->ll_name);
2686 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 if (v == NULL)
2688 return NULL;
2689
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 /*
2691 * Loop until no more [idx] or .key is following.
2692 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2697 && !(lp->ll_tv->v_type == VAR_DICT
2698 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E689: Can only index a List or Dictionary"));
2702 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
2707 EMSG(_("E708: [:] must come last"));
2708 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002709 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 len = -1;
2712 if (*p == '.')
2713 {
2714 key = p + 1;
2715 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2716 ;
2717 if (len == 0)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
2721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 p = key + len;
2724 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 else
2726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*p == ':')
2730 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731 else
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 empty1 = FALSE;
2734 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var1) == NULL)
2737 {
2738 /* not a number or string */
2739 clear_tv(&var1);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
2743
2744 /* Optionally get the second index [ :expr]. */
2745 if (*p == ':')
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002750 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 if (!empty1)
2752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (rettv != NULL && (rettv->v_type != VAR_LIST
2756 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (!quiet)
2759 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (!empty1)
2761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 p = skipwhite(p + 1);
2765 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 else
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2771 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (!empty1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 if (get_tv_string_chk(&var2) == NULL)
2777 {
2778 /* not a number or string */
2779 if (!empty1)
2780 clear_tv(&var1);
2781 clear_tv(&var2);
2782 return NULL;
2783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (!quiet)
2793 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (!empty1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800
2801 /* Skip to past ']'. */
2802 ++p;
2803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
2807 if (len == -1)
2808 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002810 key = get_tv_string_chk(&var1); /* is number or string */
2811 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 lp->ll_list = NULL;
2818 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002819 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 /* When assigning to a scope dictionary check that a function and
2822 * variable name is valid (only variable name unless it is l: or
2823 * g: dictionary). Disallow overwriting a builtin function. */
2824 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002826 int prevval;
2827 int wrong;
2828
2829 if (len != -1)
2830 {
2831 prevval = key[len];
2832 key[len] = NUL;
2833 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002834 else
2835 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002836 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2837 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002839 || !valid_varname(key);
2840 if (len != -1)
2841 key[len] = prevval;
2842 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 return NULL;
2844 }
2845
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002848 /* Can't add "v:" variable. */
2849 if (lp->ll_dict == &vimvardict)
2850 {
2851 EMSG2(_(e_illvar), name);
2852 return NULL;
2853 }
2854
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002855 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 if (len == -1)
2861 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 if (len == -1)
2869 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 p = NULL;
2872 break;
2873 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002874 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002875 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002876 return NULL;
2877
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 if (len == -1)
2879 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 }
2882 else
2883 {
2884 /*
2885 * Get the number and item for the only or first index of the List.
2886 */
2887 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 else
2890 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002891 lp->ll_n1 = (long)get_tv_number(&var1);
2892 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 clear_tv(&var1);
2894 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_list = lp->ll_tv->vval.v_list;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002900 if (lp->ll_n1 < 0)
2901 {
2902 lp->ll_n1 = 0;
2903 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2904 }
2905 }
2906 if (lp->ll_li == NULL)
2907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 if (!quiet)
2911 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 }
2914
2915 /*
2916 * May need to find the item or absolute index for the second
2917 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 * When no index given: "lp->ll_empty2" is TRUE.
2919 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002923 lp->ll_n2 = (long)get_tv_number(&var2);
2924 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002930 {
2931 if (!quiet)
2932 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002934 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002936 }
2937
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2939 if (lp->ll_n1 < 0)
2940 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2941 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 {
2943 if (!quiet)
2944 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002946 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
2948
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 }
2952
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 return p;
2954}
2955
2956/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 */
2959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002960clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961{
2962 vim_free(lp->ll_exp_name);
2963 vim_free(lp->ll_newkey);
2964}
2965
2966/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 */
2971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972set_var_lval(
2973 lval_T *lp,
2974 char_u *endp,
2975 typval_T *rettv,
2976 int copy,
2977 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978{
2979 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002980 listitem_T *ri;
2981 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982
2983 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 cc = *endp;
2988 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 if (op != NULL && *op != '=')
2990 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002991 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002993 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002994 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002995 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002996 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002998 if ((di == NULL
2999 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3000 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3001 FALSE)))
3002 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 set_var(lp->ll_name, &tv, FALSE);
3004 clear_tv(&tv);
3005 }
3006 }
3007 else
3008 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003012 else if (tv_check_lock(lp->ll_newkey == NULL
3013 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003014 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003015 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 else if (lp->ll_range)
3017 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 listitem_T *ll_li = lp->ll_li;
3019 int ll_n1 = lp->ll_n1;
3020
3021 /*
3022 * Check whether any of the list items is locked
3023 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003024 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003025 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003026 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003027 return;
3028 ri = ri->li_next;
3029 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3030 break;
3031 ll_li = ll_li->li_next;
3032 ++ll_n1;
3033 }
3034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 /*
3036 * Assign the List values to the list items.
3037 */
3038 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 if (op != NULL && *op != '=')
3041 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3042 else
3043 {
3044 clear_tv(&lp->ll_li->li_tv);
3045 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 ri = ri->li_next;
3048 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3049 break;
3050 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003053 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003054 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 ri = NULL;
3056 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003057 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 lp->ll_li = lp->ll_li->li_next;
3060 ++lp->ll_n1;
3061 }
3062 if (ri != NULL)
3063 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else if (lp->ll_empty2
3065 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 : lp->ll_n1 != lp->ll_n2)
3067 EMSG(_("E711: List value has not enough items"));
3068 }
3069 else
3070 {
3071 /*
3072 * Assign to a List or Dictionary item.
3073 */
3074 if (lp->ll_newkey != NULL)
3075 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 if (op != NULL && *op != '=')
3077 {
3078 EMSG2(_(e_letwrong), op);
3079 return;
3080 }
3081
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003082 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003083 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 if (di == NULL)
3085 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003086 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3087 {
3088 vim_free(di);
3089 return;
3090 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003091 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003092 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 else if (op != NULL && *op != '=')
3094 {
3095 tv_op(lp->ll_tv, rettv, op);
3096 return;
3097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003098 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003100
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003101 /*
3102 * Assign the value to the variable or list item.
3103 */
3104 if (copy)
3105 copy_tv(rettv, lp->ll_tv);
3106 else
3107 {
3108 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003109 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003110 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003111 }
3112 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003113}
3114
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3117 * Returns OK or FAIL.
3118 */
3119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003121{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003122 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 char_u numbuf[NUMBUFLEN];
3124 char_u *s;
3125
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003126 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3127 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3128 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 {
3130 switch (tv1->v_type)
3131 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003132 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 case VAR_DICT:
3134 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003135 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003136 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003137 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003138 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003139 break;
3140
3141 case VAR_LIST:
3142 if (*op != '+' || tv2->v_type != VAR_LIST)
3143 break;
3144 /* List += List */
3145 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3146 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3147 return OK;
3148
3149 case VAR_NUMBER:
3150 case VAR_STRING:
3151 if (tv2->v_type == VAR_LIST)
3152 break;
3153 if (*op == '+' || *op == '-')
3154 {
3155 /* nr += nr or nr -= nr*/
3156 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157#ifdef FEAT_FLOAT
3158 if (tv2->v_type == VAR_FLOAT)
3159 {
3160 float_T f = n;
3161
3162 if (*op == '+')
3163 f += tv2->vval.v_float;
3164 else
3165 f -= tv2->vval.v_float;
3166 clear_tv(tv1);
3167 tv1->v_type = VAR_FLOAT;
3168 tv1->vval.v_float = f;
3169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#endif
3172 {
3173 if (*op == '+')
3174 n += get_tv_number(tv2);
3175 else
3176 n -= get_tv_number(tv2);
3177 clear_tv(tv1);
3178 tv1->v_type = VAR_NUMBER;
3179 tv1->vval.v_number = n;
3180 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 }
3182 else
3183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003184 if (tv2->v_type == VAR_FLOAT)
3185 break;
3186
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003187 /* str .= str */
3188 s = get_tv_string(tv1);
3189 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3190 clear_tv(tv1);
3191 tv1->v_type = VAR_STRING;
3192 tv1->vval.v_string = s;
3193 }
3194 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003197#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 {
3199 float_T f;
3200
3201 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3202 && tv2->v_type != VAR_NUMBER
3203 && tv2->v_type != VAR_STRING))
3204 break;
3205 if (tv2->v_type == VAR_FLOAT)
3206 f = tv2->vval.v_float;
3207 else
3208 f = get_tv_number(tv2);
3209 if (*op == '+')
3210 tv1->vval.v_float += f;
3211 else
3212 tv1->vval.v_float -= f;
3213 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003215 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003216 }
3217 }
3218
3219 EMSG2(_(e_letwrong), op);
3220 return FAIL;
3221}
3222
3223/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 * Add a watcher to a list.
3225 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228{
3229 lw->lw_next = l->lv_watch;
3230 l->lv_watch = lw;
3231}
3232
3233/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003234 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 * No warning when it isn't found...
3236 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 lwp = &l->lv_watch;
3243 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3244 {
3245 if (lw == lwrem)
3246 {
3247 *lwp = lw->lw_next;
3248 break;
3249 }
3250 lwp = &lw->lw_next;
3251 }
3252}
3253
3254/*
3255 * Just before removing an item from a list: advance watchers to the next
3256 * item.
3257 */
3258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260{
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262
3263 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3264 if (lw->lw_item == item)
3265 lw->lw_item = item->li_next;
3266}
3267
3268/*
3269 * Evaluate the expression used in a ":for var in expr" command.
3270 * "arg" points to "var".
3271 * Set "*errp" to TRUE for an error, FALSE otherwise;
3272 * Return a pointer that holds the info. Null when there is an error.
3273 */
3274 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003275eval_for_line(
3276 char_u *arg,
3277 int *errp,
3278 char_u **nextcmdp,
3279 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280{
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 typval_T tv;
3284 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285
3286 *errp = TRUE; /* default: there is an error */
3287
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 if (fi == NULL)
3290 return NULL;
3291
3292 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3293 if (expr == NULL)
3294 return fi;
3295
3296 expr = skipwhite(expr);
3297 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3298 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003299 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 return fi;
3301 }
3302
3303 if (skip)
3304 ++emsg_skip;
3305 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3306 {
3307 *errp = FALSE;
3308 if (!skip)
3309 {
3310 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003311 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003314 clear_tv(&tv);
3315 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003316 else if (l == NULL)
3317 {
3318 /* a null list is like an empty list: do nothing */
3319 clear_tv(&tv);
3320 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 else
3322 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003323 /* No need to increment the refcount, it's already set for the
3324 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 fi->fi_list = l;
3326 list_add_watch(l, &fi->fi_lw);
3327 fi->fi_lw.lw_item = l->lv_first;
3328 }
3329 }
3330 }
3331 if (skip)
3332 --emsg_skip;
3333
3334 return fi;
3335}
3336
3337/*
3338 * Use the first item in a ":for" list. Advance to the next.
3339 * Assign the values to the variable (list). "arg" points to the first one.
3340 * Return TRUE when a valid item was found, FALSE when at end of list or
3341 * something wrong.
3342 */
3343 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003346 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349
3350 item = fi->fi_lw.lw_item;
3351 if (item == NULL)
3352 result = FALSE;
3353 else
3354 {
3355 fi->fi_lw.lw_item = item->li_next;
3356 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3357 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3358 }
3359 return result;
3360}
3361
3362/*
3363 * Free the structure used to store info used by ":for".
3364 */
3365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003366free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367{
Bram Moolenaar33570922005-01-25 22:26:29 +00003368 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003370 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003371 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003372 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003373 list_unref(fi->fi_list);
3374 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 vim_free(fi);
3376}
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3379
3380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381set_context_for_expression(
3382 expand_T *xp,
3383 char_u *arg,
3384 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 int got_eq = FALSE;
3387 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003388 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 if (cmdidx == CMD_let)
3391 {
3392 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003393 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 {
3395 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003396 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003397 {
3398 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003400 if (vim_iswhite(*p))
3401 break;
3402 }
3403 return;
3404 }
3405 }
3406 else
3407 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3408 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 while ((xp->xp_pattern = vim_strpbrk(arg,
3410 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3411 {
3412 c = *xp->xp_pattern;
3413 if (c == '&')
3414 {
3415 c = xp->xp_pattern[1];
3416 if (c == '&')
3417 {
3418 ++xp->xp_pattern;
3419 xp->xp_context = cmdidx != CMD_let || got_eq
3420 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3421 }
3422 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003425 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3426 xp->xp_pattern += 2;
3427
3428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
3430 else if (c == '$')
3431 {
3432 /* environment variable */
3433 xp->xp_context = EXPAND_ENV_VARS;
3434 }
3435 else if (c == '=')
3436 {
3437 got_eq = TRUE;
3438 xp->xp_context = EXPAND_EXPRESSION;
3439 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003440 else if (c == '#'
3441 && xp->xp_context == EXPAND_EXPRESSION)
3442 {
3443 /* Autoload function/variable contains '#'. */
3444 break;
3445 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003446 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 && xp->xp_context == EXPAND_FUNCTIONS
3448 && vim_strchr(xp->xp_pattern, '(') == NULL)
3449 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003450 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 break;
3452 }
3453 else if (cmdidx != CMD_let || got_eq)
3454 {
3455 if (c == '"') /* string */
3456 {
3457 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3458 if (c == '\\' && xp->xp_pattern[1] != NUL)
3459 ++xp->xp_pattern;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '\'') /* literal string */
3463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3466 /* skip */ ;
3467 xp->xp_context = EXPAND_NOTHING;
3468 }
3469 else if (c == '|')
3470 {
3471 if (xp->xp_pattern[1] == '|')
3472 {
3473 ++xp->xp_pattern;
3474 xp->xp_context = EXPAND_EXPRESSION;
3475 }
3476 else
3477 xp->xp_context = EXPAND_COMMANDS;
3478 }
3479 else
3480 xp->xp_context = EXPAND_EXPRESSION;
3481 }
3482 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003483 /* Doesn't look like something valid, expand as an expression
3484 * anyway. */
3485 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 arg = xp->xp_pattern;
3487 if (*arg != NUL)
3488 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3489 /* skip */ ;
3490 }
3491 xp->xp_pattern = arg;
3492}
3493
3494#endif /* FEAT_CMDL_COMPL */
3495
3496/*
3497 * ":1,25call func(arg1, arg2)" function call.
3498 */
3499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 char_u *arg = eap->arg;
3503 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003505 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003507 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 linenr_T lnum;
3509 int doesrange;
3510 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003511 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003512 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003514 if (eap->skip)
3515 {
3516 /* trans_function_name() doesn't work well when skipping, use eval0()
3517 * instead to skip to any following command, e.g. for:
3518 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003519 ++emsg_skip;
3520 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3521 clear_tv(&rettv);
3522 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003523 return;
3524 }
3525
Bram Moolenaar65639032016-03-16 21:40:30 +01003526 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003527 if (fudi.fd_newkey != NULL)
3528 {
3529 /* Still need to give an error message for missing key. */
3530 EMSG2(_(e_dictkey), fudi.fd_newkey);
3531 vim_free(fudi.fd_newkey);
3532 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003533 if (tofree == NULL)
3534 return;
3535
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003536 /* Increase refcount on dictionary, it could get deleted when evaluating
3537 * the arguments. */
3538 if (fudi.fd_dict != NULL)
3539 ++fudi.fd_dict->dv_refcount;
3540
Bram Moolenaar65639032016-03-16 21:40:30 +01003541 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3542 * contents. For VAR_PARTIAL get its partial, unless we already have one
3543 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003544 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003545 name = deref_func_name(tofree, &len,
3546 partial != NULL ? NULL : &partial, FALSE);
3547
Bram Moolenaar532c7802005-01-27 14:44:31 +00003548 /* Skip white space to allow ":call func ()". Not good, but required for
3549 * backward compatibility. */
3550 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
3553 if (*startarg != '(')
3554 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003555 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 goto end;
3557 }
3558
3559 /*
3560 * When skipping, evaluate the function once, to find the end of the
3561 * arguments.
3562 * When the function takes a range, this is discovered after the first
3563 * call, and the loop is broken.
3564 */
3565 if (eap->skip)
3566 {
3567 ++emsg_skip;
3568 lnum = eap->line2; /* do it once, also with an invalid range */
3569 }
3570 else
3571 lnum = eap->line1;
3572 for ( ; lnum <= eap->line2; ++lnum)
3573 {
3574 if (!eap->skip && eap->addr_count > 0)
3575 {
3576 curwin->w_cursor.lnum = lnum;
3577 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003578#ifdef FEAT_VIRTUALEDIT
3579 curwin->w_cursor.coladd = 0;
3580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003583 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003584 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003585 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 failed = TRUE;
3588 break;
3589 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003590
3591 /* Handle a function returning a Funcref, Dictionary or List. */
3592 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3593 {
3594 failed = TRUE;
3595 break;
3596 }
3597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (doesrange || eap->skip)
3600 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003603 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003604 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003605 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (aborting())
3607 break;
3608 }
3609 if (eap->skip)
3610 --emsg_skip;
3611
3612 if (!failed)
3613 {
3614 /* Check for trailing illegal characters and a following command. */
3615 if (!ends_excmd(*arg))
3616 {
3617 emsg_severe = TRUE;
3618 EMSG(_(e_trailing));
3619 }
3620 else
3621 eap->nextcmd = check_nextcmd(arg);
3622 }
3623
3624end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003625 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003626 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627}
3628
3629/*
3630 * ":unlet[!] var1 ... " command.
3631 */
3632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003633ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 ex_unletlock(eap, eap->arg, 0);
3636}
3637
3638/*
3639 * ":lockvar" and ":unlockvar" commands
3640 */
3641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 int deep = 2;
3646
3647 if (eap->forceit)
3648 deep = -1;
3649 else if (vim_isdigit(*arg))
3650 {
3651 deep = getdigits(&arg);
3652 arg = skipwhite(arg);
3653 }
3654
3655 ex_unletlock(eap, arg, deep);
3656}
3657
3658/*
3659 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3660 */
3661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662ex_unletlock(
3663 exarg_T *eap,
3664 char_u *argstart,
3665 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666{
3667 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 do
3673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003675 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003676 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 if (lv.ll_name == NULL)
3678 error = TRUE; /* error but continue parsing */
3679 if (name_end == NULL || (!vim_iswhite(*name_end)
3680 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 if (name_end != NULL)
3683 {
3684 emsg_severe = TRUE;
3685 EMSG(_(e_trailing));
3686 }
3687 if (!(eap->skip || error))
3688 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 break;
3690 }
3691
3692 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 {
3694 if (eap->cmdidx == CMD_unlet)
3695 {
3696 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3697 error = TRUE;
3698 }
3699 else
3700 {
3701 if (do_lock_var(&lv, name_end, deep,
3702 eap->cmdidx == CMD_lockvar) == FAIL)
3703 error = TRUE;
3704 }
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 if (!eap->skip)
3708 clear_lval(&lv);
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 arg = skipwhite(name_end);
3711 } while (!ends_excmd(*arg));
3712
3713 eap->nextcmd = check_nextcmd(arg);
3714}
3715
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717do_unlet_var(
3718 lval_T *lp,
3719 char_u *name_end,
3720 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 int ret = OK;
3723 int cc;
3724
3725 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 cc = *name_end;
3728 *name_end = NUL;
3729
3730 /* Normal name or expanded name. */
3731 if (check_changedtick(lp->ll_name))
3732 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003733 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003734 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003736 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003737 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003738 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003740 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 else if (lp->ll_range)
3743 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003746 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003747
3748 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3749 {
3750 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003751 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003752 return FAIL;
3753 ll_li = li;
3754 ++ll_n1;
3755 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003756
3757 /* Delete a range of List items. */
3758 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3759 {
3760 li = lp->ll_li->li_next;
3761 listitem_remove(lp->ll_list, lp->ll_li);
3762 lp->ll_li = li;
3763 ++lp->ll_n1;
3764 }
3765 }
3766 else
3767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003769 /* unlet a List item. */
3770 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003771 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003772 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003773 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003774 }
3775
3776 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003777}
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779/*
3780 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 */
3783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003784do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 hashtab_T *ht;
3787 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003788 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003789 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003790 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 ht = find_var_ht(name, &varname);
3793 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003795 if (ht == &globvarht)
3796 d = &globvardict;
3797 else if (current_funccal != NULL
3798 && ht == &current_funccal->l_vars.dv_hashtab)
3799 d = &current_funccal->l_vars;
3800 else if (ht == &compat_hashtab)
3801 d = &vimvardict;
3802 else
3803 {
3804 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3805 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3806 }
3807 if (d == NULL)
3808 {
3809 EMSG2(_(e_intern2), "do_unlet()");
3810 return FAIL;
3811 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 hi = hash_find(ht, varname);
3813 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003814 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003815 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003816 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003817 || var_check_ro(di->di_flags, name, FALSE)
3818 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003819 return FAIL;
3820
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 delete_var(ht, hi);
3822 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003825 if (forceit)
3826 return OK;
3827 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 return FAIL;
3829}
3830
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831/*
3832 * Lock or unlock variable indicated by "lp".
3833 * "deep" is the levels to go (-1 for unlimited);
3834 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3835 */
3836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003837do_lock_var(
3838 lval_T *lp,
3839 char_u *name_end,
3840 int deep,
3841 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003842{
3843 int ret = OK;
3844 int cc;
3845 dictitem_T *di;
3846
3847 if (deep == 0) /* nothing to do */
3848 return OK;
3849
3850 if (lp->ll_tv == NULL)
3851 {
3852 cc = *name_end;
3853 *name_end = NUL;
3854
3855 /* Normal name or expanded name. */
3856 if (check_changedtick(lp->ll_name))
3857 ret = FAIL;
3858 else
3859 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003860 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003861 if (di == NULL)
3862 ret = FAIL;
3863 else
3864 {
3865 if (lock)
3866 di->di_flags |= DI_FLAGS_LOCK;
3867 else
3868 di->di_flags &= ~DI_FLAGS_LOCK;
3869 item_lock(&di->di_tv, deep, lock);
3870 }
3871 }
3872 *name_end = cc;
3873 }
3874 else if (lp->ll_range)
3875 {
3876 listitem_T *li = lp->ll_li;
3877
3878 /* (un)lock a range of List items. */
3879 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3880 {
3881 item_lock(&li->li_tv, deep, lock);
3882 li = li->li_next;
3883 ++lp->ll_n1;
3884 }
3885 }
3886 else if (lp->ll_list != NULL)
3887 /* (un)lock a List item. */
3888 item_lock(&lp->ll_li->li_tv, deep, lock);
3889 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003890 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003891 item_lock(&lp->ll_di->di_tv, deep, lock);
3892
3893 return ret;
3894}
3895
3896/*
3897 * Lock or unlock an item. "deep" is nr of levels to go.
3898 */
3899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003900item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003901{
3902 static int recurse = 0;
3903 list_T *l;
3904 listitem_T *li;
3905 dict_T *d;
3906 hashitem_T *hi;
3907 int todo;
3908
3909 if (recurse >= DICT_MAXNEST)
3910 {
3911 EMSG(_("E743: variable nested too deep for (un)lock"));
3912 return;
3913 }
3914 if (deep == 0)
3915 return;
3916 ++recurse;
3917
3918 /* lock/unlock the item itself */
3919 if (lock)
3920 tv->v_lock |= VAR_LOCKED;
3921 else
3922 tv->v_lock &= ~VAR_LOCKED;
3923
3924 switch (tv->v_type)
3925 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003926 case VAR_UNKNOWN:
3927 case VAR_NUMBER:
3928 case VAR_STRING:
3929 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003930 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003931 case VAR_FLOAT:
3932 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003933 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003934 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003935 break;
3936
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003937 case VAR_LIST:
3938 if ((l = tv->vval.v_list) != NULL)
3939 {
3940 if (lock)
3941 l->lv_lock |= VAR_LOCKED;
3942 else
3943 l->lv_lock &= ~VAR_LOCKED;
3944 if (deep < 0 || deep > 1)
3945 /* recursive: lock/unlock the items the List contains */
3946 for (li = l->lv_first; li != NULL; li = li->li_next)
3947 item_lock(&li->li_tv, deep - 1, lock);
3948 }
3949 break;
3950 case VAR_DICT:
3951 if ((d = tv->vval.v_dict) != NULL)
3952 {
3953 if (lock)
3954 d->dv_lock |= VAR_LOCKED;
3955 else
3956 d->dv_lock &= ~VAR_LOCKED;
3957 if (deep < 0 || deep > 1)
3958 {
3959 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003960 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3962 {
3963 if (!HASHITEM_EMPTY(hi))
3964 {
3965 --todo;
3966 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3967 }
3968 }
3969 }
3970 }
3971 }
3972 --recurse;
3973}
3974
Bram Moolenaara40058a2005-07-11 22:42:07 +00003975/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003976 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3977 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003978 */
3979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003981{
3982 return (tv->v_lock & VAR_LOCKED)
3983 || (tv->v_type == VAR_LIST
3984 && tv->vval.v_list != NULL
3985 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3986 || (tv->v_type == VAR_DICT
3987 && tv->vval.v_dict != NULL
3988 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3989}
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3992/*
3993 * Delete all "menutrans_" variables.
3994 */
3995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003996del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
Bram Moolenaar33570922005-01-25 22:26:29 +00003998 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004002 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 {
4005 if (!HASHITEM_EMPTY(hi))
4006 {
4007 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004008 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4009 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004010 }
4011 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014#endif
4015
4016#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4017
4018/*
4019 * Local string buffer for the next two functions to store a variable name
4020 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4021 * get_user_var_name().
4022 */
4023
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004024static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026static char_u *varnamebuf = NULL;
4027static int varnamebuflen = 0;
4028
4029/*
4030 * Function to concatenate a prefix and a variable name.
4031 */
4032 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004033cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 int len;
4036
4037 len = (int)STRLEN(name) + 3;
4038 if (len > varnamebuflen)
4039 {
4040 vim_free(varnamebuf);
4041 len += 10; /* some additional space */
4042 varnamebuf = alloc(len);
4043 if (varnamebuf == NULL)
4044 {
4045 varnamebuflen = 0;
4046 return NULL;
4047 }
4048 varnamebuflen = len;
4049 }
4050 *varnamebuf = prefix;
4051 varnamebuf[1] = ':';
4052 STRCPY(varnamebuf + 2, name);
4053 return varnamebuf;
4054}
4055
4056/*
4057 * Function given to ExpandGeneric() to obtain the list of user defined
4058 * (global/buffer/window/built-in) variable names.
4059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004061get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 static long_u gdone;
4064 static long_u bdone;
4065 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066#ifdef FEAT_WINDOWS
4067 static long_u tdone;
4068#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004069 static int vidx;
4070 static hashitem_T *hi;
4071 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004075 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004076#ifdef FEAT_WINDOWS
4077 tdone = 0;
4078#endif
4079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
4081 /* Global variables */
4082 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004084 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004086 else
4087 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004088 while (HASHITEM_EMPTY(hi))
4089 ++hi;
4090 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4091 return cat_prefix_varname('g', hi->hi_key);
4092 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004094
4095 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004096 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004099 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004101 else
4102 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004103 while (HASHITEM_EMPTY(hi))
4104 ++hi;
4105 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004107 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004109 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 return (char_u *)"b:changedtick";
4111 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004112
4113 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004114 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004115 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004117 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004118 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004119 else
4120 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004121 while (HASHITEM_EMPTY(hi))
4122 ++hi;
4123 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004125
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004126#ifdef FEAT_WINDOWS
4127 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004128 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004129 if (tdone < ht->ht_used)
4130 {
4131 if (tdone++ == 0)
4132 hi = ht->ht_array;
4133 else
4134 ++hi;
4135 while (HASHITEM_EMPTY(hi))
4136 ++hi;
4137 return cat_prefix_varname('t', hi->hi_key);
4138 }
4139#endif
4140
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 /* v: variables */
4142 if (vidx < VV_LEN)
4143 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 vim_free(varnamebuf);
4146 varnamebuf = NULL;
4147 varnamebuflen = 0;
4148 return NULL;
4149}
4150
4151#endif /* FEAT_CMDL_COMPL */
4152
4153/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004154 * Return TRUE if "pat" matches "text".
4155 * Does not use 'cpo' and always uses 'magic'.
4156 */
4157 static int
4158pattern_match(char_u *pat, char_u *text, int ic)
4159{
4160 int matches = FALSE;
4161 char_u *save_cpo;
4162 regmatch_T regmatch;
4163
4164 /* avoid 'l' flag in 'cpoptions' */
4165 save_cpo = p_cpo;
4166 p_cpo = (char_u *)"";
4167 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4168 if (regmatch.regprog != NULL)
4169 {
4170 regmatch.rm_ic = ic;
4171 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4172 vim_regfree(regmatch.regprog);
4173 }
4174 p_cpo = save_cpo;
4175 return matches;
4176}
4177
4178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * types for expressions.
4180 */
4181typedef enum
4182{
4183 TYPE_UNKNOWN = 0
4184 , TYPE_EQUAL /* == */
4185 , TYPE_NEQUAL /* != */
4186 , TYPE_GREATER /* > */
4187 , TYPE_GEQUAL /* >= */
4188 , TYPE_SMALLER /* < */
4189 , TYPE_SEQUAL /* <= */
4190 , TYPE_MATCH /* =~ */
4191 , TYPE_NOMATCH /* !~ */
4192} exptype_T;
4193
4194/*
4195 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4198 */
4199
4200/*
4201 * Handle zero level expression.
4202 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004204 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Return OK or FAIL.
4206 */
4207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004208eval0(
4209 char_u *arg,
4210 typval_T *rettv,
4211 char_u **nextcmd,
4212 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int ret;
4215 char_u *p;
4216
4217 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (ret == FAIL || !ends_excmd(*p))
4220 {
4221 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 /*
4224 * Report the invalid expression unless the expression evaluation has
4225 * been cancelled due to an aborting error, an interrupt, or an
4226 * exception.
4227 */
4228 if (!aborting())
4229 EMSG2(_(e_invexpr2), arg);
4230 ret = FAIL;
4231 }
4232 if (nextcmd != NULL)
4233 *nextcmd = check_nextcmd(p);
4234
4235 return ret;
4236}
4237
4238/*
4239 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004240 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 *
4242 * "arg" must point to the first non-white of the expression.
4243 * "arg" is advanced to the next non-white after the recognized expression.
4244 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004245 * Note: "rettv.v_lock" is not set.
4246 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * Return OK or FAIL.
4248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004253 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * Get the first variable.
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260
4261 if ((*arg)[0] == '?')
4262 {
4263 result = FALSE;
4264 if (evaluate)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 int error = FALSE;
4267
4268 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (error)
4272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
4281
4282 /*
4283 * Check for the ":".
4284 */
4285 if ((*arg)[0] != ':')
4286 {
4287 EMSG(_("E109: Missing ':' after '?'"));
4288 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return FAIL;
4291 }
4292
4293 /*
4294 * Get the third variable.
4295 */
4296 *arg = skipwhite(*arg + 1);
4297 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4298 {
4299 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302 }
4303 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004304 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306
4307 return OK;
4308}
4309
4310/*
4311 * Handle first level expression:
4312 * expr2 || expr2 || expr2 logical OR
4313 *
4314 * "arg" must point to the first non-white of the expression.
4315 * "arg" is advanced to the next non-white after the recognized expression.
4316 *
4317 * Return OK or FAIL.
4318 */
4319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004320eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
Bram Moolenaar33570922005-01-25 22:26:29 +00004322 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 long result;
4324 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
4327 /*
4328 * Get the first variable.
4329 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 return FAIL;
4332
4333 /*
4334 * Repeat until there is no following "||".
4335 */
4336 first = TRUE;
4337 result = FALSE;
4338 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4339 {
4340 if (evaluate && first)
4341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004344 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (error)
4346 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 first = FALSE;
4348 }
4349
4350 /*
4351 * Get the second variable.
4352 */
4353 *arg = skipwhite(*arg + 2);
4354 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4355 return FAIL;
4356
4357 /*
4358 * Compute the result.
4359 */
4360 if (evaluate && !result)
4361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 if (error)
4366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 if (evaluate)
4369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 rettv->v_type = VAR_NUMBER;
4371 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
4374
4375 return OK;
4376}
4377
4378/*
4379 * Handle second level expression:
4380 * expr3 && expr3 && expr3 logical AND
4381 *
4382 * "arg" must point to the first non-white of the expression.
4383 * "arg" is advanced to the next non-white after the recognized expression.
4384 *
4385 * Return OK or FAIL.
4386 */
4387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004388eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
Bram Moolenaar33570922005-01-25 22:26:29 +00004390 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 long result;
4392 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 /*
4402 * Repeat until there is no following "&&".
4403 */
4404 first = TRUE;
4405 result = TRUE;
4406 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4407 {
4408 if (evaluate && first)
4409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 if (error)
4414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 first = FALSE;
4416 }
4417
4418 /*
4419 * Get the second variable.
4420 */
4421 *arg = skipwhite(*arg + 2);
4422 if (eval4(arg, &var2, evaluate && result) == FAIL)
4423 return FAIL;
4424
4425 /*
4426 * Compute the result.
4427 */
4428 if (evaluate && result)
4429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 if (error)
4434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 if (evaluate)
4437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004438 rettv->v_type = VAR_NUMBER;
4439 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
4442
4443 return OK;
4444}
4445
4446/*
4447 * Handle third level expression:
4448 * var1 == var2
4449 * var1 =~ var2
4450 * var1 != var2
4451 * var1 !~ var2
4452 * var1 > var2
4453 * var1 >= var2
4454 * var1 < var2
4455 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004456 * var1 is var2
4457 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 *
4459 * "arg" must point to the first non-white of the expression.
4460 * "arg" is advanced to the next non-white after the recognized expression.
4461 *
4462 * Return OK or FAIL.
4463 */
4464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004465eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
Bram Moolenaar33570922005-01-25 22:26:29 +00004467 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 char_u *p;
4469 int i;
4470 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004473 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 char_u *s1, *s2;
4475 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478 /*
4479 * Get the first variable.
4480 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return FAIL;
4483
4484 p = *arg;
4485 switch (p[0])
4486 {
4487 case '=': if (p[1] == '=')
4488 type = TYPE_EQUAL;
4489 else if (p[1] == '~')
4490 type = TYPE_MATCH;
4491 break;
4492 case '!': if (p[1] == '=')
4493 type = TYPE_NEQUAL;
4494 else if (p[1] == '~')
4495 type = TYPE_NOMATCH;
4496 break;
4497 case '>': if (p[1] != '=')
4498 {
4499 type = TYPE_GREATER;
4500 len = 1;
4501 }
4502 else
4503 type = TYPE_GEQUAL;
4504 break;
4505 case '<': if (p[1] != '=')
4506 {
4507 type = TYPE_SMALLER;
4508 len = 1;
4509 }
4510 else
4511 type = TYPE_SEQUAL;
4512 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 case 'i': if (p[1] == 's')
4514 {
4515 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4516 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004517 i = p[len];
4518 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004519 {
4520 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4521 type_is = TRUE;
4522 }
4523 }
4524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 */
4530 if (type != TYPE_UNKNOWN)
4531 {
4532 /* extra question mark appended: ignore case */
4533 if (p[len] == '?')
4534 {
4535 ic = TRUE;
4536 ++len;
4537 }
4538 /* extra '#' appended: match case */
4539 else if (p[len] == '#')
4540 {
4541 ic = FALSE;
4542 ++len;
4543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004544 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else
4546 ic = p_ic;
4547
4548 /*
4549 * Get the second variable.
4550 */
4551 *arg = skipwhite(p + len);
4552 if (eval5(arg, &var2, evaluate) == FAIL)
4553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 return FAIL;
4556 }
4557
4558 if (evaluate)
4559 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 if (type_is && rettv->v_type != var2.v_type)
4561 {
4562 /* For "is" a different type always means FALSE, for "notis"
4563 * it means TRUE. */
4564 n1 = (type == TYPE_NEQUAL);
4565 }
4566 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4567 {
4568 if (type_is)
4569 {
4570 n1 = (rettv->v_type == var2.v_type
4571 && rettv->vval.v_list == var2.vval.v_list);
4572 if (type == TYPE_NEQUAL)
4573 n1 = !n1;
4574 }
4575 else if (rettv->v_type != var2.v_type
4576 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4577 {
4578 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004579 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004580 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004581 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004582 clear_tv(rettv);
4583 clear_tv(&var2);
4584 return FAIL;
4585 }
4586 else
4587 {
4588 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004589 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4590 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
4593 }
4594 }
4595
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004596 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4597 {
4598 if (type_is)
4599 {
4600 n1 = (rettv->v_type == var2.v_type
4601 && rettv->vval.v_dict == var2.vval.v_dict);
4602 if (type == TYPE_NEQUAL)
4603 n1 = !n1;
4604 }
4605 else if (rettv->v_type != var2.v_type
4606 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4607 {
4608 if (rettv->v_type != var2.v_type)
4609 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4610 else
4611 EMSG(_("E736: Invalid operation for Dictionary"));
4612 clear_tv(rettv);
4613 clear_tv(&var2);
4614 return FAIL;
4615 }
4616 else
4617 {
4618 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004619 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4620 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004621 if (type == TYPE_NEQUAL)
4622 n1 = !n1;
4623 }
4624 }
4625
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004626 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4627 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004628 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004630 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004631 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 clear_tv(rettv);
4633 clear_tv(&var2);
4634 return FAIL;
4635 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004636 if ((rettv->v_type == VAR_PARTIAL
4637 && rettv->vval.v_partial == NULL)
4638 || (var2.v_type == VAR_PARTIAL
4639 && var2.vval.v_partial == NULL))
4640 /* when a partial is NULL assume not equal */
4641 n1 = FALSE;
4642 else if (type_is)
4643 {
4644 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4645 /* strings are considered the same if their value is
4646 * the same */
4647 n1 = tv_equal(rettv, &var2, ic, FALSE);
4648 else if (rettv->v_type == VAR_PARTIAL
4649 && var2.v_type == VAR_PARTIAL)
4650 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4651 else
4652 n1 = FALSE;
4653 }
4654 else
4655 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004656 if (type == TYPE_NEQUAL)
4657 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004658 }
4659
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004660#ifdef FEAT_FLOAT
4661 /*
4662 * If one of the two variables is a float, compare as a float.
4663 * When using "=~" or "!~", always compare as string.
4664 */
4665 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4666 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4667 {
4668 float_T f1, f2;
4669
4670 if (rettv->v_type == VAR_FLOAT)
4671 f1 = rettv->vval.v_float;
4672 else
4673 f1 = get_tv_number(rettv);
4674 if (var2.v_type == VAR_FLOAT)
4675 f2 = var2.vval.v_float;
4676 else
4677 f2 = get_tv_number(&var2);
4678 n1 = FALSE;
4679 switch (type)
4680 {
4681 case TYPE_EQUAL: n1 = (f1 == f2); break;
4682 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4683 case TYPE_GREATER: n1 = (f1 > f2); break;
4684 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4685 case TYPE_SMALLER: n1 = (f1 < f2); break;
4686 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4687 case TYPE_UNKNOWN:
4688 case TYPE_MATCH:
4689 case TYPE_NOMATCH: break; /* avoid gcc warning */
4690 }
4691 }
4692#endif
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 /*
4695 * If one of the two variables is a number, compare as a number.
4696 * When using "=~" or "!~", always compare as string.
4697 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004698 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 n1 = get_tv_number(rettv);
4702 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 switch (type)
4704 {
4705 case TYPE_EQUAL: n1 = (n1 == n2); break;
4706 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4707 case TYPE_GREATER: n1 = (n1 > n2); break;
4708 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4709 case TYPE_SMALLER: n1 = (n1 < n2); break;
4710 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4711 case TYPE_UNKNOWN:
4712 case TYPE_MATCH:
4713 case TYPE_NOMATCH: break; /* avoid gcc warning */
4714 }
4715 }
4716 else
4717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 s1 = get_tv_string_buf(rettv, buf1);
4719 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4721 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4722 else
4723 i = 0;
4724 n1 = FALSE;
4725 switch (type)
4726 {
4727 case TYPE_EQUAL: n1 = (i == 0); break;
4728 case TYPE_NEQUAL: n1 = (i != 0); break;
4729 case TYPE_GREATER: n1 = (i > 0); break;
4730 case TYPE_GEQUAL: n1 = (i >= 0); break;
4731 case TYPE_SMALLER: n1 = (i < 0); break;
4732 case TYPE_SEQUAL: n1 = (i <= 0); break;
4733
4734 case TYPE_MATCH:
4735 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004736 n1 = pattern_match(s2, s1, ic);
4737 if (type == TYPE_NOMATCH)
4738 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 break;
4740
4741 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4742 }
4743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
4745 clear_tv(&var2);
4746 rettv->v_type = VAR_NUMBER;
4747 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 }
4750
4751 return OK;
4752}
4753
4754/*
4755 * Handle fourth level expression:
4756 * + number addition
4757 * - number subtraction
4758 * . string concatenation
4759 *
4760 * "arg" must point to the first non-white of the expression.
4761 * "arg" is advanced to the next non-white after the recognized expression.
4762 *
4763 * Return OK or FAIL.
4764 */
4765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004766eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaar33570922005-01-25 22:26:29 +00004768 typval_T var2;
4769 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004771 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772#ifdef FEAT_FLOAT
4773 float_T f1 = 0, f2 = 0;
4774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 char_u *s1, *s2;
4776 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4777 char_u *p;
4778
4779 /*
4780 * Get the first variable.
4781 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004782 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return FAIL;
4784
4785 /*
4786 * Repeat computing, until no '+', '-' or '.' is following.
4787 */
4788 for (;;)
4789 {
4790 op = **arg;
4791 if (op != '+' && op != '-' && op != '.')
4792 break;
4793
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794 if ((op != '+' || rettv->v_type != VAR_LIST)
4795#ifdef FEAT_FLOAT
4796 && (op == '.' || rettv->v_type != VAR_FLOAT)
4797#endif
4798 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 {
4800 /* For "list + ...", an illegal use of the first operand as
4801 * a number cannot be determined before evaluating the 2nd
4802 * operand: if this is also a list, all is ok.
4803 * For "something . ...", "something - ..." or "non-list + ...",
4804 * we know that the first operand needs to be a string or number
4805 * without evaluating the 2nd operand. So check before to avoid
4806 * side effects after an error. */
4807 if (evaluate && get_tv_string_chk(rettv) == NULL)
4808 {
4809 clear_tv(rettv);
4810 return FAIL;
4811 }
4812 }
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * Get the second variable.
4816 */
4817 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004818 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return FAIL;
4822 }
4823
4824 if (evaluate)
4825 {
4826 /*
4827 * Compute the result.
4828 */
4829 if (op == '.')
4830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4832 s2 = get_tv_string_buf_chk(&var2, buf2);
4833 if (s2 == NULL) /* type error ? */
4834 {
4835 clear_tv(rettv);
4836 clear_tv(&var2);
4837 return FAIL;
4838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004839 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 clear_tv(rettv);
4841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004844 else if (op == '+' && rettv->v_type == VAR_LIST
4845 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004846 {
4847 /* concatenate Lists */
4848 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4849 &var3) == FAIL)
4850 {
4851 clear_tv(rettv);
4852 clear_tv(&var2);
4853 return FAIL;
4854 }
4855 clear_tv(rettv);
4856 *rettv = var3;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 else
4859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004860 int error = FALSE;
4861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862#ifdef FEAT_FLOAT
4863 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 f1 = rettv->vval.v_float;
4866 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 else
4869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 n1 = get_tv_number_chk(rettv, &error);
4872 if (error)
4873 {
4874 /* This can only happen for "list + non-list". For
4875 * "non-list + ..." or "something - ...", we returned
4876 * before evaluating the 2nd operand. */
4877 clear_tv(rettv);
4878 return FAIL;
4879 }
4880#ifdef FEAT_FLOAT
4881 if (var2.v_type == VAR_FLOAT)
4882 f1 = n1;
4883#endif
4884 }
4885#ifdef FEAT_FLOAT
4886 if (var2.v_type == VAR_FLOAT)
4887 {
4888 f2 = var2.vval.v_float;
4889 n2 = 0;
4890 }
4891 else
4892#endif
4893 {
4894 n2 = get_tv_number_chk(&var2, &error);
4895 if (error)
4896 {
4897 clear_tv(rettv);
4898 clear_tv(&var2);
4899 return FAIL;
4900 }
4901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 f2 = n2;
4904#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004906 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907
4908#ifdef FEAT_FLOAT
4909 /* If there is a float on either side the result is a float. */
4910 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4911 {
4912 if (op == '+')
4913 f1 = f1 + f2;
4914 else
4915 f1 = f1 - f2;
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
4918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
4921 {
4922 if (op == '+')
4923 n1 = n1 + n2;
4924 else
4925 n1 = n1 - n2;
4926 rettv->v_type = VAR_NUMBER;
4927 rettv->vval.v_number = n1;
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 }
4933 return OK;
4934}
4935
4936/*
4937 * Handle fifth level expression:
4938 * * number multiplication
4939 * / number division
4940 * % number modulo
4941 *
4942 * "arg" must point to the first non-white of the expression.
4943 * "arg" is advanced to the next non-white after the recognized expression.
4944 *
4945 * Return OK or FAIL.
4946 */
4947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948eval6(
4949 char_u **arg,
4950 typval_T *rettv,
4951 int evaluate,
4952 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
Bram Moolenaar33570922005-01-25 22:26:29 +00004954 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004956 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 int use_float = FALSE;
4959 float_T f1 = 0, f2;
4960#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * Get the first variable.
4965 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 return FAIL;
4968
4969 /*
4970 * Repeat computing, until no '*', '/' or '%' is following.
4971 */
4972 for (;;)
4973 {
4974 op = **arg;
4975 if (op != '*' && op != '/' && op != '%')
4976 break;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (rettv->v_type == VAR_FLOAT)
4982 {
4983 f1 = rettv->vval.v_float;
4984 use_float = TRUE;
4985 n1 = 0;
4986 }
4987 else
4988#endif
4989 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004991 if (error)
4992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 else
4995 n1 = 0;
4996
4997 /*
4998 * Get the second variable.
4999 */
5000 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005001 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 return FAIL;
5003
5004 if (evaluate)
5005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006#ifdef FEAT_FLOAT
5007 if (var2.v_type == VAR_FLOAT)
5008 {
5009 if (!use_float)
5010 {
5011 f1 = n1;
5012 use_float = TRUE;
5013 }
5014 f2 = var2.vval.v_float;
5015 n2 = 0;
5016 }
5017 else
5018#endif
5019 {
5020 n2 = get_tv_number_chk(&var2, &error);
5021 clear_tv(&var2);
5022 if (error)
5023 return FAIL;
5024#ifdef FEAT_FLOAT
5025 if (use_float)
5026 f2 = n2;
5027#endif
5028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 /*
5031 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034#ifdef FEAT_FLOAT
5035 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 if (op == '*')
5038 f1 = f1 * f2;
5039 else if (op == '/')
5040 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005041# ifdef VMS
5042 /* VMS crashes on divide by zero, work around it */
5043 if (f2 == 0.0)
5044 {
5045 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005046 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005047 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005048 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005049 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005050 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005051 }
5052 else
5053 f1 = f1 / f2;
5054# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005055 /* We rely on the floating point library to handle divide
5056 * by zero to result in "inf" and not a crash. */
5057 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005058# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005062 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 return FAIL;
5064 }
5065 rettv->v_type = VAR_FLOAT;
5066 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 if (op == '*')
5072 n1 = n1 * n2;
5073 else if (op == '/')
5074 {
5075 if (n2 == 0) /* give an error message? */
5076 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005077#ifdef FEAT_NUM64
5078 if (n1 == 0)
5079 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5080 else if (n1 < 0)
5081 n1 = -0x7fffffffffffffff;
5082 else
5083 n1 = 0x7fffffffffffffff;
5084#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005085 if (n1 == 0)
5086 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5087 else if (n1 < 0)
5088 n1 = -0x7fffffffL;
5089 else
5090 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005091#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092 }
5093 else
5094 n1 = n1 / n2;
5095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 {
5098 if (n2 == 0) /* give an error message? */
5099 n1 = 0;
5100 else
5101 n1 = n1 % n2;
5102 }
5103 rettv->v_type = VAR_NUMBER;
5104 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 }
5108
5109 return OK;
5110}
5111
5112/*
5113 * Handle sixth level expression:
5114 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005115 * "string" string constant
5116 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 * &option-name option value
5118 * @r register contents
5119 * identifier variable value
5120 * function() function call
5121 * $VAR environment variable
5122 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005123 * [expr, expr] List
5124 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 *
5126 * Also handle:
5127 * ! in front logical NOT
5128 * - in front unary minus
5129 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * trailing [] subscript in String or List
5131 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 *
5133 * "arg" must point to the first non-white of the expression.
5134 * "arg" is advanced to the next non-white after the recognized expression.
5135 *
5136 * Return OK or FAIL.
5137 */
5138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005139eval7(
5140 char_u **arg,
5141 typval_T *rettv,
5142 int evaluate,
5143 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005145 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 int len;
5147 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 char_u *start_leader, *end_leader;
5149 int ret = OK;
5150 char_u *alias;
5151
5152 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005153 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
5159 * Skip '!' and '-' characters. They are handled later.
5160 */
5161 start_leader = *arg;
5162 while (**arg == '!' || **arg == '-' || **arg == '+')
5163 *arg = skipwhite(*arg + 1);
5164 end_leader = *arg;
5165
5166 switch (**arg)
5167 {
5168 /*
5169 * Number constant.
5170 */
5171 case '0':
5172 case '1':
5173 case '2':
5174 case '3':
5175 case '4':
5176 case '5':
5177 case '6':
5178 case '7':
5179 case '8':
5180 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 {
5182#ifdef FEAT_FLOAT
5183 char_u *p = skipdigits(*arg + 1);
5184 int get_float = FALSE;
5185
5186 /* We accept a float when the format matches
5187 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005188 * strict to avoid backwards compatibility problems.
5189 * Don't look for a float after the "." operator, so that
5190 * ":let vers = 1.2.3" doesn't fail. */
5191 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 get_float = TRUE;
5194 p = skipdigits(p + 2);
5195 if (*p == 'e' || *p == 'E')
5196 {
5197 ++p;
5198 if (*p == '-' || *p == '+')
5199 ++p;
5200 if (!vim_isdigit(*p))
5201 get_float = FALSE;
5202 else
5203 p = skipdigits(p + 1);
5204 }
5205 if (ASCII_ISALPHA(*p) || *p == '.')
5206 get_float = FALSE;
5207 }
5208 if (get_float)
5209 {
5210 float_T f;
5211
5212 *arg += string2float(*arg, &f);
5213 if (evaluate)
5214 {
5215 rettv->v_type = VAR_FLOAT;
5216 rettv->vval.v_float = f;
5217 }
5218 }
5219 else
5220#endif
5221 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005222 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 *arg += len;
5224 if (evaluate)
5225 {
5226 rettv->v_type = VAR_NUMBER;
5227 rettv->vval.v_number = n;
5228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 /*
5234 * String constant: "string".
5235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238
5239 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005243 break;
5244
5245 /*
5246 * List: [expr, expr]
5247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250
5251 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 * Dictionary: {key: val, key: val}
5253 */
5254 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5255 break;
5256
5257 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005258 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 break;
5262
5263 /*
5264 * Environment variable: $VAR.
5265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005266 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 break;
5268
5269 /*
5270 * Register contents: @r.
5271 */
5272 case '@': ++*arg;
5273 if (evaluate)
5274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005276 rettv->vval.v_string = get_reg_contents(**arg,
5277 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 if (**arg != NUL)
5280 ++*arg;
5281 break;
5282
5283 /*
5284 * nested expression: (expression).
5285 */
5286 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (**arg == ')')
5289 ++*arg;
5290 else if (ret == OK)
5291 {
5292 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 ret = FAIL;
5295 }
5296 break;
5297
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 break;
5300 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301
5302 if (ret == NOTDONE)
5303 {
5304 /*
5305 * Must be a variable or function name.
5306 * Can also be a curly-braces kind of name: {expr}.
5307 */
5308 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005309 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 if (alias != NULL)
5311 s = alias;
5312
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005313 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314 ret = FAIL;
5315 else
5316 {
5317 if (**arg == '(') /* recursive! */
5318 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005319 partial_T *partial;
5320
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 /* If "s" is the name of a variable of type VAR_FUNC
5322 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005323 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324
5325 /* Invoke the function. */
5326 ret = get_func_tv(s, len, rettv, arg,
5327 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005328 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005329
5330 /* If evaluate is FALSE rettv->v_type was not set in
5331 * get_func_tv, but it's needed in handle_subscript() to parse
5332 * what follows. So set it here. */
5333 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5334 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005335 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005336 rettv->v_type = VAR_FUNC;
5337 }
5338
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 /* Stop the expression evaluation when immediately
5340 * aborting on error, or when an interrupt occurred or
5341 * an exception was thrown but not caught. */
5342 if (aborting())
5343 {
5344 if (ret == OK)
5345 clear_tv(rettv);
5346 ret = FAIL;
5347 }
5348 }
5349 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005350 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005351 else
5352 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005354 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 }
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 *arg = skipwhite(*arg);
5358
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5360 * expr(expr). */
5361 if (ret == OK)
5362 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364 /*
5365 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5366 */
5367 if (ret == OK && evaluate && end_leader > start_leader)
5368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005370 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005371#ifdef FEAT_FLOAT
5372 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005374 if (rettv->v_type == VAR_FLOAT)
5375 f = rettv->vval.v_float;
5376 else
5377#endif
5378 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005379 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 clear_tv(rettv);
5382 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384 else
5385 {
5386 while (end_leader > start_leader)
5387 {
5388 --end_leader;
5389 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005390 {
5391#ifdef FEAT_FLOAT
5392 if (rettv->v_type == VAR_FLOAT)
5393 f = !f;
5394 else
5395#endif
5396 val = !val;
5397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005398 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005399 {
5400#ifdef FEAT_FLOAT
5401 if (rettv->v_type == VAR_FLOAT)
5402 f = -f;
5403 else
5404#endif
5405 val = -val;
5406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005407 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005408#ifdef FEAT_FLOAT
5409 if (rettv->v_type == VAR_FLOAT)
5410 {
5411 clear_tv(rettv);
5412 rettv->vval.v_float = f;
5413 }
5414 else
5415#endif
5416 {
5417 clear_tv(rettv);
5418 rettv->v_type = VAR_NUMBER;
5419 rettv->vval.v_number = val;
5420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423
5424 return ret;
5425}
5426
5427/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005428 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5429 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5431 */
5432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005433eval_index(
5434 char_u **arg,
5435 typval_T *rettv,
5436 int evaluate,
5437 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438{
5439 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 long len = -1;
5443 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446
Bram Moolenaara03f2332016-02-06 18:09:59 +01005447 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005449 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005450 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005451 if (verbose)
5452 EMSG(_("E695: Cannot index a Funcref"));
5453 return FAIL;
5454 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005455#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005456 if (verbose)
5457 EMSG(_(e_float_as_string));
5458 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005459#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005460 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005461 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005462 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005463 if (verbose)
5464 EMSG(_("E909: Cannot index a special variable"));
5465 return FAIL;
5466 case VAR_UNKNOWN:
5467 if (evaluate)
5468 return FAIL;
5469 /* FALLTHROUGH */
5470
5471 case VAR_STRING:
5472 case VAR_NUMBER:
5473 case VAR_LIST:
5474 case VAR_DICT:
5475 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005478 init_tv(&var1);
5479 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 /*
5483 * dict.name
5484 */
5485 key = *arg + 1;
5486 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5487 ;
5488 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 }
5492 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 /*
5495 * something[idx]
5496 *
5497 * Get the (first) variable from inside the [].
5498 */
5499 *arg = skipwhite(*arg + 1);
5500 if (**arg == ':')
5501 empty1 = TRUE;
5502 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5503 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005504 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5505 {
5506 /* not a number or string */
5507 clear_tv(&var1);
5508 return FAIL;
5509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510
5511 /*
5512 * Get the second variable from inside the [:].
5513 */
5514 if (**arg == ':')
5515 {
5516 range = TRUE;
5517 *arg = skipwhite(*arg + 1);
5518 if (**arg == ']')
5519 empty2 = TRUE;
5520 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005522 if (!empty1)
5523 clear_tv(&var1);
5524 return FAIL;
5525 }
5526 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5527 {
5528 /* not a number or string */
5529 if (!empty1)
5530 clear_tv(&var1);
5531 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 return FAIL;
5533 }
5534 }
5535
5536 /* Check for the ']'. */
5537 if (**arg != ']')
5538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005539 if (verbose)
5540 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 clear_tv(&var1);
5542 if (range)
5543 clear_tv(&var2);
5544 return FAIL;
5545 }
5546 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 }
5548
5549 if (evaluate)
5550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005551 n1 = 0;
5552 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 n1 = get_tv_number(&var1);
5555 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 }
5557 if (range)
5558 {
5559 if (empty2)
5560 n2 = -1;
5561 else
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 n2 = get_tv_number(&var2);
5564 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 }
5567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005570 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005571 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005572 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005573 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005574 case VAR_SPECIAL:
5575 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005576 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005577 break; /* not evaluating, skipping over subscript */
5578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 case VAR_NUMBER:
5580 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 len = (long)STRLEN(s);
5583 if (range)
5584 {
5585 /* The resulting variable is a substring. If the indexes
5586 * are out of range the result is empty. */
5587 if (n1 < 0)
5588 {
5589 n1 = len + n1;
5590 if (n1 < 0)
5591 n1 = 0;
5592 }
5593 if (n2 < 0)
5594 n2 = len + n2;
5595 else if (n2 >= len)
5596 n2 = len;
5597 if (n1 >= len || n2 < 0 || n1 > n2)
5598 s = NULL;
5599 else
5600 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5601 }
5602 else
5603 {
5604 /* The resulting variable is a string of a single
5605 * character. If the index is too big or negative the
5606 * result is empty. */
5607 if (n1 >= len || n1 < 0)
5608 s = NULL;
5609 else
5610 s = vim_strnsave(s + n1, 1);
5611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 clear_tv(rettv);
5613 rettv->v_type = VAR_STRING;
5614 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 break;
5616
5617 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 if (n1 < 0)
5620 n1 = len + n1;
5621 if (!empty1 && (n1 < 0 || n1 >= len))
5622 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005623 /* For a range we allow invalid values and return an empty
5624 * list. A list index out of range is an error. */
5625 if (!range)
5626 {
5627 if (verbose)
5628 EMSGN(_(e_listidx), n1);
5629 return FAIL;
5630 }
5631 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 if (range)
5634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005635 list_T *l;
5636 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637
5638 if (n2 < 0)
5639 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005640 else if (n2 >= len)
5641 n2 = len - 1;
5642 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005643 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 l = list_alloc();
5645 if (l == NULL)
5646 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 n1 <= n2; ++n1)
5649 {
5650 if (list_append_tv(l, &item->li_tv) == FAIL)
5651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005652 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 return FAIL;
5654 }
5655 item = item->li_next;
5656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 clear_tv(rettv);
5658 rettv->v_type = VAR_LIST;
5659 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005660 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005661 }
5662 else
5663 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005664 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 clear_tv(rettv);
5666 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667 }
5668 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669
5670 case VAR_DICT:
5671 if (range)
5672 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005673 if (verbose)
5674 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 if (len == -1)
5676 clear_tv(&var1);
5677 return FAIL;
5678 }
5679 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005680 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681
5682 if (len == -1)
5683 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005684 key = get_tv_string_chk(&var1);
5685 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 clear_tv(&var1);
5688 return FAIL;
5689 }
5690 }
5691
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005692 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005694 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005695 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 if (len == -1)
5697 clear_tv(&var1);
5698 if (item == NULL)
5699 return FAIL;
5700
5701 copy_tv(&item->di_tv, &var1);
5702 clear_tv(rettv);
5703 *rettv = var1;
5704 }
5705 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 }
5707 }
5708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 return OK;
5710}
5711
5712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 * Get an option value.
5714 * "arg" points to the '&' or '+' before the option name.
5715 * "arg" is advanced to character after the option name.
5716 * Return OK or FAIL.
5717 */
5718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719get_option_tv(
5720 char_u **arg,
5721 typval_T *rettv, /* when NULL, only check if option exists */
5722 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 char_u *option_end;
5725 long numval;
5726 char_u *stringval;
5727 int opt_type;
5728 int c;
5729 int working = (**arg == '+'); /* has("+option") */
5730 int ret = OK;
5731 int opt_flags;
5732
5733 /*
5734 * Isolate the option name and find its value.
5735 */
5736 option_end = find_option_end(arg, &opt_flags);
5737 if (option_end == NULL)
5738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005739 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 EMSG2(_("E112: Option name missing: %s"), *arg);
5741 return FAIL;
5742 }
5743
5744 if (!evaluate)
5745 {
5746 *arg = option_end;
5747 return OK;
5748 }
5749
5750 c = *option_end;
5751 *option_end = NUL;
5752 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005753 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 if (opt_type == -3) /* invalid name */
5756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 EMSG2(_("E113: Unknown option: %s"), *arg);
5759 ret = FAIL;
5760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 if (opt_type == -2) /* hidden string option */
5764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 rettv->v_type = VAR_STRING;
5766 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768 else if (opt_type == -1) /* hidden number option */
5769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770 rettv->v_type = VAR_NUMBER;
5771 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
5773 else if (opt_type == 1) /* number option */
5774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775 rettv->v_type = VAR_NUMBER;
5776 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778 else /* string option */
5779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005780 rettv->v_type = VAR_STRING;
5781 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 }
5784 else if (working && (opt_type == -2 || opt_type == -1))
5785 ret = FAIL;
5786
5787 *option_end = c; /* put back for error messages */
5788 *arg = option_end;
5789
5790 return ret;
5791}
5792
5793/*
5794 * Allocate a variable for a string constant.
5795 * Return OK or FAIL.
5796 */
5797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005798get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 char_u *p;
5801 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int extra = 0;
5803
5804 /*
5805 * Find the end of the string, skipping backslashed characters.
5806 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\' && p[1] != NUL)
5810 {
5811 ++p;
5812 /* A "\<x>" form occupies at least 4 characters, and produces up
5813 * to 6 characters: reserve space for 2 extra */
5814 if (*p == '<')
5815 extra += 2;
5816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818
5819 if (*p != '"')
5820 {
5821 EMSG2(_("E114: Missing quote: %s"), *arg);
5822 return FAIL;
5823 }
5824
5825 /* If only parsing, set *arg and return here */
5826 if (!evaluate)
5827 {
5828 *arg = p + 1;
5829 return OK;
5830 }
5831
5832 /*
5833 * Copy the string into allocated memory, handling backslashed
5834 * characters.
5835 */
5836 name = alloc((unsigned)(p - *arg + extra));
5837 if (name == NULL)
5838 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 rettv->v_type = VAR_STRING;
5840 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
5844 if (*p == '\\')
5845 {
5846 switch (*++p)
5847 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 case 'b': *name++ = BS; ++p; break;
5849 case 'e': *name++ = ESC; ++p; break;
5850 case 'f': *name++ = FF; ++p; break;
5851 case 'n': *name++ = NL; ++p; break;
5852 case 'r': *name++ = CAR; ++p; break;
5853 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
5855 case 'X': /* hex: "\x1", "\x12" */
5856 case 'x':
5857 case 'u': /* Unicode: "\u0023" */
5858 case 'U':
5859 if (vim_isxdigit(p[1]))
5860 {
5861 int n, nr;
5862 int c = toupper(*p);
5863
5864 if (c == 'X')
5865 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005866 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005868 else
5869 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 nr = 0;
5871 while (--n >= 0 && vim_isxdigit(p[1]))
5872 {
5873 ++p;
5874 nr = (nr << 4) + hex2nr(*p);
5875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#ifdef FEAT_MBYTE
5878 /* For "\u" store the number according to
5879 * 'encoding'. */
5880 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 else
5883#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 break;
5887
5888 /* octal: "\1", "\12", "\123" */
5889 case '0':
5890 case '1':
5891 case '2':
5892 case '3':
5893 case '4':
5894 case '5':
5895 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 case '7': *name = *p++ - '0';
5897 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *name = (*name << 3) + *p++ - '0';
5900 if (*p >= '0' && *p <= '7')
5901 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 break;
5905
5906 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (extra != 0)
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 break;
5912 }
5913 /* FALLTHROUGH */
5914
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 break;
5917 }
5918 }
5919 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 *arg = p + 1;
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 return OK;
5927}
5928
5929/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 * Return OK or FAIL.
5932 */
5933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 int reduce = 0;
5939
5940 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 */
5943 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005948 break;
5949 ++reduce;
5950 ++p;
5951 }
5952 }
5953
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 return FAIL;
5958 }
5959
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005961 if (!evaluate)
5962 {
5963 *arg = p + 1;
5964 return OK;
5965 }
5966
5967 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 */
5970 str = alloc((unsigned)((p - *arg) - reduce));
5971 if (str == NULL)
5972 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 rettv->v_type = VAR_STRING;
5974 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005975
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005978 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981 break;
5982 ++p;
5983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 *arg = p + 1;
5988
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 return OK;
5990}
5991
Bram Moolenaarddecc252016-04-06 22:59:37 +02005992 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005993partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005994{
5995 int i;
5996
5997 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005998 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005999 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006000 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006001 func_unref(pt->pt_name);
6002 vim_free(pt->pt_name);
6003 vim_free(pt);
6004}
6005
6006/*
6007 * Unreference a closure: decrement the reference count and free it when it
6008 * becomes zero.
6009 */
6010 void
6011partial_unref(partial_T *pt)
6012{
6013 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006014 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006015}
6016
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Allocate a variable for a List and fill it from "*arg".
6019 * Return OK or FAIL.
6020 */
6021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 list_T *l = NULL;
6025 typval_T tv;
6026 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027
6028 if (evaluate)
6029 {
6030 l = list_alloc();
6031 if (l == NULL)
6032 return FAIL;
6033 }
6034
6035 *arg = skipwhite(*arg + 1);
6036 while (**arg != ']' && **arg != NUL)
6037 {
6038 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6039 goto failret;
6040 if (evaluate)
6041 {
6042 item = listitem_alloc();
6043 if (item != NULL)
6044 {
6045 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006046 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 list_append(l, item);
6048 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006049 else
6050 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 }
6052
6053 if (**arg == ']')
6054 break;
6055 if (**arg != ',')
6056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006057 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 goto failret;
6059 }
6060 *arg = skipwhite(*arg + 1);
6061 }
6062
6063 if (**arg != ']')
6064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006065 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066failret:
6067 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006068 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 return FAIL;
6070 }
6071
6072 *arg = skipwhite(*arg + 1);
6073 if (evaluate)
6074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 rettv->v_type = VAR_LIST;
6076 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 ++l->lv_refcount;
6078 }
6079
6080 return OK;
6081}
6082
6083/*
6084 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006085 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006087 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006088list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 list_T *l;
6091
6092 l = (list_T *)alloc_clear(sizeof(list_T));
6093 if (l != NULL)
6094 {
6095 /* Prepend the list to the list of lists for garbage collection. */
6096 if (first_list != NULL)
6097 first_list->lv_used_prev = l;
6098 l->lv_used_prev = NULL;
6099 l->lv_used_next = first_list;
6100 first_list = l;
6101 }
6102 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103}
6104
6105/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006106 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006107 * Returns OK or FAIL.
6108 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006111{
6112 list_T *l = list_alloc();
6113
6114 if (l == NULL)
6115 return FAIL;
6116
6117 rettv->vval.v_list = l;
6118 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006119 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006120 ++l->lv_refcount;
6121 return OK;
6122}
6123
6124/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 * Unreference a list: decrement the reference count and free it when it
6126 * becomes zero.
6127 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006131 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133}
6134
6135/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006136 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Ignores the reference count.
6138 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006139 static void
6140list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141{
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006144 for (item = l->lv_first; item != NULL; item = l->lv_first)
6145 {
6146 /* Remove the item before deleting it. */
6147 l->lv_first = item->li_next;
6148 clear_tv(&item->li_tv);
6149 vim_free(item);
6150 }
6151}
6152
6153 static void
6154list_free_list(list_T *l)
6155{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006156 /* Remove the list from the list of lists for garbage collection. */
6157 if (l->lv_used_prev == NULL)
6158 first_list = l->lv_used_next;
6159 else
6160 l->lv_used_prev->lv_used_next = l->lv_used_next;
6161 if (l->lv_used_next != NULL)
6162 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 vim_free(l);
6165}
6166
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006167 void
6168list_free(list_T *l)
6169{
6170 if (!in_free_unref_items)
6171 {
6172 list_free_contents(l);
6173 list_free_list(l);
6174 }
6175}
6176
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177/*
6178 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006179 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006181 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006182listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185}
6186
6187/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006188 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006191listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006193 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 vim_free(item);
6195}
6196
6197/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006198 * Remove a list item from a List and free it. Also clears the value.
6199 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006202{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006203 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006204 listitem_free(item);
6205}
6206
6207/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 * Get the number of items in a list.
6209 */
6210 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 if (l == NULL)
6214 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216}
6217
6218/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 * Return TRUE when two lists have exactly the same values.
6220 */
6221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006222list_equal(
6223 list_T *l1,
6224 list_T *l2,
6225 int ic, /* ignore case for strings */
6226 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227{
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006229
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006230 if (l1 == NULL || l2 == NULL)
6231 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006232 if (l1 == l2)
6233 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006234 if (list_len(l1) != list_len(l2))
6235 return FALSE;
6236
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237 for (item1 = l1->lv_first, item2 = l2->lv_first;
6238 item1 != NULL && item2 != NULL;
6239 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006240 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241 return FALSE;
6242 return item1 == NULL && item2 == NULL;
6243}
6244
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006245/*
6246 * Return the dictitem that an entry in a hashtable points to.
6247 */
6248 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006249dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006250{
6251 return HI2DI(hi);
6252}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006254/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006255 * Return TRUE when two dictionaries have exactly the same key/values.
6256 */
6257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258dict_equal(
6259 dict_T *d1,
6260 dict_T *d2,
6261 int ic, /* ignore case for strings */
6262 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006263{
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 hashitem_T *hi;
6265 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006266 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006267
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006268 if (d1 == NULL && d2 == NULL)
6269 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006270 if (d1 == NULL || d2 == NULL)
6271 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006272 if (d1 == d2)
6273 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006274 if (dict_len(d1) != dict_len(d2))
6275 return FALSE;
6276
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006277 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 if (!HASHITEM_EMPTY(hi))
6281 {
6282 item2 = dict_find(d2, hi->hi_key, -1);
6283 if (item2 == NULL)
6284 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006285 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 return FALSE;
6287 --todo;
6288 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006289 }
6290 return TRUE;
6291}
6292
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293static int tv_equal_recurse_limit;
6294
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006295 static int
6296func_equal(
6297 typval_T *tv1,
6298 typval_T *tv2,
6299 int ic) /* ignore case */
6300{
6301 char_u *s1, *s2;
6302 dict_T *d1, *d2;
6303 int a1, a2;
6304 int i;
6305
6306 /* empty and NULL function name considered the same */
6307 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6308 : tv1->vval.v_partial->pt_name;
6309 if (s1 != NULL && *s1 == NUL)
6310 s1 = NULL;
6311 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6312 : tv2->vval.v_partial->pt_name;
6313 if (s2 != NULL && *s2 == NUL)
6314 s2 = NULL;
6315 if (s1 == NULL || s2 == NULL)
6316 {
6317 if (s1 != s2)
6318 return FALSE;
6319 }
6320 else if (STRCMP(s1, s2) != 0)
6321 return FALSE;
6322
6323 /* empty dict and NULL dict is different */
6324 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6325 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6326 if (d1 == NULL || d2 == NULL)
6327 {
6328 if (d1 != d2)
6329 return FALSE;
6330 }
6331 else if (!dict_equal(d1, d2, ic, TRUE))
6332 return FALSE;
6333
6334 /* empty list and no list considered the same */
6335 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6336 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6337 if (a1 != a2)
6338 return FALSE;
6339 for (i = 0; i < a1; ++i)
6340 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6341 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6342 return FALSE;
6343
6344 return TRUE;
6345}
6346
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006349 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006350 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 */
6352 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353tv_equal(
6354 typval_T *tv1,
6355 typval_T *tv2,
6356 int ic, /* ignore case */
6357 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006358{
6359 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006360 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006361 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006362 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006363
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006364 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006365 * recursiveness to a limit. We guess they are equal then.
6366 * A fixed limit has the problem of still taking an awful long time.
6367 * Reduce the limit every time running into it. That should work fine for
6368 * deeply linked structures that are not recursively linked and catch
6369 * recursiveness quickly. */
6370 if (!recursive)
6371 tv_equal_recurse_limit = 1000;
6372 if (recursive_cnt >= tv_equal_recurse_limit)
6373 {
6374 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006375 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006376 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006377
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006378 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6379 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006380 if ((tv1->v_type == VAR_FUNC
6381 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6382 && (tv2->v_type == VAR_FUNC
6383 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6384 {
6385 ++recursive_cnt;
6386 r = func_equal(tv1, tv2, ic);
6387 --recursive_cnt;
6388 return r;
6389 }
6390
6391 if (tv1->v_type != tv2->v_type)
6392 return FALSE;
6393
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006394 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006396 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006397 ++recursive_cnt;
6398 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6399 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006400 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006401
6402 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006403 ++recursive_cnt;
6404 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6405 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006406 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006407
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006408 case VAR_NUMBER:
6409 return tv1->vval.v_number == tv2->vval.v_number;
6410
6411 case VAR_STRING:
6412 s1 = get_tv_string_buf(tv1, buf1);
6413 s2 = get_tv_string_buf(tv2, buf2);
6414 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006415
6416 case VAR_SPECIAL:
6417 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006418
6419 case VAR_FLOAT:
6420#ifdef FEAT_FLOAT
6421 return tv1->vval.v_float == tv2->vval.v_float;
6422#endif
6423 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006424#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006425 return tv1->vval.v_job == tv2->vval.v_job;
6426#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006427 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006429 return tv1->vval.v_channel == tv2->vval.v_channel;
6430#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006431 case VAR_FUNC:
6432 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006433 case VAR_UNKNOWN:
6434 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006436
Bram Moolenaara03f2332016-02-06 18:09:59 +01006437 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6438 * does not equal anything, not even itself. */
6439 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440}
6441
6442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 * Locate item with index "n" in list "l" and return it.
6444 * A negative index is counted from the end; -1 is the last item.
6445 * Returns NULL when "n" is out of range.
6446 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006447 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006448list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449{
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 long idx;
6452
6453 if (l == NULL)
6454 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455
6456 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 n = l->lv_len + n;
6459
6460 /* Check for index out of range. */
6461 if (n < 0 || n >= l->lv_len)
6462 return NULL;
6463
6464 /* When there is a cached index may start search from there. */
6465 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 if (n < l->lv_idx / 2)
6468 {
6469 /* closest to the start of the list */
6470 item = l->lv_first;
6471 idx = 0;
6472 }
6473 else if (n > (l->lv_idx + l->lv_len) / 2)
6474 {
6475 /* closest to the end of the list */
6476 item = l->lv_last;
6477 idx = l->lv_len - 1;
6478 }
6479 else
6480 {
6481 /* closest to the cached index */
6482 item = l->lv_idx_item;
6483 idx = l->lv_idx;
6484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 }
6486 else
6487 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006488 if (n < l->lv_len / 2)
6489 {
6490 /* closest to the start of the list */
6491 item = l->lv_first;
6492 idx = 0;
6493 }
6494 else
6495 {
6496 /* closest to the end of the list */
6497 item = l->lv_last;
6498 idx = l->lv_len - 1;
6499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501
6502 while (n > idx)
6503 {
6504 /* search forward */
6505 item = item->li_next;
6506 ++idx;
6507 }
6508 while (n < idx)
6509 {
6510 /* search backward */
6511 item = item->li_prev;
6512 --idx;
6513 }
6514
6515 /* cache the used index */
6516 l->lv_idx = idx;
6517 l->lv_idx_item = item;
6518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 return item;
6520}
6521
6522/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006523 * Get list item "l[idx]" as a number.
6524 */
6525 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526list_find_nr(
6527 list_T *l,
6528 long idx,
6529 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006530{
6531 listitem_T *li;
6532
6533 li = list_find(l, idx);
6534 if (li == NULL)
6535 {
6536 if (errorp != NULL)
6537 *errorp = TRUE;
6538 return -1L;
6539 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006540 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006541}
6542
6543/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006544 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6545 */
6546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006548{
6549 listitem_T *li;
6550
6551 li = list_find(l, idx - 1);
6552 if (li == NULL)
6553 {
6554 EMSGN(_(e_listidx), idx);
6555 return NULL;
6556 }
6557 return get_tv_string(&li->li_tv);
6558}
6559
6560/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006561 * Locate "item" list "l" and return its index.
6562 * Returns -1 when "item" is not in the list.
6563 */
6564 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006566{
6567 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006569
6570 if (l == NULL)
6571 return -1;
6572 idx = 0;
6573 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6574 ++idx;
6575 if (li == NULL)
6576 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006577 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006578}
6579
6580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Append item "item" to the end of list "l".
6582 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
6586 if (l->lv_last == NULL)
6587 {
6588 /* empty list */
6589 l->lv_first = item;
6590 l->lv_last = item;
6591 item->li_prev = NULL;
6592 }
6593 else
6594 {
6595 l->lv_last->li_next = item;
6596 item->li_prev = l->lv_last;
6597 l->lv_last = item;
6598 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006599 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 item->li_next = NULL;
6601}
6602
6603/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006605 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006610 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611
Bram Moolenaar05159a02005-02-26 23:04:13 +00006612 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006614 copy_tv(tv, &li->li_tv);
6615 list_append(l, li);
6616 return OK;
6617}
6618
6619/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006620 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006621 * Return FAIL when out of memory.
6622 */
6623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006624list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006625{
6626 listitem_T *li = listitem_alloc();
6627
6628 if (li == NULL)
6629 return FAIL;
6630 li->li_tv.v_type = VAR_DICT;
6631 li->li_tv.v_lock = 0;
6632 li->li_tv.vval.v_dict = dict;
6633 list_append(list, li);
6634 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 return OK;
6636}
6637
6638/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006639 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006640 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006641 * Returns FAIL when out of memory.
6642 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006643 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006645{
6646 listitem_T *li = listitem_alloc();
6647
6648 if (li == NULL)
6649 return FAIL;
6650 list_append(l, li);
6651 li->li_tv.v_type = VAR_STRING;
6652 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006653 if (str == NULL)
6654 li->li_tv.vval.v_string = NULL;
6655 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006656 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006657 return FAIL;
6658 return OK;
6659}
6660
6661/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006662 * Append "n" to list "l".
6663 * Returns FAIL when out of memory.
6664 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006667{
6668 listitem_T *li;
6669
6670 li = listitem_alloc();
6671 if (li == NULL)
6672 return FAIL;
6673 li->li_tv.v_type = VAR_NUMBER;
6674 li->li_tv.v_lock = 0;
6675 li->li_tv.vval.v_number = n;
6676 list_append(l, li);
6677 return OK;
6678}
6679
6680/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006682 * If "item" is NULL append at the end.
6683 * Return FAIL when out of memory.
6684 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687{
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006689
6690 if (ni == NULL)
6691 return FAIL;
6692 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006693 list_insert(l, ni, item);
6694 return OK;
6695}
6696
6697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006699{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006700 if (item == NULL)
6701 /* Append new item at end of list. */
6702 list_append(l, ni);
6703 else
6704 {
6705 /* Insert new item before existing item. */
6706 ni->li_prev = item->li_prev;
6707 ni->li_next = item;
6708 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006709 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006710 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006711 ++l->lv_idx;
6712 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006713 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006714 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006715 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006716 l->lv_idx_item = NULL;
6717 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006719 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006720 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721}
6722
6723/*
6724 * Extend "l1" with "l2".
6725 * If "bef" is NULL append at the end, otherwise insert before this item.
6726 * Returns FAIL when out of memory.
6727 */
6728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730{
Bram Moolenaar33570922005-01-25 22:26:29 +00006731 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006732 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006734 /* We also quit the loop when we have inserted the original item count of
6735 * the list, avoid a hang when we extend a list with itself. */
6736 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006737 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6738 return FAIL;
6739 return OK;
6740}
6741
6742/*
6743 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6744 * Return FAIL when out of memory.
6745 */
6746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748{
Bram Moolenaar33570922005-01-25 22:26:29 +00006749 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006750
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006751 if (l1 == NULL || l2 == NULL)
6752 return FAIL;
6753
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006754 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006755 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756 if (l == NULL)
6757 return FAIL;
6758 tv->v_type = VAR_LIST;
6759 tv->vval.v_list = l;
6760
6761 /* append all items from the second list */
6762 return list_extend(l, l2, NULL);
6763}
6764
6765/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006766 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006767 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006769 * Returns NULL when out of memory.
6770 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006771 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006772list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006773{
Bram Moolenaar33570922005-01-25 22:26:29 +00006774 list_T *copy;
6775 listitem_T *item;
6776 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777
6778 if (orig == NULL)
6779 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780
6781 copy = list_alloc();
6782 if (copy != NULL)
6783 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006784 if (copyID != 0)
6785 {
6786 /* Do this before adding the items, because one of the items may
6787 * refer back to this list. */
6788 orig->lv_copyID = copyID;
6789 orig->lv_copylist = copy;
6790 }
6791 for (item = orig->lv_first; item != NULL && !got_int;
6792 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006793 {
6794 ni = listitem_alloc();
6795 if (ni == NULL)
6796 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006797 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006798 {
6799 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6800 {
6801 vim_free(ni);
6802 break;
6803 }
6804 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006806 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006807 list_append(copy, ni);
6808 }
6809 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 if (item != NULL)
6811 {
6812 list_unref(copy);
6813 copy = NULL;
6814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 }
6816
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 return copy;
6818}
6819
6820/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006821 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006822 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006823 * This used to be called list_remove, but that conflicts with a Sun header
6824 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006827vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006828{
Bram Moolenaar33570922005-01-25 22:26:29 +00006829 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006830
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006831 /* notify watchers */
6832 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006834 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006835 list_fix_watch(l, ip);
6836 if (ip == item2)
6837 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839
6840 if (item2->li_next == NULL)
6841 l->lv_last = item->li_prev;
6842 else
6843 item2->li_next->li_prev = item->li_prev;
6844 if (item->li_prev == NULL)
6845 l->lv_first = item2->li_next;
6846 else
6847 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006848 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849}
6850
6851/*
6852 * Return an allocated string with the string representation of a list.
6853 * May return NULL.
6854 */
6855 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006856list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857{
6858 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006859
6860 if (tv->vval.v_list == NULL)
6861 return NULL;
6862 ga_init2(&ga, (int)sizeof(char), 80);
6863 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006864 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6865 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006866 {
6867 vim_free(ga.ga_data);
6868 return NULL;
6869 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006870 ga_append(&ga, ']');
6871 ga_append(&ga, NUL);
6872 return (char_u *)ga.ga_data;
6873}
6874
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006875typedef struct join_S {
6876 char_u *s;
6877 char_u *tofree;
6878} join_T;
6879
6880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006881list_join_inner(
6882 garray_T *gap, /* to store the result in */
6883 list_T *l,
6884 char_u *sep,
6885 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006886 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006887 int copyID,
6888 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006889{
6890 int i;
6891 join_T *p;
6892 int len;
6893 int sumlen = 0;
6894 int first = TRUE;
6895 char_u *tofree;
6896 char_u numbuf[NUMBUFLEN];
6897 listitem_T *item;
6898 char_u *s;
6899
6900 /* Stringify each item in the list. */
6901 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6902 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006903 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6904 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006905 if (s == NULL)
6906 return FAIL;
6907
6908 len = (int)STRLEN(s);
6909 sumlen += len;
6910
Bram Moolenaarcde88542015-08-11 19:14:00 +02006911 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006912 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6913 if (tofree != NULL || s != numbuf)
6914 {
6915 p->s = s;
6916 p->tofree = tofree;
6917 }
6918 else
6919 {
6920 p->s = vim_strnsave(s, len);
6921 p->tofree = p->s;
6922 }
6923
6924 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006925 if (did_echo_string_emsg) /* recursion error, bail out */
6926 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006927 }
6928
6929 /* Allocate result buffer with its total size, avoid re-allocation and
6930 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6931 if (join_gap->ga_len >= 2)
6932 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6933 if (ga_grow(gap, sumlen + 2) == FAIL)
6934 return FAIL;
6935
6936 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6937 {
6938 if (first)
6939 first = FALSE;
6940 else
6941 ga_concat(gap, sep);
6942 p = ((join_T *)join_gap->ga_data) + i;
6943
6944 if (p->s != NULL)
6945 ga_concat(gap, p->s);
6946 line_breakcheck();
6947 }
6948
6949 return OK;
6950}
6951
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006952/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006953 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006954 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006955 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006956 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006957 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006958list_join(
6959 garray_T *gap,
6960 list_T *l,
6961 char_u *sep,
6962 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006963 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006964 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006966 garray_T join_ga;
6967 int retval;
6968 join_T *p;
6969 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006970
Bram Moolenaard39a7512015-04-16 22:51:22 +02006971 if (l->lv_len < 1)
6972 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006973 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006974 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6975 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006976
6977 /* Dispose each item in join_ga. */
6978 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006979 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006980 p = (join_T *)join_ga.ga_data;
6981 for (i = 0; i < join_ga.ga_len; ++i)
6982 {
6983 vim_free(p->tofree);
6984 ++p;
6985 }
6986 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988
6989 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006990}
6991
6992/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006993 * Return the next (unique) copy ID.
6994 * Used for serializing nested structures.
6995 */
6996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006997get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006998{
6999 current_copyID += COPYID_INC;
7000 return current_copyID;
7001}
7002
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007003/* Used by get_func_tv() */
7004static garray_T funcargs = GA_EMPTY;
7005
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007006/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 * Garbage collection for lists and dictionaries.
7008 *
7009 * We use reference counts to be able to free most items right away when they
7010 * are no longer used. But for composite items it's possible that it becomes
7011 * unused while the reference count is > 0: When there is a recursive
7012 * reference. Example:
7013 * :let l = [1, 2, 3]
7014 * :let d = {9: l}
7015 * :let l[1] = d
7016 *
7017 * Since this is quite unusual we handle this with garbage collection: every
7018 * once in a while find out which lists and dicts are not referenced from any
7019 * variable.
7020 *
7021 * Here is a good reference text about garbage collection (refers to Python
7022 * but it applies to all reference-counting mechanisms):
7023 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007024 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007025
7026/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007028 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007030 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007032garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007034 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 buf_T *buf;
7037 win_T *wp;
7038 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007039 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007040 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007041 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007042#ifdef FEAT_WINDOWS
7043 tabpage_T *tp;
7044#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007046 if (!testing)
7047 {
7048 /* Only do this once. */
7049 want_garbage_collect = FALSE;
7050 may_garbage_collect = FALSE;
7051 garbage_collect_at_exit = FALSE;
7052 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007053
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054 /* We advance by two because we add one for items referenced through
7055 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007056 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 /*
7059 * 1. Go through all accessible variables and mark all lists and dicts
7060 * with copyID.
7061 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007062
7063 /* Don't free variables in the previous_funccal list unless they are only
7064 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007065 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7067 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7069 NULL);
7070 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7071 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007072 }
7073
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 /* script-local variables */
7075 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077
7078 /* buffer-local variables */
7079 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7081 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082
7083 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007084 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7086 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007087#ifdef FEAT_AUTOCMD
7088 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7090 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007091#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007093#ifdef FEAT_WINDOWS
7094 /* tabpage-local variables */
7095 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7097 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007098#endif
7099
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102
7103 /* function-local variables */
7104 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7105 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7107 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 }
7109
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007110 /* function call arguments, if v:testing is set. */
7111 for (i = 0; i < funcargs.ga_len; ++i)
7112 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7113 copyID, NULL, NULL);
7114
Bram Moolenaard812df62008-11-09 12:46:09 +00007115 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007117
Bram Moolenaar1dced572012-04-05 16:54:08 +02007118#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007120#endif
7121
Bram Moolenaardb913952012-06-29 12:54:53 +02007122#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007123 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007124#endif
7125
7126#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007128#endif
7129
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007130#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007131 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007132 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007133#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007134#ifdef FEAT_NETBEANS_INTG
7135 abort = abort || set_ref_in_nb_channel(copyID);
7136#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007137
Bram Moolenaare3188e22016-05-31 21:13:04 +02007138#ifdef FEAT_TIMERS
7139 abort = abort || set_ref_in_timer(copyID);
7140#endif
7141
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007143 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007144 /*
7145 * 2. Free lists and dictionaries that are not referenced.
7146 */
7147 did_free = free_unref_items(copyID);
7148
7149 /*
7150 * 3. Check if any funccal can be freed now.
7151 */
7152 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007153 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 if (can_free_funccal(*pfc, copyID))
7155 {
7156 fc = *pfc;
7157 *pfc = fc->caller;
7158 free_funccal(fc, TRUE);
7159 did_free = TRUE;
7160 did_free_funccal = TRUE;
7161 }
7162 else
7163 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007164 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 if (did_free_funccal)
7166 /* When a funccal was freed some more items might be garbage
7167 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007168 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007169 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 else if (p_verbose > 0)
7171 {
7172 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7173 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007174
7175 return did_free;
7176}
7177
7178/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007179 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007180 */
7181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007183{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007184 dict_T *dd, *dd_next;
7185 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186 int did_free = FALSE;
7187
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007188 /* Let all "free" functions know that we are here. This means no
7189 * dictionaries, lists, channels or jobs are to be freed, because we will
7190 * do that here. */
7191 in_free_unref_items = TRUE;
7192
7193 /*
7194 * PASS 1: free the contents of the items. We don't free the items
7195 * themselves yet, so that it is possible to decrement refcount counters
7196 */
7197
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007199 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007201 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007202 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007204 /* Free the Dictionary and ordinary items it contains, but don't
7205 * recurse into Lists and Dictionaries, they will be in the list
7206 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007207 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007208 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210
7211 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007212 * Go through the list of lists and free items without the copyID.
7213 * But don't free a list that has a watcher (used in a for loop), these
7214 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007216 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7217 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7218 && ll->lv_watch == NULL)
7219 {
7220 /* Free the List and ordinary items it contains, but don't recurse
7221 * into Lists and Dictionaries, they will be in the list of dicts
7222 * or list of lists. */
7223 list_free_contents(ll);
7224 did_free = TRUE;
7225 }
7226
7227#ifdef FEAT_JOB_CHANNEL
7228 /* Go through the list of jobs and free items without the copyID. This
7229 * must happen before doing channels, because jobs refer to channels, but
7230 * the reference from the channel to the job isn't tracked. */
7231 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7232
7233 /* Go through the list of channels and free items without the copyID. */
7234 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7235#endif
7236
7237 /*
7238 * PASS 2: free the items themselves.
7239 */
7240 for (dd = first_dict; dd != NULL; dd = dd_next)
7241 {
7242 dd_next = dd->dv_used_next;
7243 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7244 dict_free_dict(dd);
7245 }
7246
7247 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007248 {
7249 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7251 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007252 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007253 /* Free the List and ordinary items it contains, but don't recurse
7254 * into Lists and Dictionaries, they will be in the list of dicts
7255 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007256 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007257 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007258 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007259
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007260#ifdef FEAT_JOB_CHANNEL
7261 /* Go through the list of jobs and free items without the copyID. This
7262 * must happen before doing channels, because jobs refer to channels, but
7263 * the reference from the channel to the job isn't tracked. */
7264 free_unused_jobs(copyID, COPYID_MASK);
7265
7266 /* Go through the list of channels and free items without the copyID. */
7267 free_unused_channels(copyID, COPYID_MASK);
7268#endif
7269
7270 in_free_unref_items = FALSE;
7271
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007272 return did_free;
7273}
7274
7275/*
7276 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007277 * "list_stack" is used to add lists to be marked. Can be NULL.
7278 *
7279 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007283{
7284 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007286 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007287 hashtab_T *cur_ht;
7288 ht_stack_T *ht_stack = NULL;
7289 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007290
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007291 cur_ht = ht;
7292 for (;;)
7293 {
7294 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007295 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007296 /* Mark each item in the hashtab. If the item contains a hashtab
7297 * it is added to ht_stack, if it contains a list it is added to
7298 * list_stack. */
7299 todo = (int)cur_ht->ht_used;
7300 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7301 if (!HASHITEM_EMPTY(hi))
7302 {
7303 --todo;
7304 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7305 &ht_stack, list_stack);
7306 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007307 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308
7309 if (ht_stack == NULL)
7310 break;
7311
7312 /* take an item from the stack */
7313 cur_ht = ht_stack->ht;
7314 tempitem = ht_stack;
7315 ht_stack = ht_stack->prev;
7316 free(tempitem);
7317 }
7318
7319 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007320}
7321
7322/*
7323 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007324 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7325 *
7326 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007327 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007330{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007331 listitem_T *li;
7332 int abort = FALSE;
7333 list_T *cur_l;
7334 list_stack_T *list_stack = NULL;
7335 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007336
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007337 cur_l = l;
7338 for (;;)
7339 {
7340 if (!abort)
7341 /* Mark each item in the list. If the item contains a hashtab
7342 * it is added to ht_stack, if it contains a list it is added to
7343 * list_stack. */
7344 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7345 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7346 ht_stack, &list_stack);
7347 if (list_stack == NULL)
7348 break;
7349
7350 /* take an item from the stack */
7351 cur_l = list_stack->list;
7352 tempitem = list_stack;
7353 list_stack = list_stack->prev;
7354 free(tempitem);
7355 }
7356
7357 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007358}
7359
7360/*
7361 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007362 * "list_stack" is used to add lists to be marked. Can be NULL.
7363 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7364 *
7365 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007366 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007368set_ref_in_item(
7369 typval_T *tv,
7370 int copyID,
7371 ht_stack_T **ht_stack,
7372 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007373{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007374 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007375
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007376 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007377 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007378 dict_T *dd = tv->vval.v_dict;
7379
Bram Moolenaara03f2332016-02-06 18:09:59 +01007380 if (dd != NULL && dd->dv_copyID != copyID)
7381 {
7382 /* Didn't see this dict yet. */
7383 dd->dv_copyID = copyID;
7384 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007385 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007386 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7387 }
7388 else
7389 {
7390 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7391 if (newitem == NULL)
7392 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007393 else
7394 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007395 newitem->ht = &dd->dv_hashtab;
7396 newitem->prev = *ht_stack;
7397 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007398 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007399 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007400 }
7401 }
7402 else if (tv->v_type == VAR_LIST)
7403 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007404 list_T *ll = tv->vval.v_list;
7405
Bram Moolenaara03f2332016-02-06 18:09:59 +01007406 if (ll != NULL && ll->lv_copyID != copyID)
7407 {
7408 /* Didn't see this list yet. */
7409 ll->lv_copyID = copyID;
7410 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007411 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007412 abort = set_ref_in_list(ll, copyID, ht_stack);
7413 }
7414 else
7415 {
7416 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007417 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007418 if (newitem == NULL)
7419 abort = TRUE;
7420 else
7421 {
7422 newitem->list = ll;
7423 newitem->prev = *list_stack;
7424 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007425 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007426 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007427 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007428 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007429 else if (tv->v_type == VAR_PARTIAL)
7430 {
7431 partial_T *pt = tv->vval.v_partial;
7432 int i;
7433
7434 /* A partial does not have a copyID, because it cannot contain itself.
7435 */
7436 if (pt != NULL)
7437 {
7438 if (pt->pt_dict != NULL)
7439 {
7440 typval_T dtv;
7441
7442 dtv.v_type = VAR_DICT;
7443 dtv.vval.v_dict = pt->pt_dict;
7444 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7445 }
7446
7447 for (i = 0; i < pt->pt_argc; ++i)
7448 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7449 ht_stack, list_stack);
7450 }
7451 }
7452#ifdef FEAT_JOB_CHANNEL
7453 else if (tv->v_type == VAR_JOB)
7454 {
7455 job_T *job = tv->vval.v_job;
7456 typval_T dtv;
7457
7458 if (job != NULL && job->jv_copyID != copyID)
7459 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007460 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007461 if (job->jv_channel != NULL)
7462 {
7463 dtv.v_type = VAR_CHANNEL;
7464 dtv.vval.v_channel = job->jv_channel;
7465 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7466 }
7467 if (job->jv_exit_partial != NULL)
7468 {
7469 dtv.v_type = VAR_PARTIAL;
7470 dtv.vval.v_partial = job->jv_exit_partial;
7471 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7472 }
7473 }
7474 }
7475 else if (tv->v_type == VAR_CHANNEL)
7476 {
7477 channel_T *ch =tv->vval.v_channel;
7478 int part;
7479 typval_T dtv;
7480 jsonq_T *jq;
7481 cbq_T *cq;
7482
7483 if (ch != NULL && ch->ch_copyID != copyID)
7484 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007485 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007486 for (part = PART_SOCK; part <= PART_IN; ++part)
7487 {
7488 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7489 jq = jq->jq_next)
7490 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7491 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7492 cq = cq->cq_next)
7493 if (cq->cq_partial != NULL)
7494 {
7495 dtv.v_type = VAR_PARTIAL;
7496 dtv.vval.v_partial = cq->cq_partial;
7497 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7498 }
7499 if (ch->ch_part[part].ch_partial != NULL)
7500 {
7501 dtv.v_type = VAR_PARTIAL;
7502 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7503 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7504 }
7505 }
7506 if (ch->ch_partial != NULL)
7507 {
7508 dtv.v_type = VAR_PARTIAL;
7509 dtv.vval.v_partial = ch->ch_partial;
7510 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7511 }
7512 if (ch->ch_close_partial != NULL)
7513 {
7514 dtv.v_type = VAR_PARTIAL;
7515 dtv.vval.v_partial = ch->ch_close_partial;
7516 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7517 }
7518 }
7519 }
7520#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007521 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007522}
7523
7524/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 * Allocate an empty header for a dictionary.
7526 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007527 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007528dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529{
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531
Bram Moolenaar33570922005-01-25 22:26:29 +00007532 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007535 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007536 if (first_dict != NULL)
7537 first_dict->dv_used_prev = d;
7538 d->dv_used_next = first_dict;
7539 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007540 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007541
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007543 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007544 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007545 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007546 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007547 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549}
7550
7551/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007552 * Allocate an empty dict for a return value.
7553 * Returns OK or FAIL.
7554 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007555 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007557{
7558 dict_T *d = dict_alloc();
7559
7560 if (d == NULL)
7561 return FAIL;
7562
7563 rettv->vval.v_dict = d;
7564 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007565 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007566 ++d->dv_refcount;
7567 return OK;
7568}
7569
7570
7571/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 * Unreference a Dictionary: decrement the reference count and free it when it
7573 * becomes zero.
7574 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007576dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007578 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007579 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580}
7581
7582/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007583 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * Ignores the reference count.
7585 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007586 static void
7587dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007591 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007593 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007594 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007595 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 if (!HASHITEM_EMPTY(hi))
7599 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007600 /* Remove the item before deleting it, just in case there is
7601 * something recursive causing trouble. */
7602 di = HI2DI(hi);
7603 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007604 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007605 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 --todo;
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007610}
7611
7612 static void
7613dict_free_dict(dict_T *d)
7614{
7615 /* Remove the dict from the list of dicts for garbage collection. */
7616 if (d->dv_used_prev == NULL)
7617 first_dict = d->dv_used_next;
7618 else
7619 d->dv_used_prev->dv_used_next = d->dv_used_next;
7620 if (d->dv_used_next != NULL)
7621 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 vim_free(d);
7623}
7624
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007625 void
7626dict_free(dict_T *d)
7627{
7628 if (!in_free_unref_items)
7629 {
7630 dict_free_contents(d);
7631 dict_free_dict(d);
7632 }
7633}
7634
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635/*
7636 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 * The "key" is copied to the new item.
7638 * Note that the value of the item "di_tv" still needs to be initialized!
7639 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007641 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643{
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007646 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007647 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007648 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007650 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653}
7654
7655/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656 * Make a copy of a Dictionary item.
7657 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007659dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007660{
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007662
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007663 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7664 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007665 if (di != NULL)
7666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007668 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 copy_tv(&org->di_tv, &di->di_tv);
7670 }
7671 return di;
7672}
7673
7674/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 * Remove item "item" from Dictionary "dict" and free it.
7676 */
7677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679{
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007681
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 if (HASHITEM_EMPTY(hi))
7684 EMSG2(_(e_intern2), "dictitem_remove()");
7685 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 dictitem_free(item);
7688}
7689
7690/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 * Free a dict item. Also clears the value.
7692 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007697 if (item->di_flags & DI_FLAGS_ALLOC)
7698 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699}
7700
7701/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007702 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7703 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007704 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007705 * Returns NULL when out of memory.
7706 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007708dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007709{
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 dict_T *copy;
7711 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007712 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714
7715 if (orig == NULL)
7716 return NULL;
7717
7718 copy = dict_alloc();
7719 if (copy != NULL)
7720 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007721 if (copyID != 0)
7722 {
7723 orig->dv_copyID = copyID;
7724 orig->dv_copydict = copy;
7725 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007726 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007727 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007728 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 --todo;
7732
7733 di = dictitem_alloc(hi->hi_key);
7734 if (di == NULL)
7735 break;
7736 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007737 {
7738 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7739 copyID) == FAIL)
7740 {
7741 vim_free(di);
7742 break;
7743 }
7744 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007745 else
7746 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7747 if (dict_add(copy, di) == FAIL)
7748 {
7749 dictitem_free(di);
7750 break;
7751 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754
Bram Moolenaare9a41262005-01-15 22:18:47 +00007755 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007756 if (todo > 0)
7757 {
7758 dict_unref(copy);
7759 copy = NULL;
7760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 }
7762
7763 return copy;
7764}
7765
7766/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007768 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007771dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772{
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774}
7775
Bram Moolenaar8c711452005-01-14 21:53:12 +00007776/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007777 * Add a number or string entry to dictionary "d".
7778 * When "str" is NULL use number "nr", otherwise use "str".
7779 * Returns FAIL when out of memory and when key already exists.
7780 */
7781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007782dict_add_nr_str(
7783 dict_T *d,
7784 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007785 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007786 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007787{
7788 dictitem_T *item;
7789
7790 item = dictitem_alloc((char_u *)key);
7791 if (item == NULL)
7792 return FAIL;
7793 item->di_tv.v_lock = 0;
7794 if (str == NULL)
7795 {
7796 item->di_tv.v_type = VAR_NUMBER;
7797 item->di_tv.vval.v_number = nr;
7798 }
7799 else
7800 {
7801 item->di_tv.v_type = VAR_STRING;
7802 item->di_tv.vval.v_string = vim_strsave(str);
7803 }
7804 if (dict_add(d, item) == FAIL)
7805 {
7806 dictitem_free(item);
7807 return FAIL;
7808 }
7809 return OK;
7810}
7811
7812/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007813 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007814 * Returns FAIL when out of memory and when key already exists.
7815 */
7816 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007817dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007818{
7819 dictitem_T *item;
7820
7821 item = dictitem_alloc((char_u *)key);
7822 if (item == NULL)
7823 return FAIL;
7824 item->di_tv.v_lock = 0;
7825 item->di_tv.v_type = VAR_LIST;
7826 item->di_tv.vval.v_list = list;
7827 if (dict_add(d, item) == FAIL)
7828 {
7829 dictitem_free(item);
7830 return FAIL;
7831 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007832 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007833 return OK;
7834}
7835
7836/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 * Get the number of items in a Dictionary.
7838 */
7839 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007842 if (d == NULL)
7843 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007844 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845}
7846
7847/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 * Find item "key[len]" in Dictionary "d".
7849 * If "len" is negative use strlen(key).
7850 * Returns NULL when not found.
7851 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007852 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855#define AKEYLEN 200
7856 char_u buf[AKEYLEN];
7857 char_u *akey;
7858 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007861 if (d == NULL)
7862 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863 if (len < 0)
7864 akey = key;
7865 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867 tofree = akey = vim_strnsave(key, len);
7868 if (akey == NULL)
7869 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007870 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007871 else
7872 {
7873 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007874 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 akey = buf;
7876 }
7877
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 vim_free(tofree);
7880 if (HASHITEM_EMPTY(hi))
7881 return NULL;
7882 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007883}
7884
7885/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007886 * Get a string item from a dictionary.
7887 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007888 * Returns NULL if the entry doesn't exist or out of memory.
7889 */
7890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007891get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007892{
7893 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007894 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007895
7896 di = dict_find(d, key, -1);
7897 if (di == NULL)
7898 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007899 s = get_tv_string(&di->di_tv);
7900 if (save && s != NULL)
7901 s = vim_strsave(s);
7902 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007903}
7904
7905/*
7906 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007907 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007908 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007909 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007910get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007911{
7912 dictitem_T *di;
7913
7914 di = dict_find(d, key, -1);
7915 if (di == NULL)
7916 return 0;
7917 return get_tv_number(&di->di_tv);
7918}
7919
7920/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 * Return an allocated string with the string representation of a Dictionary.
7922 * May return NULL.
7923 */
7924 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007925dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926{
7927 garray_T ga;
7928 int first = TRUE;
7929 char_u *tofree;
7930 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007934 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007936 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 return NULL;
7938 ga_init2(&ga, (int)sizeof(char), 80);
7939 ga_append(&ga, '{');
7940
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007941 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007942 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 --todo;
7947
7948 if (first)
7949 first = FALSE;
7950 else
7951 ga_concat(&ga, (char_u *)", ");
7952
7953 tofree = string_quote(hi->hi_key, FALSE);
7954 if (tofree != NULL)
7955 {
7956 ga_concat(&ga, tofree);
7957 vim_free(tofree);
7958 }
7959 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007960 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7961 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 if (s != NULL)
7963 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007964 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007965 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007966 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007967 line_breakcheck();
7968
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007971 if (todo > 0)
7972 {
7973 vim_free(ga.ga_data);
7974 return NULL;
7975 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976
7977 ga_append(&ga, '}');
7978 ga_append(&ga, NUL);
7979 return (char_u *)ga.ga_data;
7980}
7981
7982/*
7983 * Allocate a variable for a Dictionary and fill it from "*arg".
7984 * Return OK or FAIL. Returns NOTDONE for {expr}.
7985 */
7986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988{
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 dict_T *d = NULL;
7990 typval_T tvkey;
7991 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007992 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007994 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007995 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996
7997 /*
7998 * First check if it's not a curly-braces thing: {expr}.
7999 * Must do this without evaluating, otherwise a function may be called
8000 * twice. Unfortunately this means we need to call eval1() twice for the
8001 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008002 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008003 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 if (*start != '}')
8005 {
8006 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8007 return FAIL;
8008 if (*start == '}')
8009 return NOTDONE;
8010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011
8012 if (evaluate)
8013 {
8014 d = dict_alloc();
8015 if (d == NULL)
8016 return FAIL;
8017 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008018 tvkey.v_type = VAR_UNKNOWN;
8019 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008020
8021 *arg = skipwhite(*arg + 1);
8022 while (**arg != '}' && **arg != NUL)
8023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008024 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 goto failret;
8026 if (**arg != ':')
8027 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008028 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008029 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030 goto failret;
8031 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008032 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008034 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008035 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008036 {
8037 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008038 clear_tv(&tvkey);
8039 goto failret;
8040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042
8043 *arg = skipwhite(*arg + 1);
8044 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8045 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 if (evaluate)
8047 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 goto failret;
8049 }
8050 if (evaluate)
8051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008052 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 if (item != NULL)
8054 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008055 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008056 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 clear_tv(&tv);
8058 goto failret;
8059 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008060 item = dictitem_alloc(key);
8061 clear_tv(&tvkey);
8062 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008065 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008066 if (dict_add(d, item) == FAIL)
8067 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 }
8069 }
8070
8071 if (**arg == '}')
8072 break;
8073 if (**arg != ',')
8074 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008075 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 goto failret;
8077 }
8078 *arg = skipwhite(*arg + 1);
8079 }
8080
8081 if (**arg != '}')
8082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008083 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008084failret:
8085 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008086 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008087 return FAIL;
8088 }
8089
8090 *arg = skipwhite(*arg + 1);
8091 if (evaluate)
8092 {
8093 rettv->v_type = VAR_DICT;
8094 rettv->vval.v_dict = d;
8095 ++d->dv_refcount;
8096 }
8097
8098 return OK;
8099}
8100
Bram Moolenaar17a13432016-01-24 14:22:10 +01008101 static char *
8102get_var_special_name(int nr)
8103{
8104 switch (nr)
8105 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008106 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008107 case VVAL_TRUE: return "v:true";
8108 case VVAL_NONE: return "v:none";
8109 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008110 }
8111 EMSG2(_(e_intern2), "get_var_special_name()");
8112 return "42";
8113}
8114
Bram Moolenaar8c711452005-01-14 21:53:12 +00008115/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 * Return a string with the string representation of a variable.
8117 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008118 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008119 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008120 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8121 * "string()", otherwise does not put quotes around strings, as ":echo"
8122 * displays values.
8123 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8124 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008125 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 */
8127 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008128echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008129 typval_T *tv,
8130 char_u **tofree,
8131 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008132 int copyID,
8133 int echo_style,
8134 int restore_copyID,
8135 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008136{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 static int recurse = 0;
8138 char_u *r = NULL;
8139
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008141 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008142 if (!did_echo_string_emsg)
8143 {
8144 /* Only give this message once for a recursive call to avoid
8145 * flooding the user with errors. And stop iterating over lists
8146 * and dicts. */
8147 did_echo_string_emsg = TRUE;
8148 EMSG(_("E724: variable nested too deep for displaying"));
8149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008151 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008152 }
8153 ++recurse;
8154
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155 switch (tv->v_type)
8156 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008157 case VAR_STRING:
8158 if (echo_style && !dict_val)
8159 {
8160 *tofree = NULL;
8161 r = get_tv_string_buf(tv, numbuf);
8162 }
8163 else
8164 {
8165 *tofree = string_quote(tv->vval.v_string, FALSE);
8166 r = *tofree;
8167 }
8168 break;
8169
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008171 if (echo_style)
8172 {
8173 *tofree = NULL;
8174 r = tv->vval.v_string;
8175 }
8176 else
8177 {
8178 *tofree = string_quote(tv->vval.v_string, TRUE);
8179 r = *tofree;
8180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008181 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008182
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008183 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008184 {
8185 partial_T *pt = tv->vval.v_partial;
8186 char_u *fname = string_quote(pt == NULL ? NULL
8187 : pt->pt_name, FALSE);
8188 garray_T ga;
8189 int i;
8190 char_u *tf;
8191
8192 ga_init2(&ga, 1, 100);
8193 ga_concat(&ga, (char_u *)"function(");
8194 if (fname != NULL)
8195 {
8196 ga_concat(&ga, fname);
8197 vim_free(fname);
8198 }
8199 if (pt != NULL && pt->pt_argc > 0)
8200 {
8201 ga_concat(&ga, (char_u *)", [");
8202 for (i = 0; i < pt->pt_argc; ++i)
8203 {
8204 if (i > 0)
8205 ga_concat(&ga, (char_u *)", ");
8206 ga_concat(&ga,
8207 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8208 vim_free(tf);
8209 }
8210 ga_concat(&ga, (char_u *)"]");
8211 }
8212 if (pt != NULL && pt->pt_dict != NULL)
8213 {
8214 typval_T dtv;
8215
8216 ga_concat(&ga, (char_u *)", ");
8217 dtv.v_type = VAR_DICT;
8218 dtv.vval.v_dict = pt->pt_dict;
8219 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8220 vim_free(tf);
8221 }
8222 ga_concat(&ga, (char_u *)")");
8223
8224 *tofree = ga.ga_data;
8225 r = *tofree;
8226 break;
8227 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008229 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008230 if (tv->vval.v_list == NULL)
8231 {
8232 *tofree = NULL;
8233 r = NULL;
8234 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008235 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8236 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008237 {
8238 *tofree = NULL;
8239 r = (char_u *)"[...]";
8240 }
8241 else
8242 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008243 int old_copyID = tv->vval.v_list->lv_copyID;
8244
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008245 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008246 *tofree = list2string(tv, copyID, restore_copyID);
8247 if (restore_copyID)
8248 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008249 r = *tofree;
8250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008251 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008252
Bram Moolenaar8c711452005-01-14 21:53:12 +00008253 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008254 if (tv->vval.v_dict == NULL)
8255 {
8256 *tofree = NULL;
8257 r = NULL;
8258 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008259 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8260 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008261 {
8262 *tofree = NULL;
8263 r = (char_u *)"{...}";
8264 }
8265 else
8266 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008267 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008268 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008269 *tofree = dict2string(tv, copyID, restore_copyID);
8270 if (restore_copyID)
8271 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008272 r = *tofree;
8273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008274 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008275
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008276 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008277 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008278 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008279 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008280 *tofree = NULL;
8281 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008284 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008285#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008286 *tofree = NULL;
8287 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8288 r = numbuf;
8289 break;
8290#endif
8291
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008292 case VAR_SPECIAL:
8293 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008294 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008295 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008296 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008297
Bram Moolenaar8502c702014-06-17 12:51:16 +02008298 if (--recurse == 0)
8299 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008301}
8302
8303/*
8304 * Return a string with the string representation of a variable.
8305 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8306 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008307 * Does not put quotes around strings, as ":echo" displays values.
8308 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8309 * May return NULL.
8310 */
8311 static char_u *
8312echo_string(
8313 typval_T *tv,
8314 char_u **tofree,
8315 char_u *numbuf,
8316 int copyID)
8317{
8318 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8319}
8320
8321/*
8322 * Return a string with the string representation of a variable.
8323 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8324 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008325 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008326 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008327 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008329tv2string(
8330 typval_T *tv,
8331 char_u **tofree,
8332 char_u *numbuf,
8333 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008334{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008335 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008336}
8337
8338/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 * Return string "str" in ' quotes, doubling ' characters.
8340 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008341 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342 */
8343 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008344string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345{
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008347 char_u *p, *r, *s;
8348
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 len = (function ? 13 : 3);
8350 if (str != NULL)
8351 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008352 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 for (p = str; *p != NUL; mb_ptr_adv(p))
8354 if (*p == '\'')
8355 ++len;
8356 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008357 s = r = alloc(len);
8358 if (r != NULL)
8359 {
8360 if (function)
8361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008362 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008363 r += 10;
8364 }
8365 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008366 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008367 if (str != NULL)
8368 for (p = str; *p != NUL; )
8369 {
8370 if (*p == '\'')
8371 *r++ = '\'';
8372 MB_COPY_CHAR(p, r);
8373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 if (function)
8376 *r++ = ')';
8377 *r++ = NUL;
8378 }
8379 return s;
8380}
8381
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008382#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383/*
8384 * Convert the string "text" to a floating point number.
8385 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8386 * this always uses a decimal point.
8387 * Returns the length of the text that was consumed.
8388 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008390string2float(
8391 char_u *text,
8392 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393{
8394 char *s = (char *)text;
8395 float_T f;
8396
8397 f = strtod(s, &s);
8398 *value = f;
8399 return (int)((char_u *)s - text);
8400}
8401#endif
8402
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 * Get the value of an environment variable.
8405 * "arg" is pointing to the '$'. It is advanced to after the name.
8406 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008407 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 */
8409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008410get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 char_u *string = NULL;
8413 int len;
8414 int cc;
8415 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008416 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
8418 ++*arg;
8419 name = *arg;
8420 len = get_env_len(arg);
8421 if (evaluate)
8422 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008423 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008424 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008425
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008426 cc = name[len];
8427 name[len] = NUL;
8428 /* first try vim_getenv(), fast for normal environment vars */
8429 string = vim_getenv(name, &mustfree);
8430 if (string != NULL && *string != NUL)
8431 {
8432 if (!mustfree)
8433 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008435 else
8436 {
8437 if (mustfree)
8438 vim_free(string);
8439
8440 /* next try expanding things like $VIM and ${HOME} */
8441 string = expand_env_save(name - 1);
8442 if (string != NULL && *string == '$')
8443 {
8444 vim_free(string);
8445 string = NULL;
8446 }
8447 }
8448 name[len] = cc;
8449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->v_type = VAR_STRING;
8451 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453
8454 return OK;
8455}
8456
8457/*
8458 * Array with names and number of arguments of all internal functions
8459 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8460 */
8461static struct fst
8462{
8463 char *f_name; /* function name */
8464 char f_min_argc; /* minimal number of arguments */
8465 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008466 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008467 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468} functions[] =
8469{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008470#ifdef FEAT_FLOAT
8471 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008472 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008474 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008475 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"append", 2, 2, f_append},
8477 {"argc", 0, 0, f_argc},
8478 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008479 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008480 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008481#ifdef FEAT_FLOAT
8482 {"asin", 1, 1, f_asin}, /* WJMc */
8483#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008484 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008485 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008486 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008487 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008488 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008489 {"assert_notequal", 2, 3, f_assert_notequal},
8490 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008491 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008492#ifdef FEAT_FLOAT
8493 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008494 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008497 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"bufexists", 1, 1, f_bufexists},
8499 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8500 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8501 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8502 {"buflisted", 1, 1, f_buflisted},
8503 {"bufloaded", 1, 1, f_bufloaded},
8504 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008505 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008506 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"bufwinnr", 1, 1, f_bufwinnr},
8508 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008509 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008510 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008511 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512#ifdef FEAT_FLOAT
8513 {"ceil", 1, 1, f_ceil},
8514#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008516 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008517 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8518 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008519 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008520 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008521 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008522 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008523 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008524 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008525 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008526 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008527 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8528 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008529 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008530 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008531#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008532 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008533 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008535 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008537#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008538 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008539 {"complete_add", 1, 1, f_complete_add},
8540 {"complete_check", 0, 0, f_complete_check},
8541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008544#ifdef FEAT_FLOAT
8545 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008546 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008547#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008548 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008550 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008551 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008552 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008554 {"diff_filler", 1, 1, f_diff_filler},
8555 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008556 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008558 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {"eventhandler", 0, 0, f_eventhandler},
8560 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008561 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008563#ifdef FEAT_FLOAT
8564 {"exp", 1, 1, f_exp},
8565#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008566 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008567 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008568 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8570 {"filereadable", 1, 1, f_filereadable},
8571 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008572 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008573 {"finddir", 1, 3, f_finddir},
8574 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008575#ifdef FEAT_FLOAT
8576 {"float2nr", 1, 1, f_float2nr},
8577 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008578 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008579#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008580 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {"fnamemodify", 2, 2, f_fnamemodify},
8582 {"foldclosed", 1, 1, f_foldclosed},
8583 {"foldclosedend", 1, 1, f_foldclosedend},
8584 {"foldlevel", 1, 1, f_foldlevel},
8585 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008586 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008588 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008589 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008590 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008591 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008592 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {"getchar", 0, 1, f_getchar},
8594 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008595 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {"getcmdline", 0, 0, f_getcmdline},
8597 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008598 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008599 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008600 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008601 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008602 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008603 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {"getfsize", 1, 1, f_getfsize},
8605 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008606 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008607 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008608 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008609 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008610 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008611 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008612 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008613 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008615 {"gettabvar", 2, 3, f_gettabvar},
8616 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {"getwinposx", 0, 0, f_getwinposx},
8618 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008619 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008620 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008621 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008622 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008625 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008626 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8628 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8629 {"histadd", 2, 2, f_histadd},
8630 {"histdel", 1, 2, f_histdel},
8631 {"histget", 1, 2, f_histget},
8632 {"histnr", 1, 1, f_histnr},
8633 {"hlID", 1, 1, f_hlID},
8634 {"hlexists", 1, 1, f_hlexists},
8635 {"hostname", 0, 0, f_hostname},
8636 {"iconv", 3, 3, f_iconv},
8637 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008638 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008639 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008641 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {"inputrestore", 0, 0, f_inputrestore},
8643 {"inputsave", 0, 0, f_inputsave},
8644 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008646 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008648 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008649#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8650 {"isnan", 1, 1, f_isnan},
8651#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008652 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008654 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008655 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008656 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008657 {"job_start", 1, 2, f_job_start},
8658 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008659 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008660#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008661 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008662 {"js_decode", 1, 1, f_js_decode},
8663 {"js_encode", 1, 1, f_js_encode},
8664 {"json_decode", 1, 1, f_json_decode},
8665 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008666 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 {"libcall", 3, 3, f_libcall},
8670 {"libcallnr", 3, 3, f_libcallnr},
8671 {"line", 1, 1, f_line},
8672 {"line2byte", 1, 1, f_line2byte},
8673 {"lispindent", 1, 1, f_lispindent},
8674 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008675#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008676 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008677 {"log10", 1, 1, f_log10},
8678#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008679#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008680 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008681#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008682 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008683 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008684 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008685 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008686 {"matchadd", 2, 5, f_matchadd},
8687 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008688 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008689 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008690 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008691 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008692 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008693 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008694 {"max", 1, 1, f_max},
8695 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008696#ifdef vim_mkdir
8697 {"mkdir", 1, 3, f_mkdir},
8698#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008699 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008700#ifdef FEAT_MZSCHEME
8701 {"mzeval", 1, 1, f_mzeval},
8702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008704 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008705 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008706 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008707#ifdef FEAT_PERL
8708 {"perleval", 1, 1, f_perleval},
8709#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710#ifdef FEAT_FLOAT
8711 {"pow", 2, 2, f_pow},
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008714 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008715 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008716#ifdef FEAT_PYTHON3
8717 {"py3eval", 1, 1, f_py3eval},
8718#endif
8719#ifdef FEAT_PYTHON
8720 {"pyeval", 1, 1, f_pyeval},
8721#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008722 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008723 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008724 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008725#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008726 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008727#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008728 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {"remote_expr", 2, 3, f_remote_expr},
8730 {"remote_foreground", 1, 1, f_remote_foreground},
8731 {"remote_peek", 1, 2, f_remote_peek},
8732 {"remote_read", 1, 1, f_remote_read},
8733 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008734 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008736 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008739#ifdef FEAT_FLOAT
8740 {"round", 1, 1, f_round},
8741#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008742 {"screenattr", 2, 2, f_screenattr},
8743 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008744 {"screencol", 0, 0, f_screencol},
8745 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008746 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008747 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008748 {"searchpair", 3, 7, f_searchpair},
8749 {"searchpairpos", 3, 7, f_searchpairpos},
8750 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {"server2client", 2, 2, f_server2client},
8752 {"serverlist", 0, 0, f_serverlist},
8753 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008754 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008756 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008758 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008759 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008760 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008761 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008763 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008764 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008766#ifdef FEAT_CRYPT
8767 {"sha256", 1, 1, f_sha256},
8768#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008769 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008770 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008772#ifdef FEAT_FLOAT
8773 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008774 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008775#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008776 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008777 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008778 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008779 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008780 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 {"sqrt", 1, 1, f_sqrt},
8783 {"str2float", 1, 1, f_str2float},
8784#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008785 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008786 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008787 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008788 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#ifdef HAVE_STRFTIME
8790 {"strftime", 1, 2, f_strftime},
8791#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008792 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008793 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008794 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {"strlen", 1, 1, f_strlen},
8796 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008797 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008799 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008800 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {"substitute", 4, 4, f_substitute},
8802 {"synID", 3, 3, f_synID},
8803 {"synIDattr", 2, 3, f_synIDattr},
8804 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008805 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008806 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008807 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008808 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008809 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008810 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008811 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008812 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008813 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008814#ifdef FEAT_FLOAT
8815 {"tan", 1, 1, f_tan},
8816 {"tanh", 1, 1, f_tanh},
8817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008819 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8820 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008821 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8822#ifdef FEAT_JOB_CHANNEL
8823 {"test_null_channel", 0, 0, f_test_null_channel},
8824#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008825 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008826#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008827 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008828#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008829 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008830 {"test_null_partial", 0, 0, f_test_null_partial},
8831 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008832 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008833#ifdef FEAT_TIMERS
8834 {"timer_start", 2, 3, f_timer_start},
8835 {"timer_stop", 1, 1, f_timer_stop},
8836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {"tolower", 1, 1, f_tolower},
8838 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008839 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008840#ifdef FEAT_FLOAT
8841 {"trunc", 1, 1, f_trunc},
8842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008844 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008845 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008846 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008847 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {"virtcol", 1, 1, f_virtcol},
8849 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008850 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008851 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008852 {"win_getid", 0, 2, f_win_getid},
8853 {"win_gotoid", 1, 1, f_win_gotoid},
8854 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8855 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {"winbufnr", 1, 1, f_winbufnr},
8857 {"wincol", 0, 0, f_wincol},
8858 {"winheight", 1, 1, f_winheight},
8859 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008860 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008862 {"winrestview", 1, 1, f_winrestview},
8863 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008865 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008866 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008867 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868};
8869
8870#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8871
8872/*
8873 * Function given to ExpandGeneric() to obtain the list of internal
8874 * or user defined function names.
8875 */
8876 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008877get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 static int intidx = -1;
8880 char_u *name;
8881
8882 if (idx == 0)
8883 intidx = -1;
8884 if (intidx < 0)
8885 {
8886 name = get_user_func_name(xp, idx);
8887 if (name != NULL)
8888 return name;
8889 }
8890 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8891 {
8892 STRCPY(IObuff, functions[intidx].f_name);
8893 STRCAT(IObuff, "(");
8894 if (functions[intidx].f_max_argc == 0)
8895 STRCAT(IObuff, ")");
8896 return IObuff;
8897 }
8898
8899 return NULL;
8900}
8901
8902/*
8903 * Function given to ExpandGeneric() to obtain the list of internal or
8904 * user defined variable or function names.
8905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008907get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 static int intidx = -1;
8910 char_u *name;
8911
8912 if (idx == 0)
8913 intidx = -1;
8914 if (intidx < 0)
8915 {
8916 name = get_function_name(xp, idx);
8917 if (name != NULL)
8918 return name;
8919 }
8920 return get_user_var_name(xp, ++intidx);
8921}
8922
8923#endif /* FEAT_CMDL_COMPL */
8924
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008925#if defined(EBCDIC) || defined(PROTO)
8926/*
8927 * Compare struct fst by function name.
8928 */
8929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008930compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008931{
8932 struct fst *p1 = (struct fst *)s1;
8933 struct fst *p2 = (struct fst *)s2;
8934
8935 return STRCMP(p1->f_name, p2->f_name);
8936}
8937
8938/*
8939 * Sort the function table by function name.
8940 * The sorting of the table above is ASCII dependant.
8941 * On machines using EBCDIC we have to sort it.
8942 */
8943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008944sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008945{
8946 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8947
8948 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8949}
8950#endif
8951
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953/*
8954 * Find internal function in table above.
8955 * Return index, or -1 if not found
8956 */
8957 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008958find_internal_func(
8959 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961 int first = 0;
8962 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8963 int cmp;
8964 int x;
8965
8966 /*
8967 * Find the function name in the table. Binary search.
8968 */
8969 while (first <= last)
8970 {
8971 x = first + ((unsigned)(last - first) >> 1);
8972 cmp = STRCMP(name, functions[x].f_name);
8973 if (cmp < 0)
8974 last = x - 1;
8975 else if (cmp > 0)
8976 first = x + 1;
8977 else
8978 return x;
8979 }
8980 return -1;
8981}
8982
8983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8985 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008986 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8987 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 */
8989 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008990deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008991{
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 int cc;
8994
Bram Moolenaar65639032016-03-16 21:40:30 +01008995 if (partialp != NULL)
8996 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008998 cc = name[*lenp];
8999 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009000 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009002 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009004 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 {
9006 *lenp = 0;
9007 return (char_u *)""; /* just in case */
9008 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009009 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009011 }
9012
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009013 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9014 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009015 partial_T *pt = v->di_tv.vval.v_partial;
9016
9017 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009018 {
9019 *lenp = 0;
9020 return (char_u *)""; /* just in case */
9021 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009022 if (partialp != NULL)
9023 *partialp = pt;
9024 *lenp = (int)STRLEN(pt->pt_name);
9025 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009026 }
9027
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 return name;
9029}
9030
9031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 * Allocate a variable for the result of a function.
9033 * Return OK or FAIL.
9034 */
9035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009036get_func_tv(
9037 char_u *name, /* name of the function */
9038 int len, /* length of "name" */
9039 typval_T *rettv,
9040 char_u **arg, /* argument, pointing to the '(' */
9041 linenr_T firstline, /* first line of range */
9042 linenr_T lastline, /* last line of range */
9043 int *doesrange, /* return: function handled range */
9044 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009045 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 char_u *argp;
9049 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009050 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 int argcount = 0; /* number of arguments found */
9052
9053 /*
9054 * Get the arguments.
9055 */
9056 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009057 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 {
9059 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9060 if (*argp == ')' || *argp == ',' || *argp == NUL)
9061 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9063 {
9064 ret = FAIL;
9065 break;
9066 }
9067 ++argcount;
9068 if (*argp != ',')
9069 break;
9070 }
9071 if (*argp == ')')
9072 ++argp;
9073 else
9074 ret = FAIL;
9075
9076 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009077 {
9078 int i = 0;
9079
9080 if (get_vim_var_nr(VV_TESTING))
9081 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009082 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009083 * what variables are used on the call stack. */
9084 if (funcargs.ga_itemsize == 0)
9085 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9086 for (i = 0; i < argcount; ++i)
9087 if (ga_grow(&funcargs, 1) == OK)
9088 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9089 &argvars[i];
9090 }
9091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009093 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009094
9095 funcargs.ga_len -= i;
9096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 {
9099 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009100 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009101 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009102 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108 *arg = skipwhite(argp);
9109 return ret;
9110}
9111
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009112#define ERROR_UNKNOWN 0
9113#define ERROR_TOOMANY 1
9114#define ERROR_TOOFEW 2
9115#define ERROR_SCRIPT 3
9116#define ERROR_DICT 4
9117#define ERROR_NONE 5
9118#define ERROR_OTHER 6
9119#define FLEN_FIXED 40
9120
9121/*
9122 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9123 * Change <SNR>123_name() to K_SNR 123_name().
9124 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9125 * (slow).
9126 */
9127 static char_u *
9128fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9129{
9130 int llen;
9131 char_u *fname;
9132 int i;
9133
9134 llen = eval_fname_script(name);
9135 if (llen > 0)
9136 {
9137 fname_buf[0] = K_SPECIAL;
9138 fname_buf[1] = KS_EXTRA;
9139 fname_buf[2] = (int)KE_SNR;
9140 i = 3;
9141 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9142 {
9143 if (current_SID <= 0)
9144 *error = ERROR_SCRIPT;
9145 else
9146 {
9147 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9148 i = (int)STRLEN(fname_buf);
9149 }
9150 }
9151 if (i + STRLEN(name + llen) < FLEN_FIXED)
9152 {
9153 STRCPY(fname_buf + i, name + llen);
9154 fname = fname_buf;
9155 }
9156 else
9157 {
9158 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9159 if (fname == NULL)
9160 *error = ERROR_OTHER;
9161 else
9162 {
9163 *tofree = fname;
9164 mch_memmove(fname, fname_buf, (size_t)i);
9165 STRCPY(fname + i, name + llen);
9166 }
9167 }
9168 }
9169 else
9170 fname = name;
9171 return fname;
9172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173
9174/*
9175 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009176 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009177 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009179 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009180call_func(
9181 char_u *funcname, /* name of the function */
9182 int len, /* length of "name" */
9183 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009184 int argcount_in, /* number of "argvars" */
9185 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009186 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187 linenr_T firstline, /* first line of range */
9188 linenr_T lastline, /* last line of range */
9189 int *doesrange, /* return: function handled range */
9190 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009191 partial_T *partial, /* optional, can be NULL */
9192 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 int error = ERROR_NONE;
9196 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009199 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009201 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009202 int argcount = argcount_in;
9203 typval_T *argvars = argvars_in;
9204 dict_T *selfdict = selfdict_in;
9205 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9206 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009207
9208 /* Make a copy of the name, if it comes from a funcref variable it could
9209 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009210 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009211 if (name == NULL)
9212 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009214 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
9216 *doesrange = FALSE;
9217
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009218 if (partial != NULL)
9219 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009220 /* When the function has a partial with a dict and there is a dict
9221 * argument, use the dict argument. That is backwards compatible.
9222 * When the dict was bound explicitly use the one from the partial. */
9223 if (partial->pt_dict != NULL
9224 && (selfdict_in == NULL || !partial->pt_auto))
9225 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009226 if (error == ERROR_NONE && partial->pt_argc > 0)
9227 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009228 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9229 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9230 for (i = 0; i < argcount_in; ++i)
9231 argv[i + argv_clear] = argvars_in[i];
9232 argvars = argv;
9233 argcount = partial->pt_argc + argcount_in;
9234 }
9235 }
9236
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
9238 /* execute the function if no errors detected and executing */
9239 if (evaluate && error == ERROR_NONE)
9240 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009241 char_u *rfname = fname;
9242
9243 /* Ignore "g:" before a function name. */
9244 if (fname[0] == 'g' && fname[1] == ':')
9245 rfname = fname + 2;
9246
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009247 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9248 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 error = ERROR_UNKNOWN;
9250
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009251 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
9253 /*
9254 * User defined function.
9255 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009256 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009259 /* Trigger FuncUndefined event, may load the function. */
9260 if (fp == NULL
9261 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009262 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009263 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009265 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009266 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
9268#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009269 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009270 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009271 {
9272 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009273 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274 }
9275
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 if (fp != NULL)
9277 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009278 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009280 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009282 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009284 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009285 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 else
9287 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009288 int did_save_redo = FALSE;
9289
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 /*
9291 * Call the user function.
9292 * Save and restore search patterns, script variables and
9293 * redo buffer.
9294 */
9295 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009296#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009297 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009298#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009299 {
9300 saveRedobuff();
9301 did_save_redo = TRUE;
9302 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009303 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009305 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009306 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9307 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9308 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009309 /* Function was unreferenced while being used, free it
9310 * now. */
9311 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009312 if (did_save_redo)
9313 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 restore_search_patterns();
9315 error = ERROR_NONE;
9316 }
9317 }
9318 }
9319 else
9320 {
9321 /*
9322 * Find the function name in the table, call its implementation.
9323 */
9324 i = find_internal_func(fname);
9325 if (i >= 0)
9326 {
9327 if (argcount < functions[i].f_min_argc)
9328 error = ERROR_TOOFEW;
9329 else if (argcount > functions[i].f_max_argc)
9330 error = ERROR_TOOMANY;
9331 else
9332 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 error = ERROR_NONE;
9336 }
9337 }
9338 }
9339 /*
9340 * The function call (or "FuncUndefined" autocommand sequence) might
9341 * have been aborted by an error, an interrupt, or an explicitly thrown
9342 * exception that has not been caught so far. This situation can be
9343 * tested for by calling aborting(). For an error in an internal
9344 * function or for the "E132" error in call_user_func(), however, the
9345 * throw point at which the "force_abort" flag (temporarily reset by
9346 * emsg()) is normally updated has not been reached yet. We need to
9347 * update that flag first to make aborting() reliable.
9348 */
9349 update_force_abort();
9350 }
9351 if (error == ERROR_NONE)
9352 ret = OK;
9353
9354 /*
9355 * Report an error unless the argument evaluation or function call has been
9356 * cancelled due to an aborting error, an interrupt, or an exception.
9357 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009358 if (!aborting())
9359 {
9360 switch (error)
9361 {
9362 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009363 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009364 break;
9365 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009366 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009367 break;
9368 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009369 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009370 name);
9371 break;
9372 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009373 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009374 name);
9375 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009376 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009377 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 name);
9379 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009380 }
9381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009383 while (argv_clear > 0)
9384 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009385 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009386 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
9388 return ret;
9389}
9390
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009391/*
9392 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009393 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009394 */
9395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009396emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009397{
9398 char_u *p;
9399
9400 if (*name == K_SPECIAL)
9401 p = concat_str((char_u *)"<SNR>", name + 3);
9402 else
9403 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009404 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009405 if (p != name)
9406 vim_free(p);
9407}
9408
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009409/*
9410 * Return TRUE for a non-zero Number and a non-empty String.
9411 */
9412 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009413non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009414{
9415 return ((argvars[0].v_type == VAR_NUMBER
9416 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009417 || (argvars[0].v_type == VAR_SPECIAL
9418 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009419 || (argvars[0].v_type == VAR_STRING
9420 && argvars[0].vval.v_string != NULL
9421 && *argvars[0].vval.v_string != NUL));
9422}
9423
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424/*********************************************
9425 * Implementation of the built-in functions
9426 */
9427
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009428#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009429static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009430
9431/*
9432 * Get the float value of "argvars[0]" into "f".
9433 * Returns FAIL when the argument is not a Number or Float.
9434 */
9435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009436get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009437{
9438 if (argvars[0].v_type == VAR_FLOAT)
9439 {
9440 *f = argvars[0].vval.v_float;
9441 return OK;
9442 }
9443 if (argvars[0].v_type == VAR_NUMBER)
9444 {
9445 *f = (float_T)argvars[0].vval.v_number;
9446 return OK;
9447 }
9448 EMSG(_("E808: Number or Float required"));
9449 return FAIL;
9450}
9451
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009452/*
9453 * "abs(expr)" function
9454 */
9455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009456f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009457{
9458 if (argvars[0].v_type == VAR_FLOAT)
9459 {
9460 rettv->v_type = VAR_FLOAT;
9461 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9462 }
9463 else
9464 {
9465 varnumber_T n;
9466 int error = FALSE;
9467
9468 n = get_tv_number_chk(&argvars[0], &error);
9469 if (error)
9470 rettv->vval.v_number = -1;
9471 else if (n > 0)
9472 rettv->vval.v_number = n;
9473 else
9474 rettv->vval.v_number = -n;
9475 }
9476}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009477
9478/*
9479 * "acos()" function
9480 */
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009484 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009485
9486 rettv->v_type = VAR_FLOAT;
9487 if (get_float_arg(argvars, &f) == OK)
9488 rettv->vval.v_float = acos(f);
9489 else
9490 rettv->vval.v_float = 0.0;
9491}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009495 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 */
9497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009503 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009505 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009506 && !tv_check_lock(l->lv_lock,
9507 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009508 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 }
9511 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009512 EMSG(_(e_listreq));
9513}
9514
9515/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009516 * "and(expr, expr)" function
9517 */
9518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009519f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009520{
9521 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9522 & get_tv_number_chk(&argvars[1], NULL);
9523}
9524
9525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009526 * "append(lnum, string/list)" function
9527 */
9528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009530{
9531 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009532 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009533 list_T *l = NULL;
9534 listitem_T *li = NULL;
9535 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536 long added = 0;
9537
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009538 /* When coming here from Insert mode, sync undo, so that this can be
9539 * undone separately from what was previously inserted. */
9540 if (u_sync_once == 2)
9541 {
9542 u_sync_once = 1; /* notify that u_sync() was called */
9543 u_sync(TRUE);
9544 }
9545
Bram Moolenaar0d660222005-01-07 21:51:51 +00009546 lnum = get_tv_lnum(argvars);
9547 if (lnum >= 0
9548 && lnum <= curbuf->b_ml.ml_line_count
9549 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009551 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 l = argvars[1].vval.v_list;
9554 if (l == NULL)
9555 return;
9556 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009558 for (;;)
9559 {
9560 if (l == NULL)
9561 tv = &argvars[1]; /* append a string */
9562 else if (li == NULL)
9563 break; /* end of list */
9564 else
9565 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 line = get_tv_string_chk(tv);
9567 if (line == NULL) /* type error */
9568 {
9569 rettv->vval.v_number = 1; /* Failed */
9570 break;
9571 }
9572 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009573 ++added;
9574 if (l == NULL)
9575 break;
9576 li = li->li_next;
9577 }
9578
9579 appended_lines_mark(lnum, added);
9580 if (curwin->w_cursor.lnum > lnum)
9581 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 else
9584 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
9587/*
9588 * "argc()" function
9589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009591f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
9595
9596/*
9597 * "argidx()" function
9598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009606 * "arglistid()" function
9607 */
9608 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009609f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009610{
9611 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009612
9613 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009614 wp = find_tabwin(&argvars[0], &argvars[1]);
9615 if (wp != NULL)
9616 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009617}
9618
9619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 * "argv(nr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 int idx;
9626
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009627 if (argvars[0].v_type != VAR_UNKNOWN)
9628 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009629 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009630 if (idx >= 0 && idx < ARGCOUNT)
9631 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9632 else
9633 rettv->vval.v_string = NULL;
9634 rettv->v_type = VAR_STRING;
9635 }
9636 else if (rettv_list_alloc(rettv) == OK)
9637 for (idx = 0; idx < ARGCOUNT; ++idx)
9638 list_append_string(rettv->vval.v_list,
9639 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640}
9641
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009642typedef enum
9643{
9644 ASSERT_EQUAL,
9645 ASSERT_NOTEQUAL,
9646 ASSERT_MATCH,
9647 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009648 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009649} assert_type_T;
9650
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009651static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009652static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009653static void assert_error(garray_T *gap);
9654static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009655
9656/*
9657 * Prepare "gap" for an assert error and add the sourcing position.
9658 */
9659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009660prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009661{
9662 char buf[NUMBUFLEN];
9663
9664 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009665 if (sourcing_name != NULL)
9666 {
9667 ga_concat(gap, sourcing_name);
9668 if (sourcing_lnum > 0)
9669 ga_concat(gap, (char_u *)" ");
9670 }
9671 if (sourcing_lnum > 0)
9672 {
9673 sprintf(buf, "line %ld", (long)sourcing_lnum);
9674 ga_concat(gap, (char_u *)buf);
9675 }
9676 if (sourcing_name != NULL || sourcing_lnum > 0)
9677 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678}
9679
9680/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009681 * Append "str" to "gap", escaping unprintable characters.
9682 * Changes NL to \n, CR to \r, etc.
9683 */
9684 static void
9685ga_concat_esc(garray_T *gap, char_u *str)
9686{
9687 char_u *p;
9688 char_u buf[NUMBUFLEN];
9689
Bram Moolenaarf1551962016-03-15 12:55:58 +01009690 if (str == NULL)
9691 {
9692 ga_concat(gap, (char_u *)"NULL");
9693 return;
9694 }
9695
Bram Moolenaar23689172016-02-15 22:37:37 +01009696 for (p = str; *p != NUL; ++p)
9697 switch (*p)
9698 {
9699 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9700 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9701 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9702 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9703 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9704 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9705 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9706 default:
9707 if (*p < ' ')
9708 {
9709 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9710 ga_concat(gap, buf);
9711 }
9712 else
9713 ga_append(gap, *p);
9714 break;
9715 }
9716}
9717
9718/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009719 * Fill "gap" with information about an assert error.
9720 */
9721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009722fill_assert_error(
9723 garray_T *gap,
9724 typval_T *opt_msg_tv,
9725 char_u *exp_str,
9726 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009727 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009728 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009729{
9730 char_u numbuf[NUMBUFLEN];
9731 char_u *tofree;
9732
9733 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9734 {
9735 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9736 vim_free(tofree);
9737 }
9738 else
9739 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009740 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009741 ga_concat(gap, (char_u *)"Pattern ");
9742 else
9743 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009744 if (exp_str == NULL)
9745 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009746 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009747 vim_free(tofree);
9748 }
9749 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009750 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009751 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009752 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009753 else if (atype == ASSERT_NOTMATCH)
9754 ga_concat(gap, (char_u *)" does match ");
9755 else if (atype == ASSERT_NOTEQUAL)
9756 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009757 else
9758 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009759 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009760 vim_free(tofree);
9761 }
9762}
Bram Moolenaar43345542015-11-29 17:35:35 +01009763
9764/*
9765 * Add an assert error to v:errors.
9766 */
9767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009768assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009769{
9770 struct vimvar *vp = &vimvars[VV_ERRORS];
9771
9772 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9773 /* Make sure v:errors is a list. */
9774 set_vim_var_list(VV_ERRORS, list_alloc());
9775 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9776}
9777
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009778 static void
9779assert_equal_common(typval_T *argvars, assert_type_T atype)
9780{
9781 garray_T ga;
9782
9783 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9784 != (atype == ASSERT_EQUAL))
9785 {
9786 prepare_assert_error(&ga);
9787 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9788 atype);
9789 assert_error(&ga);
9790 ga_clear(&ga);
9791 }
9792}
9793
Bram Moolenaar43345542015-11-29 17:35:35 +01009794/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009796 */
9797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009799{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009800 assert_equal_common(argvars, ASSERT_EQUAL);
9801}
Bram Moolenaar43345542015-11-29 17:35:35 +01009802
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009803/*
9804 * "assert_notequal(expected, actual[, msg])" function
9805 */
9806 static void
9807f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9808{
9809 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009810}
9811
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009812/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009813 * "assert_exception(string[, msg])" function
9814 */
9815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009816f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009817{
9818 garray_T ga;
9819 char *error;
9820
9821 error = (char *)get_tv_string_chk(&argvars[0]);
9822 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9823 {
9824 prepare_assert_error(&ga);
9825 ga_concat(&ga, (char_u *)"v:exception is not set");
9826 assert_error(&ga);
9827 ga_clear(&ga);
9828 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009829 else if (error != NULL
9830 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009831 {
9832 prepare_assert_error(&ga);
9833 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009834 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009835 assert_error(&ga);
9836 ga_clear(&ga);
9837 }
9838}
9839
9840/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009841 * "assert_fails(cmd [, error])" function
9842 */
9843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009844f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009845{
9846 char_u *cmd = get_tv_string_chk(&argvars[0]);
9847 garray_T ga;
9848
9849 called_emsg = FALSE;
9850 suppress_errthrow = TRUE;
9851 emsg_silent = TRUE;
9852 do_cmdline_cmd(cmd);
9853 if (!called_emsg)
9854 {
9855 prepare_assert_error(&ga);
9856 ga_concat(&ga, (char_u *)"command did not fail: ");
9857 ga_concat(&ga, cmd);
9858 assert_error(&ga);
9859 ga_clear(&ga);
9860 }
9861 else if (argvars[1].v_type != VAR_UNKNOWN)
9862 {
9863 char_u buf[NUMBUFLEN];
9864 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9865
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009866 if (error == NULL
9867 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009868 {
9869 prepare_assert_error(&ga);
9870 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009871 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009872 assert_error(&ga);
9873 ga_clear(&ga);
9874 }
9875 }
9876
9877 called_emsg = FALSE;
9878 suppress_errthrow = FALSE;
9879 emsg_silent = FALSE;
9880 emsg_on_display = FALSE;
9881 set_vim_var_string(VV_ERRMSG, NULL, 0);
9882}
9883
9884/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009885 * Common for assert_true() and assert_false().
9886 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009888assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009889{
9890 int error = FALSE;
9891 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009892
Bram Moolenaar37127922016-02-06 20:29:28 +01009893 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009894 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009895 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009896 if (argvars[0].v_type != VAR_NUMBER
9897 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9898 || error)
9899 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009900 prepare_assert_error(&ga);
9901 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009902 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009903 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009904 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009905 ga_clear(&ga);
9906 }
9907}
9908
9909/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009910 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009911 */
9912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009914{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009915 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009916}
9917
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009918 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009919assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009920{
9921 garray_T ga;
9922 char_u buf1[NUMBUFLEN];
9923 char_u buf2[NUMBUFLEN];
9924 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9925 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9926
Bram Moolenaar72188e92016-03-28 22:48:29 +02009927 if (pat == NULL || text == NULL)
9928 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009929 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009930 {
9931 prepare_assert_error(&ga);
9932 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009933 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009934 assert_error(&ga);
9935 ga_clear(&ga);
9936 }
9937}
9938
9939/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009940 * "assert_match(pattern, actual[, msg])" function
9941 */
9942 static void
9943f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9944{
9945 assert_match_common(argvars, ASSERT_MATCH);
9946}
9947
9948/*
9949 * "assert_notmatch(pattern, actual[, msg])" function
9950 */
9951 static void
9952f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9953{
9954 assert_match_common(argvars, ASSERT_NOTMATCH);
9955}
9956
9957/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009958 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009959 */
9960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009962{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009963 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009964}
9965
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009966#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009967/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009968 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009969 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009972{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009973 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009974
9975 rettv->v_type = VAR_FLOAT;
9976 if (get_float_arg(argvars, &f) == OK)
9977 rettv->vval.v_float = asin(f);
9978 else
9979 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009980}
9981
9982/*
9983 * "atan()" function
9984 */
9985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009986f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009987{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009988 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989
9990 rettv->v_type = VAR_FLOAT;
9991 if (get_float_arg(argvars, &f) == OK)
9992 rettv->vval.v_float = atan(f);
9993 else
9994 rettv->vval.v_float = 0.0;
9995}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009996
9997/*
9998 * "atan2()" function
9999 */
10000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010001f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010002{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010003 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010004
10005 rettv->v_type = VAR_FLOAT;
10006 if (get_float_arg(argvars, &fx) == OK
10007 && get_float_arg(&argvars[1], &fy) == OK)
10008 rettv->vval.v_float = atan2(fx, fy);
10009 else
10010 rettv->vval.v_float = 0.0;
10011}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010012#endif
10013
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014/*
10015 * "browse(save, title, initdir, default)" function
10016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
10020#ifdef FEAT_BROWSE
10021 int save;
10022 char_u *title;
10023 char_u *initdir;
10024 char_u *defname;
10025 char_u buf[NUMBUFLEN];
10026 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010029 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030 title = get_tv_string_chk(&argvars[1]);
10031 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10032 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010034 if (error || title == NULL || initdir == NULL || defname == NULL)
10035 rettv->vval.v_string = NULL;
10036 else
10037 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010038 do_browse(save ? BROWSE_SAVE : 0,
10039 title, defname, NULL, initdir, NULL, curbuf);
10040#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010044}
10045
10046/*
10047 * "browsedir(title, initdir)" function
10048 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010050f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010051{
10052#ifdef FEAT_BROWSE
10053 char_u *title;
10054 char_u *initdir;
10055 char_u buf[NUMBUFLEN];
10056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 title = get_tv_string_chk(&argvars[0]);
10058 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060 if (title == NULL || initdir == NULL)
10061 rettv->vval.v_string = NULL;
10062 else
10063 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010064 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010071static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010072
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073/*
10074 * Find a buffer by number or exact name.
10075 */
10076 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010077find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 if (avar->v_type == VAR_NUMBER)
10082 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010083 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010085 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010086 if (buf == NULL)
10087 {
10088 /* No full path name match, try a match with a URL or a "nofile"
10089 * buffer, these don't use the full path. */
10090 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10091 if (buf->b_fname != NULL
10092 && (path_with_url(buf->b_fname)
10093#ifdef FEAT_QUICKFIX
10094 || bt_nofile(buf)
10095#endif
10096 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010097 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010098 break;
10099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 }
10101 return buf;
10102}
10103
10104/*
10105 * "bufexists(expr)" function
10106 */
10107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010108f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111}
10112
10113/*
10114 * "buflisted(expr)" function
10115 */
10116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
10119 buf_T *buf;
10120
10121 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123}
10124
10125/*
10126 * "bufloaded(expr)" function
10127 */
10128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010129f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
10131 buf_T *buf;
10132
10133 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135}
10136
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010137 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010138buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 int save_magic;
10141 char_u *save_cpo;
10142 buf_T *buf;
10143
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10145 save_magic = p_magic;
10146 p_magic = TRUE;
10147 save_cpo = p_cpo;
10148 p_cpo = (char_u *)"";
10149
10150 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010151 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
10153 p_magic = save_magic;
10154 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010155 return buf;
10156}
10157
10158/*
10159 * Get buffer by number or pattern.
10160 */
10161 static buf_T *
10162get_buf_tv(typval_T *tv, int curtab_only)
10163{
10164 char_u *name = tv->vval.v_string;
10165 buf_T *buf;
10166
10167 if (tv->v_type == VAR_NUMBER)
10168 return buflist_findnr((int)tv->vval.v_number);
10169 if (tv->v_type != VAR_STRING)
10170 return NULL;
10171 if (name == NULL || *name == NUL)
10172 return curbuf;
10173 if (name[0] == '$' && name[1] == NUL)
10174 return lastbuf;
10175
10176 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
10178 /* If not found, try expanding the name, like done for bufexists(). */
10179 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
10182 return buf;
10183}
10184
10185/*
10186 * "bufname(expr)" function
10187 */
10188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 buf_T *buf;
10192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010195 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 --emsg_off;
10202}
10203
10204/*
10205 * "bufnr(expr)" function
10206 */
10207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010208f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010211 int error = FALSE;
10212 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010216 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010217 --emsg_off;
10218
10219 /* If the buffer isn't found and the second argument is not zero create a
10220 * new buffer. */
10221 if (buf == NULL
10222 && argvars[1].v_type != VAR_UNKNOWN
10223 && get_tv_number_chk(&argvars[1], &error) != 0
10224 && !error
10225 && (name = get_tv_string_chk(&argvars[0])) != NULL
10226 && !error)
10227 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10228
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233}
10234
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010236buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237{
10238#ifdef FEAT_WINDOWS
10239 win_T *wp;
10240 int winnr = 0;
10241#endif
10242 buf_T *buf;
10243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010246 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247#ifdef FEAT_WINDOWS
10248 for (wp = firstwin; wp; wp = wp->w_next)
10249 {
10250 ++winnr;
10251 if (wp->w_buffer == buf)
10252 break;
10253 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010254 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010256 rettv->vval.v_number = (curwin->w_buffer == buf
10257 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#endif
10259 --emsg_off;
10260}
10261
10262/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010263 * "bufwinid(nr)" function
10264 */
10265 static void
10266f_bufwinid(typval_T *argvars, typval_T *rettv)
10267{
10268 buf_win_common(argvars, rettv, FALSE);
10269}
10270
10271/*
10272 * "bufwinnr(nr)" function
10273 */
10274 static void
10275f_bufwinnr(typval_T *argvars, typval_T *rettv)
10276{
10277 buf_win_common(argvars, rettv, TRUE);
10278}
10279
10280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 * "byte2line(byte)" function
10282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#else
10289 long boff = 0;
10290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 (linenr_T)0, &boff);
10297#endif
10298}
10299
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010302{
10303#ifdef FEAT_MBYTE
10304 char_u *t;
10305#endif
10306 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010307 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 str = get_tv_string_chk(&argvars[0]);
10310 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010313 return;
10314
10315#ifdef FEAT_MBYTE
10316 t = str;
10317 for ( ; idx > 0; idx--)
10318 {
10319 if (*t == NUL) /* EOL reached */
10320 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010321 if (enc_utf8 && comp)
10322 t += utf_ptr2len(t);
10323 else
10324 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010325 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010326 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010327#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010328 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010330#endif
10331}
10332
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010333/*
10334 * "byteidx()" function
10335 */
10336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010337f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010338{
10339 byteidx(argvars, rettv, FALSE);
10340}
10341
10342/*
10343 * "byteidxcomp()" function
10344 */
10345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010346f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010347{
10348 byteidx(argvars, rettv, TRUE);
10349}
10350
Bram Moolenaardb913952012-06-29 12:54:53 +020010351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352func_call(
10353 char_u *name,
10354 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010355 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010356 dict_T *selfdict,
10357 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010358{
10359 listitem_T *item;
10360 typval_T argv[MAX_FUNC_ARGS + 1];
10361 int argc = 0;
10362 int dummy;
10363 int r = 0;
10364
10365 for (item = args->vval.v_list->lv_first; item != NULL;
10366 item = item->li_next)
10367 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010368 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010369 {
10370 EMSG(_("E699: Too many arguments"));
10371 break;
10372 }
10373 /* Make a copy of each argument. This is needed to be able to set
10374 * v_lock to VAR_FIXED in the copy without changing the original list.
10375 */
10376 copy_tv(&item->li_tv, &argv[argc++]);
10377 }
10378
10379 if (item == NULL)
10380 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10381 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010382 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010383
10384 /* Free the arguments. */
10385 while (argc > 0)
10386 clear_tv(&argv[--argc]);
10387
10388 return r;
10389}
10390
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010391/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010392 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010393 */
10394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010396{
10397 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010398 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010399 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010400
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010401 if (argvars[1].v_type != VAR_LIST)
10402 {
10403 EMSG(_(e_listreq));
10404 return;
10405 }
10406 if (argvars[1].vval.v_list == NULL)
10407 return;
10408
10409 if (argvars[0].v_type == VAR_FUNC)
10410 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010411 else if (argvars[0].v_type == VAR_PARTIAL)
10412 {
10413 partial = argvars[0].vval.v_partial;
10414 func = partial->pt_name;
10415 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010416 else
10417 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 if (*func == NUL)
10419 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010420
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 if (argvars[2].v_type != VAR_UNKNOWN)
10422 {
10423 if (argvars[2].v_type != VAR_DICT)
10424 {
10425 EMSG(_(e_dictreq));
10426 return;
10427 }
10428 selfdict = argvars[2].vval.v_dict;
10429 }
10430
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010431 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010432}
10433
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010434#ifdef FEAT_FLOAT
10435/*
10436 * "ceil({float})" function
10437 */
10438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010439f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010440{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010441 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010442
10443 rettv->v_type = VAR_FLOAT;
10444 if (get_float_arg(argvars, &f) == OK)
10445 rettv->vval.v_float = ceil(f);
10446 else
10447 rettv->vval.v_float = 0.0;
10448}
10449#endif
10450
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010451#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010452/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010453 * "ch_close()" function
10454 */
10455 static void
10456f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10457{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010458 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010459
10460 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010461 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010462 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010463 channel_clear(channel);
10464 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010465}
10466
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010467/*
10468 * "ch_getbufnr()" function
10469 */
10470 static void
10471f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10472{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010473 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010474
10475 rettv->vval.v_number = -1;
10476 if (channel != NULL)
10477 {
10478 char_u *what = get_tv_string(&argvars[1]);
10479 int part;
10480
10481 if (STRCMP(what, "err") == 0)
10482 part = PART_ERR;
10483 else if (STRCMP(what, "out") == 0)
10484 part = PART_OUT;
10485 else if (STRCMP(what, "in") == 0)
10486 part = PART_IN;
10487 else
10488 part = PART_SOCK;
10489 if (channel->ch_part[part].ch_buffer != NULL)
10490 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10491 }
10492}
10493
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010494/*
10495 * "ch_getjob()" function
10496 */
10497 static void
10498f_ch_getjob(typval_T *argvars, typval_T *rettv)
10499{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010500 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010501
10502 if (channel != NULL)
10503 {
10504 rettv->v_type = VAR_JOB;
10505 rettv->vval.v_job = channel->ch_job;
10506 if (channel->ch_job != NULL)
10507 ++channel->ch_job->jv_refcount;
10508 }
10509}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010510
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010511/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010512 * "ch_info()" function
10513 */
10514 static void
10515f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10516{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010517 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010518
10519 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10520 channel_info(channel, rettv->vval.v_dict);
10521}
10522
10523/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010524 * "ch_log()" function
10525 */
10526 static void
10527f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10528{
10529 char_u *msg = get_tv_string(&argvars[0]);
10530 channel_T *channel = NULL;
10531
10532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010533 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010534
10535 ch_log(channel, (char *)msg);
10536}
10537
10538/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010539 * "ch_logfile()" function
10540 */
10541 static void
10542f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10543{
10544 char_u *fname;
10545 char_u *opt = (char_u *)"";
10546 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010547
10548 fname = get_tv_string(&argvars[0]);
10549 if (argvars[1].v_type == VAR_STRING)
10550 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010551 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010552}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010553
10554/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010555 * "ch_open()" function
10556 */
10557 static void
10558f_ch_open(typval_T *argvars, typval_T *rettv)
10559{
Bram Moolenaar77073442016-02-13 23:23:53 +010010560 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010561 if (check_restricted() || check_secure())
10562 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010563 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010564}
10565
10566/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010567 * "ch_read()" function
10568 */
10569 static void
10570f_ch_read(typval_T *argvars, typval_T *rettv)
10571{
10572 common_channel_read(argvars, rettv, FALSE);
10573}
10574
10575/*
10576 * "ch_readraw()" function
10577 */
10578 static void
10579f_ch_readraw(typval_T *argvars, typval_T *rettv)
10580{
10581 common_channel_read(argvars, rettv, TRUE);
10582}
10583
10584/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010585 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010586 */
10587 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010588f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10589{
10590 ch_expr_common(argvars, rettv, TRUE);
10591}
10592
10593/*
10594 * "ch_sendexpr()" function
10595 */
10596 static void
10597f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10598{
10599 ch_expr_common(argvars, rettv, FALSE);
10600}
10601
10602/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010603 * "ch_evalraw()" function
10604 */
10605 static void
10606f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_raw_common(argvars, rettv, TRUE);
10609}
10610
10611/*
10612 * "ch_sendraw()" function
10613 */
10614 static void
10615f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_raw_common(argvars, rettv, FALSE);
10618}
10619
10620/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010621 * "ch_setoptions()" function
10622 */
10623 static void
10624f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10625{
10626 channel_T *channel;
10627 jobopt_T opt;
10628
Bram Moolenaar437905c2016-04-26 19:01:05 +020010629 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010630 if (channel == NULL)
10631 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010632 clear_job_options(&opt);
10633 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010634 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10635 channel_set_options(channel, &opt);
10636 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010637}
10638
10639/*
10640 * "ch_status()" function
10641 */
10642 static void
10643f_ch_status(typval_T *argvars, typval_T *rettv)
10644{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010645 channel_T *channel;
10646
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010647 /* return an empty string by default */
10648 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010649 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010650
Bram Moolenaar437905c2016-04-26 19:01:05 +020010651 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010652 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010653}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010654#endif
10655
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010656/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010657 * "changenr()" function
10658 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010660f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010661{
10662 rettv->vval.v_number = curbuf->b_u_seq_cur;
10663}
10664
10665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 * "char2nr(string)" function
10667 */
10668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010669f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670{
10671#ifdef FEAT_MBYTE
10672 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010673 {
10674 int utf8 = 0;
10675
10676 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010677 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010678
10679 if (utf8)
10680 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10681 else
10682 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 else
10685#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687}
10688
10689/*
10690 * "cindent(lnum)" function
10691 */
10692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010693f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694{
10695#ifdef FEAT_CINDENT
10696 pos_T pos;
10697 linenr_T lnum;
10698
10699 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10702 {
10703 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 curwin->w_cursor = pos;
10706 }
10707 else
10708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710}
10711
10712/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010713 * "clearmatches()" function
10714 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010716f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010717{
10718#ifdef FEAT_SEARCH_EXTRA
10719 clear_matches(curwin);
10720#endif
10721}
10722
10723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 * "col(string)" function
10725 */
10726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010727f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 colnr_T col = 0;
10730 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010731 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010733 fp = var2fpos(&argvars[0], FALSE, &fnum);
10734 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 {
10736 if (fp->col == MAXCOL)
10737 {
10738 /* '> can be MAXCOL, get the length of the line then */
10739 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010740 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 else
10742 col = MAXCOL;
10743 }
10744 else
10745 {
10746 col = fp->col + 1;
10747#ifdef FEAT_VIRTUALEDIT
10748 /* col(".") when the cursor is on the NUL at the end of the line
10749 * because of "coladd" can be seen as an extra column. */
10750 if (virtual_active() && fp == &curwin->w_cursor)
10751 {
10752 char_u *p = ml_get_cursor();
10753
10754 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10755 curwin->w_virtcol - curwin->w_cursor.coladd))
10756 {
10757# ifdef FEAT_MBYTE
10758 int l;
10759
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010760 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 col += l;
10762# else
10763 if (*p != NUL && p[1] == NUL)
10764 ++col;
10765# endif
10766 }
10767 }
10768#endif
10769 }
10770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010771 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772}
10773
Bram Moolenaar572cb562005-08-05 21:35:02 +000010774#if defined(FEAT_INS_EXPAND)
10775/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010776 * "complete()" function
10777 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010779f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010780{
10781 int startcol;
10782
10783 if ((State & INSERT) == 0)
10784 {
10785 EMSG(_("E785: complete() can only be used in Insert mode"));
10786 return;
10787 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010788
10789 /* Check for undo allowed here, because if something was already inserted
10790 * the line was already saved for undo and this check isn't done. */
10791 if (!undo_allowed())
10792 return;
10793
Bram Moolenaarade00832006-03-10 21:46:58 +000010794 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10795 {
10796 EMSG(_(e_invarg));
10797 return;
10798 }
10799
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010800 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010801 if (startcol <= 0)
10802 return;
10803
10804 set_completion(startcol - 1, argvars[1].vval.v_list);
10805}
10806
10807/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010808 * "complete_add()" function
10809 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010811f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010812{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010813 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010814}
10815
10816/*
10817 * "complete_check()" function
10818 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010821{
10822 int saved = RedrawingDisabled;
10823
10824 RedrawingDisabled = 0;
10825 ins_compl_check_keys(0);
10826 rettv->vval.v_number = compl_interrupted;
10827 RedrawingDisabled = saved;
10828}
10829#endif
10830
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831/*
10832 * "confirm(message, buttons[, default [, type]])" function
10833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010835f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10838 char_u *message;
10839 char_u *buttons = NULL;
10840 char_u buf[NUMBUFLEN];
10841 char_u buf2[NUMBUFLEN];
10842 int def = 1;
10843 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 char_u *typestr;
10845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 message = get_tv_string_chk(&argvars[0]);
10848 if (message == NULL)
10849 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010850 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10853 if (buttons == NULL)
10854 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010857 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010858 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10861 if (typestr == NULL)
10862 error = TRUE;
10863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 switch (TOUPPER_ASC(*typestr))
10866 {
10867 case 'E': type = VIM_ERROR; break;
10868 case 'Q': type = VIM_QUESTION; break;
10869 case 'I': type = VIM_INFO; break;
10870 case 'W': type = VIM_WARNING; break;
10871 case 'G': type = VIM_GENERIC; break;
10872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874 }
10875 }
10876 }
10877
10878 if (buttons == NULL || *buttons == NUL)
10879 buttons = (char_u *)_("&Ok");
10880
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010881 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010883 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884#endif
10885}
10886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010887/*
10888 * "copy()" function
10889 */
10890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010891f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010892{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010893 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010894}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010896#ifdef FEAT_FLOAT
10897/*
10898 * "cos()" function
10899 */
10900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010901f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010902{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010903 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010904
10905 rettv->v_type = VAR_FLOAT;
10906 if (get_float_arg(argvars, &f) == OK)
10907 rettv->vval.v_float = cos(f);
10908 else
10909 rettv->vval.v_float = 0.0;
10910}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010911
10912/*
10913 * "cosh()" function
10914 */
10915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010917{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010918 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010919
10920 rettv->v_type = VAR_FLOAT;
10921 if (get_float_arg(argvars, &f) == OK)
10922 rettv->vval.v_float = cosh(f);
10923 else
10924 rettv->vval.v_float = 0.0;
10925}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010926#endif
10927
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010929 * "count()" function
10930 */
10931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010932f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010933{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934 long n = 0;
10935 int ic = FALSE;
10936
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010939 listitem_T *li;
10940 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010942
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 if ((l = argvars[0].vval.v_list) != NULL)
10944 {
10945 li = l->lv_first;
10946 if (argvars[2].v_type != VAR_UNKNOWN)
10947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010948 int error = FALSE;
10949
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010950 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 if (argvars[3].v_type != VAR_UNKNOWN)
10952 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010953 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 if (!error)
10955 {
10956 li = list_find(l, idx);
10957 if (li == NULL)
10958 EMSGN(_(e_listidx), idx);
10959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 if (error)
10962 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 }
10964
10965 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010966 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 ++n;
10968 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 else if (argvars[0].v_type == VAR_DICT)
10971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 int todo;
10973 dict_T *d;
10974 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010976 if ((d = argvars[0].vval.v_dict) != NULL)
10977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 int error = FALSE;
10979
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 if (argvars[2].v_type != VAR_UNKNOWN)
10981 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010982 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010983 if (argvars[3].v_type != VAR_UNKNOWN)
10984 EMSG(_(e_invarg));
10985 }
10986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010987 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010989 {
10990 if (!HASHITEM_EMPTY(hi))
10991 {
10992 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010993 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010994 ++n;
10995 }
10996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 }
10998 }
10999 else
11000 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011001 rettv->vval.v_number = n;
11002}
11003
11004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11006 *
11007 * Checks the existence of a cscope connection.
11008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011010f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011{
11012#ifdef FEAT_CSCOPE
11013 int num = 0;
11014 char_u *dbpath = NULL;
11015 char_u *prepend = NULL;
11016 char_u buf[NUMBUFLEN];
11017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011018 if (argvars[0].v_type != VAR_UNKNOWN
11019 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 num = (int)get_tv_number(&argvars[0]);
11022 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028#endif
11029}
11030
11031/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011032 * "cursor(lnum, col)" function, or
11033 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011035 * Moves the cursor to the specified line and column.
11036 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011039f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
11041 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011042#ifdef FEAT_VIRTUALEDIT
11043 long coladd = 0;
11044#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011045 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011047 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011048 if (argvars[1].v_type == VAR_UNKNOWN)
11049 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011050 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011051 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011052
Bram Moolenaar493c1782014-05-28 14:34:46 +020011053 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011054 {
11055 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011056 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011057 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011058 line = pos.lnum;
11059 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011060#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011061 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011062#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011063 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011064 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011065 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011066 set_curswant = FALSE;
11067 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011068 }
11069 else
11070 {
11071 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011072 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011073#ifdef FEAT_VIRTUALEDIT
11074 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011075 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011076#endif
11077 }
11078 if (line < 0 || col < 0
11079#ifdef FEAT_VIRTUALEDIT
11080 || coladd < 0
11081#endif
11082 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 if (line > 0)
11085 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 if (col > 0)
11087 curwin->w_cursor.col = col - 1;
11088#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011089 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090#endif
11091
11092 /* Make sure the cursor is in a valid position. */
11093 check_cursor();
11094#ifdef FEAT_MBYTE
11095 /* Correct cursor for multi-byte character. */
11096 if (has_mbyte)
11097 mb_adjust_cursor();
11098#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011099
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011100 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011101 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102}
11103
11104/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011105 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 */
11107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011108f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011110 int noref = 0;
11111
11112 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011113 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011114 if (noref < 0 || noref > 1)
11115 EMSG(_(e_invarg));
11116 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011117 {
11118 current_copyID += COPYID_INC;
11119 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121}
11122
11123/*
11124 * "delete()" function
11125 */
11126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011127f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011129 char_u nbuf[NUMBUFLEN];
11130 char_u *name;
11131 char_u *flags;
11132
11133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011135 return;
11136
11137 name = get_tv_string(&argvars[0]);
11138 if (name == NULL || *name == NUL)
11139 {
11140 EMSG(_(e_invarg));
11141 return;
11142 }
11143
11144 if (argvars[1].v_type != VAR_UNKNOWN)
11145 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011147 flags = (char_u *)"";
11148
11149 if (*flags == NUL)
11150 /* delete a file */
11151 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11152 else if (STRCMP(flags, "d") == 0)
11153 /* delete an empty directory */
11154 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11155 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011156 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011157 rettv->vval.v_number = delete_recursive(name);
11158 else
11159 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161
11162/*
11163 * "did_filetype()" function
11164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011166f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170#endif
11171}
11172
11173/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011174 * "diff_filler()" function
11175 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011177f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011178{
11179#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011181#endif
11182}
11183
11184/*
11185 * "diff_hlID()" function
11186 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011188f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011189{
11190#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011191 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011192 static linenr_T prev_lnum = 0;
11193 static int changedtick = 0;
11194 static int fnum = 0;
11195 static int change_start = 0;
11196 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011197 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198 int filler_lines;
11199 int col;
11200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 if (lnum < 0) /* ignore type error in {lnum} arg */
11202 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011203 if (lnum != prev_lnum
11204 || changedtick != curbuf->b_changedtick
11205 || fnum != curbuf->b_fnum)
11206 {
11207 /* New line, buffer, change: need to get the values. */
11208 filler_lines = diff_check(curwin, lnum);
11209 if (filler_lines < 0)
11210 {
11211 if (filler_lines == -1)
11212 {
11213 change_start = MAXCOL;
11214 change_end = -1;
11215 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11216 hlID = HLF_ADD; /* added line */
11217 else
11218 hlID = HLF_CHD; /* changed line */
11219 }
11220 else
11221 hlID = HLF_ADD; /* added line */
11222 }
11223 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011224 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011225 prev_lnum = lnum;
11226 changedtick = curbuf->b_changedtick;
11227 fnum = curbuf->b_fnum;
11228 }
11229
11230 if (hlID == HLF_CHD || hlID == HLF_TXD)
11231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011233 if (col >= change_start && col <= change_end)
11234 hlID = HLF_TXD; /* changed text */
11235 else
11236 hlID = HLF_CHD; /* changed line */
11237 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011238 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011239#endif
11240}
11241
11242/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011243 * "empty({expr})" function
11244 */
11245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011246f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011247{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011248 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011249
11250 switch (argvars[0].v_type)
11251 {
11252 case VAR_STRING:
11253 case VAR_FUNC:
11254 n = argvars[0].vval.v_string == NULL
11255 || *argvars[0].vval.v_string == NUL;
11256 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011257 case VAR_PARTIAL:
11258 n = FALSE;
11259 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011260 case VAR_NUMBER:
11261 n = argvars[0].vval.v_number == 0;
11262 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011263 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011264#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011265 n = argvars[0].vval.v_float == 0.0;
11266 break;
11267#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011268 case VAR_LIST:
11269 n = argvars[0].vval.v_list == NULL
11270 || argvars[0].vval.v_list->lv_first == NULL;
11271 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 case VAR_DICT:
11273 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011274 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011275 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011276 case VAR_SPECIAL:
11277 n = argvars[0].vval.v_number != VVAL_TRUE;
11278 break;
11279
Bram Moolenaar835dc632016-02-07 14:27:38 +010011280 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011281#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011282 n = argvars[0].vval.v_job == NULL
11283 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11284 break;
11285#endif
11286 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011287#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011288 n = argvars[0].vval.v_channel == NULL
11289 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011290 break;
11291#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011292 case VAR_UNKNOWN:
11293 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11294 n = TRUE;
11295 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011296 }
11297
11298 rettv->vval.v_number = n;
11299}
11300
11301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 * "escape({string}, {chars})" function
11303 */
11304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011305f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306{
11307 char_u buf[NUMBUFLEN];
11308
Bram Moolenaar758711c2005-02-02 23:11:38 +000011309 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11310 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312}
11313
11314/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011315 * "eval()" function
11316 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011318f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011319{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011320 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011322 s = get_tv_string_chk(&argvars[0]);
11323 if (s != NULL)
11324 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325
Bram Moolenaar615b9972015-01-14 17:15:05 +010011326 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11328 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011329 if (p != NULL && !aborting())
11330 EMSG2(_(e_invexpr2), p);
11331 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011333 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011335 else if (*s != NUL)
11336 EMSG(_(e_trailing));
11337}
11338
11339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 * "eventhandler()" function
11341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011343f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346}
11347
11348/*
11349 * "executable()" function
11350 */
11351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011352f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011354 char_u *name = get_tv_string(&argvars[0]);
11355
11356 /* Check in $PATH and also check directly if there is a directory name. */
11357 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11358 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011359}
11360
11361/*
11362 * "exepath()" function
11363 */
11364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011365f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011366{
11367 char_u *p = NULL;
11368
Bram Moolenaarb5971142015-03-21 17:32:19 +010011369 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011370 rettv->v_type = VAR_STRING;
11371 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372}
11373
11374/*
11375 * "exists()" function
11376 */
11377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011378f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379{
11380 char_u *p;
11381 char_u *name;
11382 int n = FALSE;
11383 int len = 0;
11384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 if (*p == '$') /* environment variable */
11387 {
11388 /* first try "normal" environment variables (fast) */
11389 if (mch_getenv(p + 1) != NULL)
11390 n = TRUE;
11391 else
11392 {
11393 /* try expanding things like $VIM and ${HOME} */
11394 p = expand_env_save(p);
11395 if (p != NULL && *p != '$')
11396 n = TRUE;
11397 vim_free(p);
11398 }
11399 }
11400 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011403 if (*skipwhite(p) != NUL)
11404 n = FALSE; /* trailing garbage */
11405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 else if (*p == '*') /* internal or user defined function */
11407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011408 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 }
11410 else if (*p == ':')
11411 {
11412 n = cmd_exists(p + 1);
11413 }
11414 else if (*p == '#')
11415 {
11416#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011417 if (p[1] == '#')
11418 n = autocmd_supported(p + 2);
11419 else
11420 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421#endif
11422 }
11423 else /* internal variable */
11424 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011425 char_u *tofree;
11426 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011428 /* get_name_len() takes care of expanding curly braces */
11429 name = p;
11430 len = get_name_len(&p, &tofree, TRUE, FALSE);
11431 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011433 if (tofree != NULL)
11434 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011435 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011436 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011438 /* handle d.key, l[idx], f(expr) */
11439 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11440 if (n)
11441 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 }
11443 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011444 if (*p != NUL)
11445 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011447 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 }
11449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451}
11452
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011453#ifdef FEAT_FLOAT
11454/*
11455 * "exp()" function
11456 */
11457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011458f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011459{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011460 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011461
11462 rettv->v_type = VAR_FLOAT;
11463 if (get_float_arg(argvars, &f) == OK)
11464 rettv->vval.v_float = exp(f);
11465 else
11466 rettv->vval.v_float = 0.0;
11467}
11468#endif
11469
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470/*
11471 * "expand()" function
11472 */
11473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011474f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475{
11476 char_u *s;
11477 int len;
11478 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011479 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011482 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011485 if (argvars[1].v_type != VAR_UNKNOWN
11486 && argvars[2].v_type != VAR_UNKNOWN
11487 && get_tv_number_chk(&argvars[2], &error)
11488 && !error)
11489 {
11490 rettv->v_type = VAR_LIST;
11491 rettv->vval.v_list = NULL;
11492 }
11493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 if (*s == '%' || *s == '#' || *s == '<')
11496 {
11497 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011498 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011500 if (rettv->v_type == VAR_LIST)
11501 {
11502 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11503 list_append_string(rettv->vval.v_list, result, -1);
11504 else
11505 vim_free(result);
11506 }
11507 else
11508 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 }
11510 else
11511 {
11512 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011513 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 if (argvars[1].v_type != VAR_UNKNOWN
11515 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011516 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 if (!error)
11518 {
11519 ExpandInit(&xpc);
11520 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011521 if (p_wic)
11522 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011523 if (rettv->v_type == VAR_STRING)
11524 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11525 options, WILD_ALL);
11526 else if (rettv_list_alloc(rettv) != FAIL)
11527 {
11528 int i;
11529
11530 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11531 for (i = 0; i < xpc.xp_numfiles; i++)
11532 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11533 ExpandCleanup(&xpc);
11534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 }
11536 else
11537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 }
11539}
11540
11541/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011542 * Go over all entries in "d2" and add them to "d1".
11543 * When "action" is "error" then a duplicate key is an error.
11544 * When "action" is "force" then a duplicate key is overwritten.
11545 * Otherwise duplicate keys are ignored ("action" is "keep").
11546 */
11547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011548dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011549{
11550 dictitem_T *di1;
11551 hashitem_T *hi2;
11552 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011553 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011554
11555 todo = (int)d2->dv_hashtab.ht_used;
11556 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11557 {
11558 if (!HASHITEM_EMPTY(hi2))
11559 {
11560 --todo;
11561 di1 = dict_find(d1, hi2->hi_key, -1);
11562 if (d1->dv_scope != 0)
11563 {
11564 /* Disallow replacing a builtin function in l: and g:.
11565 * Check the key to be valid when adding to any
11566 * scope. */
11567 if (d1->dv_scope == VAR_DEF_SCOPE
11568 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11569 && var_check_func_name(hi2->hi_key,
11570 di1 == NULL))
11571 break;
11572 if (!valid_varname(hi2->hi_key))
11573 break;
11574 }
11575 if (di1 == NULL)
11576 {
11577 di1 = dictitem_copy(HI2DI(hi2));
11578 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11579 dictitem_free(di1);
11580 }
11581 else if (*action == 'e')
11582 {
11583 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11584 break;
11585 }
11586 else if (*action == 'f' && HI2DI(hi2) != di1)
11587 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011588 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11589 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011590 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011591 clear_tv(&di1->di_tv);
11592 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11593 }
11594 }
11595 }
11596}
11597
11598/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011599 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011601 */
11602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011604{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011605 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011606
Bram Moolenaare9a41262005-01-15 22:18:47 +000011607 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011608 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011609 list_T *l1, *l2;
11610 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011611 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011613
Bram Moolenaare9a41262005-01-15 22:18:47 +000011614 l1 = argvars[0].vval.v_list;
11615 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011616 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011617 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011618 {
11619 if (argvars[2].v_type != VAR_UNKNOWN)
11620 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011621 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 if (error)
11623 return; /* type error; errmsg already given */
11624
Bram Moolenaar758711c2005-02-02 23:11:38 +000011625 if (before == l1->lv_len)
11626 item = NULL;
11627 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011629 item = list_find(l1, before);
11630 if (item == NULL)
11631 {
11632 EMSGN(_(e_listidx), before);
11633 return;
11634 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011635 }
11636 }
11637 else
11638 item = NULL;
11639 list_extend(l1, l2, item);
11640
Bram Moolenaare9a41262005-01-15 22:18:47 +000011641 copy_tv(&argvars[0], rettv);
11642 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011644 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11645 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011646 dict_T *d1, *d2;
11647 char_u *action;
11648 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011649
11650 d1 = argvars[0].vval.v_dict;
11651 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011652 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011653 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011654 {
11655 /* Check the third argument. */
11656 if (argvars[2].v_type != VAR_UNKNOWN)
11657 {
11658 static char *(av[]) = {"keep", "force", "error"};
11659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 action = get_tv_string_chk(&argvars[2]);
11661 if (action == NULL)
11662 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663 for (i = 0; i < 3; ++i)
11664 if (STRCMP(action, av[i]) == 0)
11665 break;
11666 if (i == 3)
11667 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011668 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 return;
11670 }
11671 }
11672 else
11673 action = (char_u *)"force";
11674
Bram Moolenaara9922d62013-05-30 13:01:18 +020011675 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011676
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 copy_tv(&argvars[0], rettv);
11678 }
11679 }
11680 else
11681 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011682}
11683
11684/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011685 * "feedkeys()" function
11686 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011688f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011689{
11690 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011691 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011692 char_u *keys, *flags;
11693 char_u nbuf[NUMBUFLEN];
11694 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011695 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011696 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011697 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011698
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011699 /* This is not allowed in the sandbox. If the commands would still be
11700 * executed in the sandbox it would be OK, but it probably happens later,
11701 * when "sandbox" is no longer set. */
11702 if (check_secure())
11703 return;
11704
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011705 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011706
11707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011708 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011709 flags = get_tv_string_buf(&argvars[1], nbuf);
11710 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011711 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011712 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011713 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011714 case 'n': remap = FALSE; break;
11715 case 'm': remap = TRUE; break;
11716 case 't': typed = TRUE; break;
11717 case 'i': insert = TRUE; break;
11718 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011719 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011720 }
11721 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011722 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011723
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011724 if (*keys != NUL || execute)
11725 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011726 /* Need to escape K_SPECIAL and CSI before putting the string in the
11727 * typeahead buffer. */
11728 keys_esc = vim_strsave_escape_csi(keys);
11729 if (keys_esc != NULL)
11730 {
11731 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011732 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011733 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011734 if (vgetc_busy)
11735 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011736 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011737 {
11738 int save_msg_scroll = msg_scroll;
11739
11740 /* Avoid a 1 second delay when the keys start Insert mode. */
11741 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011742
Bram Moolenaar245c4102016-04-20 17:37:41 +020011743 if (!dangerous)
11744 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011745 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011746 if (!dangerous)
11747 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011748 msg_scroll |= save_msg_scroll;
11749 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011750 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011751 }
11752}
11753
11754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 * "filereadable()" function
11756 */
11757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011760 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761 char_u *p;
11762 int n;
11763
Bram Moolenaarc236c162008-07-13 17:41:49 +000011764#ifndef O_NONBLOCK
11765# define O_NONBLOCK 0
11766#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011768 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11769 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 {
11771 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011772 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 }
11774 else
11775 n = FALSE;
11776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778}
11779
11780/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011781 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 * rights to write into.
11783 */
11784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011785f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011787 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788}
11789
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011790 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011791findfilendir(
11792 typval_T *argvars UNUSED,
11793 typval_T *rettv,
11794 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011795{
11796#ifdef FEAT_SEARCHPATH
11797 char_u *fname;
11798 char_u *fresult = NULL;
11799 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11800 char_u *p;
11801 char_u pathbuf[NUMBUFLEN];
11802 int count = 1;
11803 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011804 int error = FALSE;
11805#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011806
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011807 rettv->vval.v_string = NULL;
11808 rettv->v_type = VAR_STRING;
11809
11810#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011812
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11816 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011817 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 else
11819 {
11820 if (*p != NUL)
11821 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011824 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011826 }
11827
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011828 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11829 error = TRUE;
11830
11831 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011833 do
11834 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011835 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011836 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837 fresult = find_file_in_path_option(first ? fname : NULL,
11838 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011839 0, first, path,
11840 find_what,
11841 curbuf->b_ffname,
11842 find_what == FINDFILE_DIR
11843 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011845
11846 if (fresult != NULL && rettv->v_type == VAR_LIST)
11847 list_append_string(rettv->vval.v_list, fresult, -1);
11848
11849 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011851
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011852 if (rettv->v_type == VAR_STRING)
11853 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011854#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011855}
11856
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011857static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011858static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011859
11860/*
11861 * Implementation of map() and filter().
11862 */
11863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011864filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011865{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011866 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 listitem_T *li, *nli;
11868 list_T *l = NULL;
11869 dictitem_T *di;
11870 hashtab_T *ht;
11871 hashitem_T *hi;
11872 dict_T *d = NULL;
11873 typval_T save_val;
11874 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011876 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011877 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011878 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011879 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011880 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011881 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011882
Bram Moolenaare9a41262005-01-15 22:18:47 +000011883 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011884 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011885 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011886 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011887 return;
11888 }
11889 else if (argvars[0].v_type == VAR_DICT)
11890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011891 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011892 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011893 return;
11894 }
11895 else
11896 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011897 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011898 return;
11899 }
11900
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011901 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 /* On type errors, the preceding call has already displayed an error
11903 * message. Avoid a misleading error message for an empty string that
11904 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011905 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011908
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011909 /* We reset "did_emsg" to be able to detect whether an error
11910 * occurred during evaluation of the expression. */
11911 save_did_emsg = did_emsg;
11912 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011913
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011914 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011915 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 vimvars[VV_KEY].vv_type = VAR_STRING;
11918
11919 ht = &d->dv_hashtab;
11920 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011921 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 if (!HASHITEM_EMPTY(hi))
11925 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011926 int r;
11927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 --todo;
11929 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011930 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011931 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11932 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011933 break;
11934 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011935 r = filter_map_one(&di->di_tv, expr, map, &rem);
11936 clear_tv(&vimvars[VV_KEY].vv_tv);
11937 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 break;
11939 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011940 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011941 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11942 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011943 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011945 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 }
11947 }
11948 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 }
11950 else
11951 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011952 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 for (li = l->lv_first; li != NULL; li = nli)
11955 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011956 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011957 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011959 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011960 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011961 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011962 break;
11963 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011964 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011965 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011966 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011967 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011968
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011969 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011971
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011972 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974
11975 copy_tv(&argvars[0], rettv);
11976}
11977
11978 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011979filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011980{
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011982 typval_T argv[3];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011983 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011984 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011985 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011986
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011988 argv[0] = vimvars[VV_KEY].vv_tv;
11989 argv[1] = vimvars[VV_VAL].vv_tv;
11990 s = expr->vval.v_string;
11991 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011992 {
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011993 if (call_func(s, (int)STRLEN(s),
11994 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
11995 goto theend;
11996 }
11997 else if (expr->v_type == VAR_PARTIAL)
11998 {
11999 partial_T *partial = expr->vval.v_partial;
12000
12001 s = partial->pt_name;
12002 if (call_func(s, (int)STRLEN(s),
12003 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12004 == FAIL)
12005 goto theend;
12006 }
12007 else
12008 {
12009 s = skipwhite(s);
12010 if (eval1(&s, &rettv, TRUE) == FAIL)
12011 goto theend;
12012 if (*s != NUL) /* check for trailing chars after expr */
12013 {
12014 EMSG2(_(e_invexpr2), s);
12015 goto theend;
12016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017 }
12018 if (map)
12019 {
12020 /* map(): replace the list item value */
12021 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012022 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012023 *tv = rettv;
12024 }
12025 else
12026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 int error = FALSE;
12028
Bram Moolenaare9a41262005-01-15 22:18:47 +000012029 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012032 /* On type error, nothing has been removed; return FAIL to stop the
12033 * loop. The error message was given by get_tv_number_chk(). */
12034 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012035 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012037 retval = OK;
12038theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012039 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012040 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041}
12042
12043/*
12044 * "filter()" function
12045 */
12046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012047f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012048{
12049 filter_map(argvars, rettv, FALSE);
12050}
12051
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012052/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012053 * "finddir({fname}[, {path}[, {count}]])" function
12054 */
12055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012056f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012058 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059}
12060
12061/*
12062 * "findfile({fname}[, {path}[, {count}]])" function
12063 */
12064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012065f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012067 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068}
12069
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012070#ifdef FEAT_FLOAT
12071/*
12072 * "float2nr({float})" function
12073 */
12074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012075f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012076{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012077 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012078
12079 if (get_float_arg(argvars, &f) == OK)
12080 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012081# ifdef FEAT_NUM64
12082 if (f < -0x7fffffffffffffff)
12083 rettv->vval.v_number = -0x7fffffffffffffff;
12084 else if (f > 0x7fffffffffffffff)
12085 rettv->vval.v_number = 0x7fffffffffffffff;
12086 else
12087 rettv->vval.v_number = (varnumber_T)f;
12088# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012089 if (f < -0x7fffffff)
12090 rettv->vval.v_number = -0x7fffffff;
12091 else if (f > 0x7fffffff)
12092 rettv->vval.v_number = 0x7fffffff;
12093 else
12094 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012095# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012096 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012097}
12098
12099/*
12100 * "floor({float})" function
12101 */
12102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012104{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012105 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012106
12107 rettv->v_type = VAR_FLOAT;
12108 if (get_float_arg(argvars, &f) == OK)
12109 rettv->vval.v_float = floor(f);
12110 else
12111 rettv->vval.v_float = 0.0;
12112}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012113
12114/*
12115 * "fmod()" function
12116 */
12117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012118f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012119{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012120 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012121
12122 rettv->v_type = VAR_FLOAT;
12123 if (get_float_arg(argvars, &fx) == OK
12124 && get_float_arg(&argvars[1], &fy) == OK)
12125 rettv->vval.v_float = fmod(fx, fy);
12126 else
12127 rettv->vval.v_float = 0.0;
12128}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012129#endif
12130
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012132 * "fnameescape({string})" function
12133 */
12134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012135f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012136{
12137 rettv->vval.v_string = vim_strsave_fnameescape(
12138 get_tv_string(&argvars[0]), FALSE);
12139 rettv->v_type = VAR_STRING;
12140}
12141
12142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 * "fnamemodify({fname}, {mods})" function
12144 */
12145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012146f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147{
12148 char_u *fname;
12149 char_u *mods;
12150 int usedlen = 0;
12151 int len;
12152 char_u *fbuf = NULL;
12153 char_u buf[NUMBUFLEN];
12154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 fname = get_tv_string_chk(&argvars[0]);
12156 mods = get_tv_string_buf_chk(&argvars[1], buf);
12157 if (fname == NULL || mods == NULL)
12158 fname = NULL;
12159 else
12160 {
12161 len = (int)STRLEN(fname);
12162 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 vim_free(fbuf);
12171}
12172
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012173static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174
12175/*
12176 * "foldclosed()" function
12177 */
12178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179foldclosed_both(
12180 typval_T *argvars UNUSED,
12181 typval_T *rettv,
12182 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183{
12184#ifdef FEAT_FOLDING
12185 linenr_T lnum;
12186 linenr_T first, last;
12187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12190 {
12191 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12192 {
12193 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 return;
12198 }
12199 }
12200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202}
12203
12204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205 * "foldclosed()" function
12206 */
12207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012208f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012209{
12210 foldclosed_both(argvars, rettv, FALSE);
12211}
12212
12213/*
12214 * "foldclosedend()" function
12215 */
12216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012217f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012218{
12219 foldclosed_both(argvars, rettv, TRUE);
12220}
12221
12222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 * "foldlevel()" function
12224 */
12225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012226f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227{
12228#ifdef FEAT_FOLDING
12229 linenr_T lnum;
12230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
12238 * "foldtext()" function
12239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012241f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242{
12243#ifdef FEAT_FOLDING
12244 linenr_T lnum;
12245 char_u *s;
12246 char_u *r;
12247 int len;
12248 char *txt;
12249#endif
12250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251 rettv->v_type = VAR_STRING;
12252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012254 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12255 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12256 <= curbuf->b_ml.ml_line_count
12257 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 {
12259 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012260 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12261 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 {
12263 if (!linewhite(lnum))
12264 break;
12265 ++lnum;
12266 }
12267
12268 /* Find interesting text in this line. */
12269 s = skipwhite(ml_get(lnum));
12270 /* skip C comment-start */
12271 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012274 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012275 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012276 {
12277 s = skipwhite(ml_get(lnum + 1));
12278 if (*s == '*')
12279 s = skipwhite(s + 1);
12280 }
12281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 txt = _("+-%s%3ld lines: ");
12283 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012284 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 + 20 /* for %3ld */
12286 + STRLEN(s))); /* concatenated */
12287 if (r != NULL)
12288 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012289 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12290 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12291 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 len = (int)STRLEN(r);
12293 STRCAT(r, s);
12294 /* remove 'foldmarker' and 'commentstring' */
12295 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 }
12298 }
12299#endif
12300}
12301
12302/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012303 * "foldtextresult(lnum)" function
12304 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012306f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012307{
12308#ifdef FEAT_FOLDING
12309 linenr_T lnum;
12310 char_u *text;
12311 char_u buf[51];
12312 foldinfo_T foldinfo;
12313 int fold_count;
12314#endif
12315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 rettv->v_type = VAR_STRING;
12317 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012318#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 /* treat illegal types and illegal string values for {lnum} the same */
12321 if (lnum < 0)
12322 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012323 fold_count = foldedCount(curwin, lnum, &foldinfo);
12324 if (fold_count > 0)
12325 {
12326 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12327 &foldinfo, buf);
12328 if (text == buf)
12329 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012331 }
12332#endif
12333}
12334
12335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 * "foreground()" function
12337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012339f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341#ifdef FEAT_GUI
12342 if (gui.in_use)
12343 gui_mch_set_foreground();
12344#else
12345# ifdef WIN32
12346 win32_set_foreground();
12347# endif
12348#endif
12349}
12350
12351/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012352 * "function()" function
12353 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012355f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012356{
12357 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012358 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012359 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012360 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012361
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012362 if (argvars[0].v_type == VAR_FUNC)
12363 {
12364 /* function(MyFunc, [arg], dict) */
12365 s = argvars[0].vval.v_string;
12366 }
12367 else if (argvars[0].v_type == VAR_PARTIAL
12368 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012369 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012370 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012371 arg_pt = argvars[0].vval.v_partial;
12372 s = arg_pt->pt_name;
12373 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012374 else
12375 {
12376 /* function('MyFunc', [arg], dict) */
12377 s = get_tv_string(&argvars[0]);
12378 use_string = TRUE;
12379 }
12380
12381 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012382 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012383 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012384 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12385 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012386 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 else
12388 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012389 int dict_idx = 0;
12390 int arg_idx = 0;
12391 list_T *list = NULL;
12392
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012393 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012394 {
12395 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012396 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012397
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012398 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12399 * also be called from another script. Using trans_function_name()
12400 * would also work, but some plugins depend on the name being
12401 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012402 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012403 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12404 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012405 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012406 STRCPY(name, sid_buf);
12407 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012408 }
12409 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012410 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012411 name = vim_strsave(s);
12412
12413 if (argvars[1].v_type != VAR_UNKNOWN)
12414 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012415 if (argvars[2].v_type != VAR_UNKNOWN)
12416 {
12417 /* function(name, [args], dict) */
12418 arg_idx = 1;
12419 dict_idx = 2;
12420 }
12421 else if (argvars[1].v_type == VAR_DICT)
12422 /* function(name, dict) */
12423 dict_idx = 1;
12424 else
12425 /* function(name, [args]) */
12426 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012427 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012428 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012429 if (argvars[dict_idx].v_type != VAR_DICT)
12430 {
12431 EMSG(_("E922: expected a dict"));
12432 vim_free(name);
12433 return;
12434 }
12435 if (argvars[dict_idx].vval.v_dict == NULL)
12436 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012437 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012438 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012439 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012440 if (argvars[arg_idx].v_type != VAR_LIST)
12441 {
12442 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12443 vim_free(name);
12444 return;
12445 }
12446 list = argvars[arg_idx].vval.v_list;
12447 if (list == NULL || list->lv_len == 0)
12448 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012449 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012450 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012451 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012452 {
12453 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012454
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012455 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012456 if (pt == NULL)
12457 vim_free(name);
12458 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012459 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012460 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012461 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012462 listitem_T *li;
12463 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012464 int arg_len = 0;
12465 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012466
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012467 if (arg_pt != NULL)
12468 arg_len = arg_pt->pt_argc;
12469 if (list != NULL)
12470 lv_len = list->lv_len;
12471 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012472 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012473 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012474 if (pt->pt_argv == NULL)
12475 {
12476 vim_free(pt);
12477 vim_free(name);
12478 return;
12479 }
12480 else
12481 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012482 for (i = 0; i < arg_len; i++)
12483 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12484 if (lv_len > 0)
12485 for (li = list->lv_first; li != NULL;
12486 li = li->li_next)
12487 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012488 }
12489 }
12490
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012491 /* For "function(dict.func, [], dict)" and "func" is a partial
12492 * use "dict". That is backwards compatible. */
12493 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012494 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012495 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012496 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12497 ++pt->pt_dict->dv_refcount;
12498 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012499 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012500 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012501 /* If the dict was bound automatically the result is also
12502 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012503 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012504 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012505 if (pt->pt_dict != NULL)
12506 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012507 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012508
12509 pt->pt_refcount = 1;
12510 pt->pt_name = name;
12511 func_ref(pt->pt_name);
12512 }
12513 rettv->v_type = VAR_PARTIAL;
12514 rettv->vval.v_partial = pt;
12515 }
12516 else
12517 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012518 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012519 rettv->v_type = VAR_FUNC;
12520 rettv->vval.v_string = name;
12521 func_ref(name);
12522 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012523 }
12524}
12525
12526/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012527 * "garbagecollect()" function
12528 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012530f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012531{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012532 /* This is postponed until we are back at the toplevel, because we may be
12533 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12534 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012535
12536 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12537 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012538}
12539
12540/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012541 * "get()" function
12542 */
12543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012544f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545{
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 listitem_T *li;
12547 list_T *l;
12548 dictitem_T *di;
12549 dict_T *d;
12550 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551
Bram Moolenaare9a41262005-01-15 22:18:47 +000012552 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012554 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012556 int error = FALSE;
12557
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012558 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012560 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012563 else if (argvars[0].v_type == VAR_DICT)
12564 {
12565 if ((d = argvars[0].vval.v_dict) != NULL)
12566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012567 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012568 if (di != NULL)
12569 tv = &di->di_tv;
12570 }
12571 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012572 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012573 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012574 partial_T *pt;
12575 partial_T fref_pt;
12576
12577 if (argvars[0].v_type == VAR_PARTIAL)
12578 pt = argvars[0].vval.v_partial;
12579 else
12580 {
12581 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12582 fref_pt.pt_name = argvars[0].vval.v_string;
12583 pt = &fref_pt;
12584 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012585
12586 if (pt != NULL)
12587 {
12588 char_u *what = get_tv_string(&argvars[1]);
12589
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012590 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012591 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012592 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012593 if (pt->pt_name == NULL)
12594 rettv->vval.v_string = NULL;
12595 else
12596 rettv->vval.v_string = vim_strsave(pt->pt_name);
12597 }
12598 else if (STRCMP(what, "dict") == 0)
12599 {
12600 rettv->v_type = VAR_DICT;
12601 rettv->vval.v_dict = pt->pt_dict;
12602 if (pt->pt_dict != NULL)
12603 ++pt->pt_dict->dv_refcount;
12604 }
12605 else if (STRCMP(what, "args") == 0)
12606 {
12607 rettv->v_type = VAR_LIST;
12608 if (rettv_list_alloc(rettv) == OK)
12609 {
12610 int i;
12611
12612 for (i = 0; i < pt->pt_argc; ++i)
12613 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12614 }
12615 }
12616 else
12617 EMSG2(_(e_invarg2), what);
12618 return;
12619 }
12620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012621 else
12622 EMSG2(_(e_listdictarg), "get()");
12623
12624 if (tv == NULL)
12625 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012627 copy_tv(&argvars[2], rettv);
12628 }
12629 else
12630 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631}
12632
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012633static 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 +000012634
12635/*
12636 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012637 * Return a range (from start to end) of lines in rettv from the specified
12638 * buffer.
12639 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012640 */
12641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012642get_buffer_lines(
12643 buf_T *buf,
12644 linenr_T start,
12645 linenr_T end,
12646 int retlist,
12647 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012648{
12649 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012650
Bram Moolenaar959a1432013-12-14 12:17:38 +010012651 rettv->v_type = VAR_STRING;
12652 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012653 if (retlist && rettv_list_alloc(rettv) == FAIL)
12654 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012655
12656 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12657 return;
12658
12659 if (!retlist)
12660 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012661 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12662 p = ml_get_buf(buf, start, FALSE);
12663 else
12664 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012665 rettv->vval.v_string = vim_strsave(p);
12666 }
12667 else
12668 {
12669 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012670 return;
12671
12672 if (start < 1)
12673 start = 1;
12674 if (end > buf->b_ml.ml_line_count)
12675 end = buf->b_ml.ml_line_count;
12676 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012677 if (list_append_string(rettv->vval.v_list,
12678 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012679 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012680 }
12681}
12682
12683/*
12684 * "getbufline()" function
12685 */
12686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012687f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012688{
12689 linenr_T lnum;
12690 linenr_T end;
12691 buf_T *buf;
12692
12693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12694 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012695 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012696 --emsg_off;
12697
Bram Moolenaar661b1822005-07-28 22:36:45 +000012698 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012699 if (argvars[2].v_type == VAR_UNKNOWN)
12700 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012701 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012702 end = get_tv_lnum_buf(&argvars[2], buf);
12703
Bram Moolenaar342337a2005-07-21 21:11:17 +000012704 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012705}
12706
Bram Moolenaar0d660222005-01-07 21:51:51 +000012707/*
12708 * "getbufvar()" function
12709 */
12710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012711f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012712{
12713 buf_T *buf;
12714 buf_T *save_curbuf;
12715 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012716 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012717 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12720 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012722 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012723
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012724 rettv->v_type = VAR_STRING;
12725 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012726
12727 if (buf != NULL && varname != NULL)
12728 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012729 /* set curbuf to be our buf, temporarily */
12730 save_curbuf = curbuf;
12731 curbuf = buf;
12732
Bram Moolenaar0d660222005-01-07 21:51:51 +000012733 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012734 {
12735 if (get_option_tv(&varname, rettv, TRUE) == OK)
12736 done = TRUE;
12737 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012738 else if (STRCMP(varname, "changedtick") == 0)
12739 {
12740 rettv->v_type = VAR_NUMBER;
12741 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012742 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012743 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012744 else
12745 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012746 /* Look up the variable. */
12747 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12748 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12749 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012750 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012751 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012752 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012753 done = TRUE;
12754 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012756
12757 /* restore previous notion of curbuf */
12758 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012759 }
12760
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012761 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12762 /* use the default value */
12763 copy_tv(&argvars[2], rettv);
12764
Bram Moolenaar0d660222005-01-07 21:51:51 +000012765 --emsg_off;
12766}
12767
12768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 * "getchar()" function
12770 */
12771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012772f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773{
12774 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012777 /* Position the cursor. Needed after a message that ends in a space. */
12778 windgoto(msg_row, msg_col);
12779
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 ++no_mapping;
12781 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012782 for (;;)
12783 {
12784 if (argvars[0].v_type == VAR_UNKNOWN)
12785 /* getchar(): blocking wait. */
12786 n = safe_vgetc();
12787 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12788 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012789 n = vpeekc_any();
12790 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012791 /* illegal argument or getchar(0) and no char avail: return zero */
12792 n = 0;
12793 else
12794 /* getchar(0) and char avail: return char */
12795 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012796
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012797 if (n == K_IGNORE)
12798 continue;
12799 break;
12800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 --no_mapping;
12802 --allow_keys;
12803
Bram Moolenaar219b8702006-11-01 14:32:36 +000012804 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012805 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012806 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12807 vimvars[VV_MOUSE_COL].vv_nr = 0;
12808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 if (IS_SPECIAL(n) || mod_mask != 0)
12811 {
12812 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12813 int i = 0;
12814
12815 /* Turn a special key into three bytes, plus modifier. */
12816 if (mod_mask != 0)
12817 {
12818 temp[i++] = K_SPECIAL;
12819 temp[i++] = KS_MODIFIER;
12820 temp[i++] = mod_mask;
12821 }
12822 if (IS_SPECIAL(n))
12823 {
12824 temp[i++] = K_SPECIAL;
12825 temp[i++] = K_SECOND(n);
12826 temp[i++] = K_THIRD(n);
12827 }
12828#ifdef FEAT_MBYTE
12829 else if (has_mbyte)
12830 i += (*mb_char2bytes)(n, temp + i);
12831#endif
12832 else
12833 temp[i++] = n;
12834 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012837
12838#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012839 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012840 {
12841 int row = mouse_row;
12842 int col = mouse_col;
12843 win_T *win;
12844 linenr_T lnum;
12845# ifdef FEAT_WINDOWS
12846 win_T *wp;
12847# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012848 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012849
12850 if (row >= 0 && col >= 0)
12851 {
12852 /* Find the window at the mouse coordinates and compute the
12853 * text position. */
12854 win = mouse_find_win(&row, &col);
12855 (void)mouse_comp_pos(win, &row, &col, &lnum);
12856# ifdef FEAT_WINDOWS
12857 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012858 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012859# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012860 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012861 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012862 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12863 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12864 }
12865 }
12866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 }
12868}
12869
12870/*
12871 * "getcharmod()" function
12872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012874f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877}
12878
12879/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012880 * "getcharsearch()" function
12881 */
12882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012883f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012884{
12885 if (rettv_dict_alloc(rettv) != FAIL)
12886 {
12887 dict_T *dict = rettv->vval.v_dict;
12888
12889 dict_add_nr_str(dict, "char", 0L, last_csearch());
12890 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12891 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12892 }
12893}
12894
12895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 * "getcmdline()" function
12897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012899f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->v_type = VAR_STRING;
12902 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903}
12904
12905/*
12906 * "getcmdpos()" function
12907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012909f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912}
12913
12914/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012915 * "getcmdtype()" function
12916 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012918f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012919{
12920 rettv->v_type = VAR_STRING;
12921 rettv->vval.v_string = alloc(2);
12922 if (rettv->vval.v_string != NULL)
12923 {
12924 rettv->vval.v_string[0] = get_cmdline_type();
12925 rettv->vval.v_string[1] = NUL;
12926 }
12927}
12928
12929/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012930 * "getcmdwintype()" function
12931 */
12932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012933f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012934{
12935 rettv->v_type = VAR_STRING;
12936 rettv->vval.v_string = NULL;
12937#ifdef FEAT_CMDWIN
12938 rettv->vval.v_string = alloc(2);
12939 if (rettv->vval.v_string != NULL)
12940 {
12941 rettv->vval.v_string[0] = cmdwin_type;
12942 rettv->vval.v_string[1] = NUL;
12943 }
12944#endif
12945}
12946
12947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 * "getcwd()" function
12949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012951f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012953 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012954 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012957 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012958
12959 wp = find_tabwin(&argvars[0], &argvars[1]);
12960 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012962 if (wp->w_localdir != NULL)
12963 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12964 else if(globaldir != NULL)
12965 rettv->vval.v_string = vim_strsave(globaldir);
12966 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012967 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012968 cwd = alloc(MAXPATHL);
12969 if (cwd != NULL)
12970 {
12971 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12972 rettv->vval.v_string = vim_strsave(cwd);
12973 vim_free(cwd);
12974 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012975 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012976#ifdef BACKSLASH_IN_FILENAME
12977 if (rettv->vval.v_string != NULL)
12978 slash_adjust(rettv->vval.v_string);
12979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 }
12981}
12982
12983/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012984 * "getfontname()" function
12985 */
12986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012987f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989 rettv->v_type = VAR_STRING;
12990 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012991#ifdef FEAT_GUI
12992 if (gui.in_use)
12993 {
12994 GuiFont font;
12995 char_u *name = NULL;
12996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012997 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012998 {
12999 /* Get the "Normal" font. Either the name saved by
13000 * hl_set_font_name() or from the font ID. */
13001 font = gui.norm_font;
13002 name = hl_get_font_name();
13003 }
13004 else
13005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013006 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013007 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13008 return;
13009 font = gui_mch_get_font(name, FALSE);
13010 if (font == NOFONT)
13011 return; /* Invalid font name, return empty string. */
13012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013014 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013015 gui_mch_free_font(font);
13016 }
13017#endif
13018}
13019
13020/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013021 * "getfperm({fname})" function
13022 */
13023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013024f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013025{
13026 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013027 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013028 char_u *perm = NULL;
13029 char_u flags[] = "rwx";
13030 int i;
13031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013035 if (mch_stat((char *)fname, &st) >= 0)
13036 {
13037 perm = vim_strsave((char_u *)"---------");
13038 if (perm != NULL)
13039 {
13040 for (i = 0; i < 9; i++)
13041 {
13042 if (st.st_mode & (1 << (8 - i)))
13043 perm[i] = flags[i % 3];
13044 }
13045 }
13046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013048}
13049
13050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051 * "getfsize({fname})" function
13052 */
13053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013054f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055{
13056 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013057 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062
13063 if (mch_stat((char *)fname, &st) >= 0)
13064 {
13065 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013070
13071 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013072 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013073 rettv->vval.v_number = -2;
13074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 }
13076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078}
13079
13080/*
13081 * "getftime({fname})" function
13082 */
13083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013084f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085{
13086 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013087 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090
13091 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013094 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095}
13096
13097/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013098 * "getftype({fname})" function
13099 */
13100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013101f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013102{
13103 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013104 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013105 char_u *type = NULL;
13106 char *t;
13107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013110 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013111 if (mch_lstat((char *)fname, &st) >= 0)
13112 {
13113#ifdef S_ISREG
13114 if (S_ISREG(st.st_mode))
13115 t = "file";
13116 else if (S_ISDIR(st.st_mode))
13117 t = "dir";
13118# ifdef S_ISLNK
13119 else if (S_ISLNK(st.st_mode))
13120 t = "link";
13121# endif
13122# ifdef S_ISBLK
13123 else if (S_ISBLK(st.st_mode))
13124 t = "bdev";
13125# endif
13126# ifdef S_ISCHR
13127 else if (S_ISCHR(st.st_mode))
13128 t = "cdev";
13129# endif
13130# ifdef S_ISFIFO
13131 else if (S_ISFIFO(st.st_mode))
13132 t = "fifo";
13133# endif
13134# ifdef S_ISSOCK
13135 else if (S_ISSOCK(st.st_mode))
13136 t = "fifo";
13137# endif
13138 else
13139 t = "other";
13140#else
13141# ifdef S_IFMT
13142 switch (st.st_mode & S_IFMT)
13143 {
13144 case S_IFREG: t = "file"; break;
13145 case S_IFDIR: t = "dir"; break;
13146# ifdef S_IFLNK
13147 case S_IFLNK: t = "link"; break;
13148# endif
13149# ifdef S_IFBLK
13150 case S_IFBLK: t = "bdev"; break;
13151# endif
13152# ifdef S_IFCHR
13153 case S_IFCHR: t = "cdev"; break;
13154# endif
13155# ifdef S_IFIFO
13156 case S_IFIFO: t = "fifo"; break;
13157# endif
13158# ifdef S_IFSOCK
13159 case S_IFSOCK: t = "socket"; break;
13160# endif
13161 default: t = "other";
13162 }
13163# else
13164 if (mch_isdir(fname))
13165 t = "dir";
13166 else
13167 t = "file";
13168# endif
13169#endif
13170 type = vim_strsave((char_u *)t);
13171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013173}
13174
13175/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013176 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013177 */
13178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013179f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013180{
13181 linenr_T lnum;
13182 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013183 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013184
13185 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013186 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013187 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013188 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013189 retlist = FALSE;
13190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013191 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013192 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013193 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013194 retlist = TRUE;
13195 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013196
Bram Moolenaar342337a2005-07-21 21:11:17 +000013197 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198}
13199
13200/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013201 * "getmatches()" function
13202 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013204f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013205{
13206#ifdef FEAT_SEARCH_EXTRA
13207 dict_T *dict;
13208 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013209 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013210
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013211 if (rettv_list_alloc(rettv) == OK)
13212 {
13213 while (cur != NULL)
13214 {
13215 dict = dict_alloc();
13216 if (dict == NULL)
13217 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013218 if (cur->match.regprog == NULL)
13219 {
13220 /* match added with matchaddpos() */
13221 for (i = 0; i < MAXPOSMATCH; ++i)
13222 {
13223 llpos_T *llpos;
13224 char buf[6];
13225 list_T *l;
13226
13227 llpos = &cur->pos.pos[i];
13228 if (llpos->lnum == 0)
13229 break;
13230 l = list_alloc();
13231 if (l == NULL)
13232 break;
13233 list_append_number(l, (varnumber_T)llpos->lnum);
13234 if (llpos->col > 0)
13235 {
13236 list_append_number(l, (varnumber_T)llpos->col);
13237 list_append_number(l, (varnumber_T)llpos->len);
13238 }
13239 sprintf(buf, "pos%d", i + 1);
13240 dict_add_list(dict, buf, l);
13241 }
13242 }
13243 else
13244 {
13245 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13246 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013247 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013248 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13249 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013250# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013251 if (cur->conceal_char)
13252 {
13253 char_u buf[MB_MAXBYTES + 1];
13254
13255 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13256 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13257 }
13258# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013259 list_append_dict(rettv->vval.v_list, dict);
13260 cur = cur->next;
13261 }
13262 }
13263#endif
13264}
13265
13266/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013267 * "getpid()" function
13268 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013270f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013271{
13272 rettv->vval.v_number = mch_get_pid();
13273}
13274
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013276getpos_both(
13277 typval_T *argvars,
13278 typval_T *rettv,
13279 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013280{
Bram Moolenaara5525202006-03-02 22:52:09 +000013281 pos_T *fp;
13282 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013283 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013284
13285 if (rettv_list_alloc(rettv) == OK)
13286 {
13287 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013288 if (getcurpos)
13289 fp = &curwin->w_cursor;
13290 else
13291 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013292 if (fnum != -1)
13293 list_append_number(l, (varnumber_T)fnum);
13294 else
13295 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013296 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13297 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013298 list_append_number(l, (fp != NULL)
13299 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013300 : (varnumber_T)0);
13301 list_append_number(l,
13302#ifdef FEAT_VIRTUALEDIT
13303 (fp != NULL) ? (varnumber_T)fp->coladd :
13304#endif
13305 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013306 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013307 {
13308 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013309 list_append_number(l, curwin->w_curswant == MAXCOL ?
13310 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013311 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013312 }
13313 else
13314 rettv->vval.v_number = FALSE;
13315}
13316
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013317
13318/*
13319 * "getcurpos()" function
13320 */
13321 static void
13322f_getcurpos(typval_T *argvars, typval_T *rettv)
13323{
13324 getpos_both(argvars, rettv, TRUE);
13325}
13326
13327/*
13328 * "getpos(string)" function
13329 */
13330 static void
13331f_getpos(typval_T *argvars, typval_T *rettv)
13332{
13333 getpos_both(argvars, rettv, FALSE);
13334}
13335
Bram Moolenaara5525202006-03-02 22:52:09 +000013336/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013337 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013338 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013340f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013341{
13342#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013343 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013344#endif
13345
Bram Moolenaar2641f772005-03-25 21:58:17 +000013346#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013347 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013348 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013349 wp = NULL;
13350 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13351 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013352 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013353 if (wp == NULL)
13354 return;
13355 }
13356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013357 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013358 }
13359#endif
13360}
13361
13362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 * "getreg()" function
13364 */
13365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013366f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367{
13368 char_u *strregname;
13369 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013370 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013371 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013372 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013374 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013376 strregname = get_tv_string_chk(&argvars[0]);
13377 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013378 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013379 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013380 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013381 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013382 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013383 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013386 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013387
13388 if (error)
13389 return;
13390
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 regname = (strregname == NULL ? '"' : *strregname);
13392 if (regname == 0)
13393 regname = '"';
13394
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013395 if (return_list)
13396 {
13397 rettv->v_type = VAR_LIST;
13398 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13399 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013400 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013401 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013402 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013403 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013404 }
13405 else
13406 {
13407 rettv->v_type = VAR_STRING;
13408 rettv->vval.v_string = get_reg_contents(regname,
13409 arg2 ? GREG_EXPR_SRC : 0);
13410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411}
13412
13413/*
13414 * "getregtype()" function
13415 */
13416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013417f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418{
13419 char_u *strregname;
13420 int regname;
13421 char_u buf[NUMBUFLEN + 2];
13422 long reglen = 0;
13423
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013424 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 {
13426 strregname = get_tv_string_chk(&argvars[0]);
13427 if (strregname == NULL) /* type error; errmsg already given */
13428 {
13429 rettv->v_type = VAR_STRING;
13430 rettv->vval.v_string = NULL;
13431 return;
13432 }
13433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 else
13435 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013436 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
13438 regname = (strregname == NULL ? '"' : *strregname);
13439 if (regname == 0)
13440 regname = '"';
13441
13442 buf[0] = NUL;
13443 buf[1] = NUL;
13444 switch (get_reg_type(regname, &reglen))
13445 {
13446 case MLINE: buf[0] = 'V'; break;
13447 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 case MBLOCK:
13449 buf[0] = Ctrl_V;
13450 sprintf((char *)buf + 1, "%ld", reglen + 1);
13451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013453 rettv->v_type = VAR_STRING;
13454 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455}
13456
13457/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013458 * "gettabvar()" function
13459 */
13460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013461f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013462{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013463 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013464 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013465 dictitem_T *v;
13466 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013467 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013468
13469 rettv->v_type = VAR_STRING;
13470 rettv->vval.v_string = NULL;
13471
13472 varname = get_tv_string_chk(&argvars[1]);
13473 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13474 if (tp != NULL && varname != NULL)
13475 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013476 /* Set tp to be our tabpage, temporarily. Also set the window to the
13477 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013478 if (switch_win(&oldcurwin, &oldtabpage,
13479 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013480 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013481 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013482 /* look up the variable */
13483 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13484 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13485 if (v != NULL)
13486 {
13487 copy_tv(&v->di_tv, rettv);
13488 done = TRUE;
13489 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013490 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013491
13492 /* restore previous notion of curwin */
13493 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013494 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013495
13496 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013497 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013498}
13499
13500/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013501 * "gettabwinvar()" function
13502 */
13503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013504f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013505{
13506 getwinvar(argvars, rettv, 1);
13507}
13508
13509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 * "getwinposx()" function
13511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013513f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516#ifdef FEAT_GUI
13517 if (gui.in_use)
13518 {
13519 int x, y;
13520
13521 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 }
13524#endif
13525}
13526
13527/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013528 * "win_findbuf()" function
13529 */
13530 static void
13531f_win_findbuf(typval_T *argvars, typval_T *rettv)
13532{
13533 if (rettv_list_alloc(rettv) != FAIL)
13534 win_findbuf(argvars, rettv->vval.v_list);
13535}
13536
13537/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013538 * "win_getid()" function
13539 */
13540 static void
13541f_win_getid(typval_T *argvars, typval_T *rettv)
13542{
13543 rettv->vval.v_number = win_getid(argvars);
13544}
13545
13546/*
13547 * "win_gotoid()" function
13548 */
13549 static void
13550f_win_gotoid(typval_T *argvars, typval_T *rettv)
13551{
13552 rettv->vval.v_number = win_gotoid(argvars);
13553}
13554
13555/*
13556 * "win_id2tabwin()" function
13557 */
13558 static void
13559f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13560{
13561 if (rettv_list_alloc(rettv) != FAIL)
13562 win_id2tabwin(argvars, rettv->vval.v_list);
13563}
13564
13565/*
13566 * "win_id2win()" function
13567 */
13568 static void
13569f_win_id2win(typval_T *argvars, typval_T *rettv)
13570{
13571 rettv->vval.v_number = win_id2win(argvars);
13572}
13573
13574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 * "getwinposy()" function
13576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013578f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013580 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581#ifdef FEAT_GUI
13582 if (gui.in_use)
13583 {
13584 int x, y;
13585
13586 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 }
13589#endif
13590}
13591
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013592/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013593 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013594 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013595 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013596find_win_by_nr(
13597 typval_T *vp,
13598 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013599{
13600#ifdef FEAT_WINDOWS
13601 win_T *wp;
13602#endif
13603 int nr;
13604
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013605 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013606
13607#ifdef FEAT_WINDOWS
13608 if (nr < 0)
13609 return NULL;
13610 if (nr == 0)
13611 return curwin;
13612
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013613 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13614 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013615 if (nr >= LOWEST_WIN_ID)
13616 {
13617 if (wp->w_id == nr)
13618 return wp;
13619 }
13620 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013621 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013622 if (nr >= LOWEST_WIN_ID)
13623 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013624 return wp;
13625#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013626 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013627 return curwin;
13628 return NULL;
13629#endif
13630}
13631
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013633 * Find window specified by "wvp" in tabpage "tvp".
13634 */
13635 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013636find_tabwin(
13637 typval_T *wvp, /* VAR_UNKNOWN for current window */
13638 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013639{
13640 win_T *wp = NULL;
13641 tabpage_T *tp = NULL;
13642 long n;
13643
13644 if (wvp->v_type != VAR_UNKNOWN)
13645 {
13646 if (tvp->v_type != VAR_UNKNOWN)
13647 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013648 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013649 if (n >= 0)
13650 tp = find_tabpage(n);
13651 }
13652 else
13653 tp = curtab;
13654
13655 if (tp != NULL)
13656 wp = find_win_by_nr(wvp, tp);
13657 }
13658 else
13659 wp = curwin;
13660
13661 return wp;
13662}
13663
13664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 * "getwinvar()" function
13666 */
13667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013668f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013670 getwinvar(argvars, rettv, 0);
13671}
13672
13673/*
13674 * getwinvar() and gettabwinvar()
13675 */
13676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013677getwinvar(
13678 typval_T *argvars,
13679 typval_T *rettv,
13680 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013681{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013682 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013685 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013686 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013687#ifdef FEAT_WINDOWS
13688 win_T *oldcurwin;
13689 tabpage_T *oldtabpage;
13690 int need_switch_win;
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013693#ifdef FEAT_WINDOWS
13694 if (off == 1)
13695 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13696 else
13697 tp = curtab;
13698#endif
13699 win = find_win_by_nr(&argvars[off], tp);
13700 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013703 rettv->v_type = VAR_STRING;
13704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705
13706 if (win != NULL && varname != NULL)
13707 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013708#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013709 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013710 * otherwise the window is not valid. Only do this when needed,
13711 * autocommands get blocked. */
13712 need_switch_win = !(tp == curtab && win == curwin);
13713 if (!need_switch_win
13714 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13715#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013716 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013717 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013718 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013719 if (get_option_tv(&varname, rettv, 1) == OK)
13720 done = TRUE;
13721 }
13722 else
13723 {
13724 /* Look up the variable. */
13725 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13726 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13727 varname, FALSE);
13728 if (v != NULL)
13729 {
13730 copy_tv(&v->di_tv, rettv);
13731 done = TRUE;
13732 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013735
Bram Moolenaarba117c22015-09-29 16:53:22 +020013736#ifdef FEAT_WINDOWS
13737 if (need_switch_win)
13738 /* restore previous notion of curwin */
13739 restore_win(oldcurwin, oldtabpage, TRUE);
13740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 }
13742
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013743 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13744 /* use the default return value */
13745 copy_tv(&argvars[off + 2], rettv);
13746
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 --emsg_off;
13748}
13749
13750/*
13751 * "glob()" function
13752 */
13753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013754f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013756 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013758 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013760 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013761 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013763 if (argvars[1].v_type != VAR_UNKNOWN)
13764 {
13765 if (get_tv_number_chk(&argvars[1], &error))
13766 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013768 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013769 if (get_tv_number_chk(&argvars[2], &error))
13770 {
13771 rettv->v_type = VAR_LIST;
13772 rettv->vval.v_list = NULL;
13773 }
13774 if (argvars[3].v_type != VAR_UNKNOWN
13775 && get_tv_number_chk(&argvars[3], &error))
13776 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013777 }
13778 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013779 if (!error)
13780 {
13781 ExpandInit(&xpc);
13782 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013783 if (p_wic)
13784 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013785 if (rettv->v_type == VAR_STRING)
13786 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013787 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013788 else if (rettv_list_alloc(rettv) != FAIL)
13789 {
13790 int i;
13791
13792 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13793 NULL, options, WILD_ALL_KEEP);
13794 for (i = 0; i < xpc.xp_numfiles; i++)
13795 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13796
13797 ExpandCleanup(&xpc);
13798 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013799 }
13800 else
13801 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
13804/*
13805 * "globpath()" function
13806 */
13807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013808f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013810 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013813 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013814 garray_T ga;
13815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013817 /* When the optional second argument is non-zero, don't remove matches
13818 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013819 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013820 if (argvars[2].v_type != VAR_UNKNOWN)
13821 {
13822 if (get_tv_number_chk(&argvars[2], &error))
13823 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013824 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013825 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013826 if (get_tv_number_chk(&argvars[3], &error))
13827 {
13828 rettv->v_type = VAR_LIST;
13829 rettv->vval.v_list = NULL;
13830 }
13831 if (argvars[4].v_type != VAR_UNKNOWN
13832 && get_tv_number_chk(&argvars[4], &error))
13833 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013834 }
13835 }
13836 if (file != NULL && !error)
13837 {
13838 ga_init2(&ga, (int)sizeof(char_u *), 10);
13839 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13840 if (rettv->v_type == VAR_STRING)
13841 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13842 else if (rettv_list_alloc(rettv) != FAIL)
13843 for (i = 0; i < ga.ga_len; ++i)
13844 list_append_string(rettv->vval.v_list,
13845 ((char_u **)(ga.ga_data))[i], -1);
13846 ga_clear_strings(&ga);
13847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850}
13851
13852/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013853 * "glob2regpat()" function
13854 */
13855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013856f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013857{
13858 char_u *pat = get_tv_string_chk(&argvars[0]);
13859
13860 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013861 rettv->vval.v_string = (pat == NULL)
13862 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013863}
13864
13865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 * "has()" function
13867 */
13868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013869f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870{
13871 int i;
13872 char_u *name;
13873 int n = FALSE;
13874 static char *(has_list[]) =
13875 {
13876#ifdef AMIGA
13877 "amiga",
13878# ifdef FEAT_ARP
13879 "arp",
13880# endif
13881#endif
13882#ifdef __BEOS__
13883 "beos",
13884#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013885#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 "mac",
13887#endif
13888#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013889 "macunix", /* built with 'darwin' enabled */
13890#endif
13891#if defined(__APPLE__) && __APPLE__ == 1
13892 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894#ifdef __QNX__
13895 "qnx",
13896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#ifdef UNIX
13898 "unix",
13899#endif
13900#ifdef VMS
13901 "vms",
13902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#ifdef WIN32
13904 "win32",
13905#endif
13906#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13907 "win32unix",
13908#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013909#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 "win64",
13911#endif
13912#ifdef EBCDIC
13913 "ebcdic",
13914#endif
13915#ifndef CASE_INSENSITIVE_FILENAME
13916 "fname_case",
13917#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013918#ifdef HAVE_ACL
13919 "acl",
13920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921#ifdef FEAT_ARABIC
13922 "arabic",
13923#endif
13924#ifdef FEAT_AUTOCMD
13925 "autocmd",
13926#endif
13927#ifdef FEAT_BEVAL
13928 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013929# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13930 "balloon_multiline",
13931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932#endif
13933#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13934 "builtin_terms",
13935# ifdef ALL_BUILTIN_TCAPS
13936 "all_builtin_terms",
13937# endif
13938#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013939#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13940 || defined(FEAT_GUI_W32) \
13941 || defined(FEAT_GUI_MOTIF))
13942 "browsefilter",
13943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944#ifdef FEAT_BYTEOFF
13945 "byte_offset",
13946#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013947#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013948 "channel",
13949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950#ifdef FEAT_CINDENT
13951 "cindent",
13952#endif
13953#ifdef FEAT_CLIENTSERVER
13954 "clientserver",
13955#endif
13956#ifdef FEAT_CLIPBOARD
13957 "clipboard",
13958#endif
13959#ifdef FEAT_CMDL_COMPL
13960 "cmdline_compl",
13961#endif
13962#ifdef FEAT_CMDHIST
13963 "cmdline_hist",
13964#endif
13965#ifdef FEAT_COMMENTS
13966 "comments",
13967#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013968#ifdef FEAT_CONCEAL
13969 "conceal",
13970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#ifdef FEAT_CRYPT
13972 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013973 "crypt-blowfish",
13974 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#endif
13976#ifdef FEAT_CSCOPE
13977 "cscope",
13978#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013979#ifdef FEAT_CURSORBIND
13980 "cursorbind",
13981#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013982#ifdef CURSOR_SHAPE
13983 "cursorshape",
13984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985#ifdef DEBUG
13986 "debug",
13987#endif
13988#ifdef FEAT_CON_DIALOG
13989 "dialog_con",
13990#endif
13991#ifdef FEAT_GUI_DIALOG
13992 "dialog_gui",
13993#endif
13994#ifdef FEAT_DIFF
13995 "diff",
13996#endif
13997#ifdef FEAT_DIGRAPHS
13998 "digraphs",
13999#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014000#ifdef FEAT_DIRECTX
14001 "directx",
14002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003#ifdef FEAT_DND
14004 "dnd",
14005#endif
14006#ifdef FEAT_EMACS_TAGS
14007 "emacs_tags",
14008#endif
14009 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014010 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef FEAT_SEARCH_EXTRA
14012 "extra_search",
14013#endif
14014#ifdef FEAT_FKMAP
14015 "farsi",
14016#endif
14017#ifdef FEAT_SEARCHPATH
14018 "file_in_path",
14019#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014020#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014021 "filterpipe",
14022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023#ifdef FEAT_FIND_ID
14024 "find_in_path",
14025#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014026#ifdef FEAT_FLOAT
14027 "float",
14028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029#ifdef FEAT_FOLDING
14030 "folding",
14031#endif
14032#ifdef FEAT_FOOTER
14033 "footer",
14034#endif
14035#if !defined(USE_SYSTEM) && defined(UNIX)
14036 "fork",
14037#endif
14038#ifdef FEAT_GETTEXT
14039 "gettext",
14040#endif
14041#ifdef FEAT_GUI
14042 "gui",
14043#endif
14044#ifdef FEAT_GUI_ATHENA
14045# ifdef FEAT_GUI_NEXTAW
14046 "gui_neXtaw",
14047# else
14048 "gui_athena",
14049# endif
14050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051#ifdef FEAT_GUI_GTK
14052 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014053# ifdef USE_GTK3
14054 "gui_gtk3",
14055# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014059#ifdef FEAT_GUI_GNOME
14060 "gui_gnome",
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#ifdef FEAT_GUI_MAC
14063 "gui_mac",
14064#endif
14065#ifdef FEAT_GUI_MOTIF
14066 "gui_motif",
14067#endif
14068#ifdef FEAT_GUI_PHOTON
14069 "gui_photon",
14070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#ifdef FEAT_GUI_W32
14072 "gui_win32",
14073#endif
14074#ifdef FEAT_HANGULIN
14075 "hangul_input",
14076#endif
14077#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14078 "iconv",
14079#endif
14080#ifdef FEAT_INS_EXPAND
14081 "insert_expand",
14082#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014083#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014084 "job",
14085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086#ifdef FEAT_JUMPLIST
14087 "jumplist",
14088#endif
14089#ifdef FEAT_KEYMAP
14090 "keymap",
14091#endif
14092#ifdef FEAT_LANGMAP
14093 "langmap",
14094#endif
14095#ifdef FEAT_LIBCALL
14096 "libcall",
14097#endif
14098#ifdef FEAT_LINEBREAK
14099 "linebreak",
14100#endif
14101#ifdef FEAT_LISP
14102 "lispindent",
14103#endif
14104#ifdef FEAT_LISTCMDS
14105 "listcmds",
14106#endif
14107#ifdef FEAT_LOCALMAP
14108 "localmap",
14109#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014110#ifdef FEAT_LUA
14111# ifndef DYNAMIC_LUA
14112 "lua",
14113# endif
14114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115#ifdef FEAT_MENU
14116 "menu",
14117#endif
14118#ifdef FEAT_SESSION
14119 "mksession",
14120#endif
14121#ifdef FEAT_MODIFY_FNAME
14122 "modify_fname",
14123#endif
14124#ifdef FEAT_MOUSE
14125 "mouse",
14126#endif
14127#ifdef FEAT_MOUSESHAPE
14128 "mouseshape",
14129#endif
14130#if defined(UNIX) || defined(VMS)
14131# ifdef FEAT_MOUSE_DEC
14132 "mouse_dec",
14133# endif
14134# ifdef FEAT_MOUSE_GPM
14135 "mouse_gpm",
14136# endif
14137# ifdef FEAT_MOUSE_JSB
14138 "mouse_jsbterm",
14139# endif
14140# ifdef FEAT_MOUSE_NET
14141 "mouse_netterm",
14142# endif
14143# ifdef FEAT_MOUSE_PTERM
14144 "mouse_pterm",
14145# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014146# ifdef FEAT_MOUSE_SGR
14147 "mouse_sgr",
14148# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014149# ifdef FEAT_SYSMOUSE
14150 "mouse_sysmouse",
14151# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014152# ifdef FEAT_MOUSE_URXVT
14153 "mouse_urxvt",
14154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155# ifdef FEAT_MOUSE_XTERM
14156 "mouse_xterm",
14157# endif
14158#endif
14159#ifdef FEAT_MBYTE
14160 "multi_byte",
14161#endif
14162#ifdef FEAT_MBYTE_IME
14163 "multi_byte_ime",
14164#endif
14165#ifdef FEAT_MULTI_LANG
14166 "multi_lang",
14167#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014168#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014169#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014170 "mzscheme",
14171#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014172#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014173#ifdef FEAT_NUM64
14174 "num64",
14175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176#ifdef FEAT_OLE
14177 "ole",
14178#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014179 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180#ifdef FEAT_PATH_EXTRA
14181 "path_extra",
14182#endif
14183#ifdef FEAT_PERL
14184#ifndef DYNAMIC_PERL
14185 "perl",
14186#endif
14187#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014188#ifdef FEAT_PERSISTENT_UNDO
14189 "persistent_undo",
14190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191#ifdef FEAT_PYTHON
14192#ifndef DYNAMIC_PYTHON
14193 "python",
14194#endif
14195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014196#ifdef FEAT_PYTHON3
14197#ifndef DYNAMIC_PYTHON3
14198 "python3",
14199#endif
14200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#ifdef FEAT_POSTSCRIPT
14202 "postscript",
14203#endif
14204#ifdef FEAT_PRINTER
14205 "printer",
14206#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014207#ifdef FEAT_PROFILE
14208 "profile",
14209#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014210#ifdef FEAT_RELTIME
14211 "reltime",
14212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213#ifdef FEAT_QUICKFIX
14214 "quickfix",
14215#endif
14216#ifdef FEAT_RIGHTLEFT
14217 "rightleft",
14218#endif
14219#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14220 "ruby",
14221#endif
14222#ifdef FEAT_SCROLLBIND
14223 "scrollbind",
14224#endif
14225#ifdef FEAT_CMDL_INFO
14226 "showcmd",
14227 "cmdline_info",
14228#endif
14229#ifdef FEAT_SIGNS
14230 "signs",
14231#endif
14232#ifdef FEAT_SMARTINDENT
14233 "smartindent",
14234#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014235#ifdef STARTUPTIME
14236 "startuptime",
14237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238#ifdef FEAT_STL_OPT
14239 "statusline",
14240#endif
14241#ifdef FEAT_SUN_WORKSHOP
14242 "sun_workshop",
14243#endif
14244#ifdef FEAT_NETBEANS_INTG
14245 "netbeans_intg",
14246#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014247#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014248 "spell",
14249#endif
14250#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 "syntax",
14252#endif
14253#if defined(USE_SYSTEM) || !defined(UNIX)
14254 "system",
14255#endif
14256#ifdef FEAT_TAG_BINS
14257 "tag_binary",
14258#endif
14259#ifdef FEAT_TAG_OLDSTATIC
14260 "tag_old_static",
14261#endif
14262#ifdef FEAT_TAG_ANYWHITE
14263 "tag_any_white",
14264#endif
14265#ifdef FEAT_TCL
14266# ifndef DYNAMIC_TCL
14267 "tcl",
14268# endif
14269#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014270#ifdef FEAT_TERMGUICOLORS
14271 "termguicolors",
14272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273#ifdef TERMINFO
14274 "terminfo",
14275#endif
14276#ifdef FEAT_TERMRESPONSE
14277 "termresponse",
14278#endif
14279#ifdef FEAT_TEXTOBJ
14280 "textobjects",
14281#endif
14282#ifdef HAVE_TGETENT
14283 "tgetent",
14284#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014285#ifdef FEAT_TIMERS
14286 "timers",
14287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288#ifdef FEAT_TITLE
14289 "title",
14290#endif
14291#ifdef FEAT_TOOLBAR
14292 "toolbar",
14293#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014294#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14295 "unnamedplus",
14296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297#ifdef FEAT_USR_CMDS
14298 "user-commands", /* was accidentally included in 5.4 */
14299 "user_commands",
14300#endif
14301#ifdef FEAT_VIMINFO
14302 "viminfo",
14303#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014304#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 "vertsplit",
14306#endif
14307#ifdef FEAT_VIRTUALEDIT
14308 "virtualedit",
14309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311#ifdef FEAT_VISUALEXTRA
14312 "visualextra",
14313#endif
14314#ifdef FEAT_VREPLACE
14315 "vreplace",
14316#endif
14317#ifdef FEAT_WILDIGN
14318 "wildignore",
14319#endif
14320#ifdef FEAT_WILDMENU
14321 "wildmenu",
14322#endif
14323#ifdef FEAT_WINDOWS
14324 "windows",
14325#endif
14326#ifdef FEAT_WAK
14327 "winaltkeys",
14328#endif
14329#ifdef FEAT_WRITEBACKUP
14330 "writebackup",
14331#endif
14332#ifdef FEAT_XIM
14333 "xim",
14334#endif
14335#ifdef FEAT_XFONTSET
14336 "xfontset",
14337#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014338#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014339 "xpm",
14340 "xpm_w32", /* for backward compatibility */
14341#else
14342# if defined(HAVE_XPM)
14343 "xpm",
14344# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346#ifdef USE_XSMP
14347 "xsmp",
14348#endif
14349#ifdef USE_XSMP_INTERACT
14350 "xsmp_interact",
14351#endif
14352#ifdef FEAT_XCLIPBOARD
14353 "xterm_clipboard",
14354#endif
14355#ifdef FEAT_XTERM_SAVE
14356 "xterm_save",
14357#endif
14358#if defined(UNIX) && defined(FEAT_X11)
14359 "X11",
14360#endif
14361 NULL
14362 };
14363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 for (i = 0; has_list[i] != NULL; ++i)
14366 if (STRICMP(name, has_list[i]) == 0)
14367 {
14368 n = TRUE;
14369 break;
14370 }
14371
14372 if (n == FALSE)
14373 {
14374 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014375 {
14376 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014377 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014378 && vim_isdigit(name[6])
14379 && vim_isdigit(name[8])
14380 && vim_isdigit(name[10]))
14381 {
14382 int major = atoi((char *)name + 6);
14383 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014384
14385 /* Expect "patch-9.9.01234". */
14386 n = (major < VIM_VERSION_MAJOR
14387 || (major == VIM_VERSION_MAJOR
14388 && (minor < VIM_VERSION_MINOR
14389 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014390 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014391 }
14392 else
14393 n = has_patch(atoi((char *)name + 5));
14394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 else if (STRICMP(name, "vim_starting") == 0)
14396 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014397#ifdef FEAT_MBYTE
14398 else if (STRICMP(name, "multi_byte_encoding") == 0)
14399 n = has_mbyte;
14400#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014401#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14402 else if (STRICMP(name, "balloon_multiline") == 0)
14403 n = multiline_balloon_available();
14404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#ifdef DYNAMIC_TCL
14406 else if (STRICMP(name, "tcl") == 0)
14407 n = tcl_enabled(FALSE);
14408#endif
14409#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14410 else if (STRICMP(name, "iconv") == 0)
14411 n = iconv_enabled(FALSE);
14412#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014413#ifdef DYNAMIC_LUA
14414 else if (STRICMP(name, "lua") == 0)
14415 n = lua_enabled(FALSE);
14416#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014417#ifdef DYNAMIC_MZSCHEME
14418 else if (STRICMP(name, "mzscheme") == 0)
14419 n = mzscheme_enabled(FALSE);
14420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421#ifdef DYNAMIC_RUBY
14422 else if (STRICMP(name, "ruby") == 0)
14423 n = ruby_enabled(FALSE);
14424#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014425#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426#ifdef DYNAMIC_PYTHON
14427 else if (STRICMP(name, "python") == 0)
14428 n = python_enabled(FALSE);
14429#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014430#endif
14431#ifdef FEAT_PYTHON3
14432#ifdef DYNAMIC_PYTHON3
14433 else if (STRICMP(name, "python3") == 0)
14434 n = python3_enabled(FALSE);
14435#endif
14436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437#ifdef DYNAMIC_PERL
14438 else if (STRICMP(name, "perl") == 0)
14439 n = perl_enabled(FALSE);
14440#endif
14441#ifdef FEAT_GUI
14442 else if (STRICMP(name, "gui_running") == 0)
14443 n = (gui.in_use || gui.starting);
14444# ifdef FEAT_GUI_W32
14445 else if (STRICMP(name, "gui_win32s") == 0)
14446 n = gui_is_win32s();
14447# endif
14448# ifdef FEAT_BROWSE
14449 else if (STRICMP(name, "browse") == 0)
14450 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14451# endif
14452#endif
14453#ifdef FEAT_SYN_HL
14454 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014455 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456#endif
14457#if defined(WIN3264)
14458 else if (STRICMP(name, "win95") == 0)
14459 n = mch_windows95();
14460#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014461#ifdef FEAT_NETBEANS_INTG
14462 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014463 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 }
14466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014471 * "has_key()" function
14472 */
14473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014475{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014476 if (argvars[0].v_type != VAR_DICT)
14477 {
14478 EMSG(_(e_dictreq));
14479 return;
14480 }
14481 if (argvars[0].vval.v_dict == NULL)
14482 return;
14483
14484 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014485 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014486}
14487
14488/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014489 * "haslocaldir()" function
14490 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014492f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014493{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014494 win_T *wp = NULL;
14495
14496 wp = find_tabwin(&argvars[0], &argvars[1]);
14497 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014498}
14499
14500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 * "hasmapto()" function
14502 */
14503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014504f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505{
14506 char_u *name;
14507 char_u *mode;
14508 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014509 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014512 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 mode = (char_u *)"nvo";
14514 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014517 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014518 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520
Bram Moolenaar2c932302006-03-18 21:42:09 +000014521 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014522 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525}
14526
14527/*
14528 * "histadd()" function
14529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014531f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
14533#ifdef FEAT_CMDHIST
14534 int histype;
14535 char_u *str;
14536 char_u buf[NUMBUFLEN];
14537#endif
14538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 if (check_restricted() || check_secure())
14541 return;
14542#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14544 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 if (histype >= 0)
14546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548 if (*str != NUL)
14549 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014550 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 return;
14554 }
14555 }
14556#endif
14557}
14558
14559/*
14560 * "histdel()" function
14561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014563f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564{
14565#ifdef FEAT_CMDHIST
14566 int n;
14567 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14571 if (str == NULL)
14572 n = 0;
14573 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014575 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014576 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 else
14581 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014583 get_tv_string_buf(&argvars[1], buf));
14584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585#endif
14586}
14587
14588/*
14589 * "histget()" function
14590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014592f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593{
14594#ifdef FEAT_CMDHIST
14595 int type;
14596 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14600 if (str == NULL)
14601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 {
14604 type = get_histtype(str);
14605 if (argvars[1].v_type == VAR_UNKNOWN)
14606 idx = get_history_idx(type);
14607 else
14608 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14609 /* -1 on type error */
14610 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014613 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616}
14617
14618/*
14619 * "histnr()" function
14620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014622f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623{
14624 int i;
14625
14626#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 char_u *history = get_tv_string_chk(&argvars[0]);
14628
14629 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 if (i >= HIST_CMD && i < HIST_COUNT)
14631 i = get_history_idx(i);
14632 else
14633#endif
14634 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636}
14637
14638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 * "highlightID(name)" function
14640 */
14641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014642f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014644 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645}
14646
14647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 * "highlight_exists()" function
14649 */
14650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014651f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652{
14653 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14654}
14655
14656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 * "hostname()" function
14658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014660f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661{
14662 char_u hostname[256];
14663
14664 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->v_type = VAR_STRING;
14666 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
14669/*
14670 * iconv() function
14671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014673f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675#ifdef FEAT_MBYTE
14676 char_u buf1[NUMBUFLEN];
14677 char_u buf2[NUMBUFLEN];
14678 char_u *from, *to, *str;
14679 vimconv_T vimconv;
14680#endif
14681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->v_type = VAR_STRING;
14683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684
14685#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 str = get_tv_string(&argvars[0]);
14687 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14688 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 vimconv.vc_type = CONV_NONE;
14690 convert_setup(&vimconv, from, to);
14691
14692 /* If the encodings are equal, no conversion needed. */
14693 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014694 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697
14698 convert_setup(&vimconv, NULL, NULL);
14699 vim_free(from);
14700 vim_free(to);
14701#endif
14702}
14703
14704/*
14705 * "indent()" function
14706 */
14707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014708f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709{
14710 linenr_T lnum;
14711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014714 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717}
14718
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014719/*
14720 * "index()" function
14721 */
14722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014723f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014724{
Bram Moolenaar33570922005-01-25 22:26:29 +000014725 list_T *l;
14726 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014727 long idx = 0;
14728 int ic = FALSE;
14729
14730 rettv->vval.v_number = -1;
14731 if (argvars[0].v_type != VAR_LIST)
14732 {
14733 EMSG(_(e_listreq));
14734 return;
14735 }
14736 l = argvars[0].vval.v_list;
14737 if (l != NULL)
14738 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014739 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014740 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 int error = FALSE;
14743
Bram Moolenaar758711c2005-02-02 23:11:38 +000014744 /* Start at specified item. Use the cached index that list_find()
14745 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014746 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014747 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014748 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014749 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014750 if (error)
14751 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014752 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014753
Bram Moolenaar758711c2005-02-02 23:11:38 +000014754 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014755 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014756 {
14757 rettv->vval.v_number = idx;
14758 break;
14759 }
14760 }
14761}
14762
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763static int inputsecret_flag = 0;
14764
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014765static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014766
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014768 * This function is used by f_input() and f_inputdialog() functions. The third
14769 * argument to f_input() specifies the type of completion to use at the
14770 * prompt. The third argument to f_inputdialog() specifies the value to return
14771 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 */
14773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014774get_user_input(
14775 typval_T *argvars,
14776 typval_T *rettv,
14777 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780 char_u *p = NULL;
14781 int c;
14782 char_u buf[NUMBUFLEN];
14783 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014785 int xp_type = EXPAND_NOTHING;
14786 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014789 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790
14791#ifdef NO_CONSOLE_INPUT
14792 /* While starting up, there is no place to enter text. */
14793 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795#endif
14796
14797 cmd_silent = FALSE; /* Want to see the prompt. */
14798 if (prompt != NULL)
14799 {
14800 /* Only the part of the message after the last NL is considered as
14801 * prompt for the command line */
14802 p = vim_strrchr(prompt, '\n');
14803 if (p == NULL)
14804 p = prompt;
14805 else
14806 {
14807 ++p;
14808 c = *p;
14809 *p = NUL;
14810 msg_start();
14811 msg_clr_eos();
14812 msg_puts_attr(prompt, echo_attr);
14813 msg_didout = FALSE;
14814 msg_starthere();
14815 *p = c;
14816 }
14817 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 if (argvars[1].v_type != VAR_UNKNOWN)
14820 {
14821 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14822 if (defstr != NULL)
14823 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014825 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014826 {
14827 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014828 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014829 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014830
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014831 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014832 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014833
Bram Moolenaar4463f292005-09-25 22:20:24 +000014834 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14835 if (xp_name == NULL)
14836 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014837
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014838 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014839
Bram Moolenaar4463f292005-09-25 22:20:24 +000014840 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14841 &xp_arg) == FAIL)
14842 return;
14843 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014844 }
14845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014847 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014848 int save_ex_normal_busy = ex_normal_busy;
14849 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014851 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14852 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014853 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014854 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014855 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014856 && argvars[1].v_type != VAR_UNKNOWN
14857 && argvars[2].v_type != VAR_UNKNOWN)
14858 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14859 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014860
14861 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014863 /* since the user typed this, no need to wait for return */
14864 need_wait_return = FALSE;
14865 msg_didout = FALSE;
14866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867 cmd_silent = cmd_silent_save;
14868}
14869
14870/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014871 * "input()" function
14872 * Also handles inputsecret() when inputsecret is set.
14873 */
14874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014875f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014876{
14877 get_user_input(argvars, rettv, FALSE);
14878}
14879
14880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 * "inputdialog()" function
14882 */
14883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014884f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885{
14886#if defined(FEAT_GUI_TEXTDIALOG)
14887 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14888 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14889 {
14890 char_u *message;
14891 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014892 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014894 message = get_tv_string_chk(&argvars[0]);
14895 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014896 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014897 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 else
14899 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014900 if (message != NULL && defstr != NULL
14901 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014902 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 else
14905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 if (message != NULL && defstr != NULL
14907 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 rettv->vval.v_string = vim_strsave(
14910 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 }
14916 else
14917#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014918 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919}
14920
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014921/*
14922 * "inputlist()" function
14923 */
14924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014925f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014926{
14927 listitem_T *li;
14928 int selected;
14929 int mouse_used;
14930
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014931#ifdef NO_CONSOLE_INPUT
14932 /* While starting up, there is no place to enter text. */
14933 if (no_console_input())
14934 return;
14935#endif
14936 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14937 {
14938 EMSG2(_(e_listarg), "inputlist()");
14939 return;
14940 }
14941
14942 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014943 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014944 lines_left = Rows; /* avoid more prompt */
14945 msg_scroll = TRUE;
14946 msg_clr_eos();
14947
14948 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14949 {
14950 msg_puts(get_tv_string(&li->li_tv));
14951 msg_putchar('\n');
14952 }
14953
14954 /* Ask for choice. */
14955 selected = prompt_for_number(&mouse_used);
14956 if (mouse_used)
14957 selected -= lines_left;
14958
14959 rettv->vval.v_number = selected;
14960}
14961
14962
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14964
14965/*
14966 * "inputrestore()" function
14967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014969f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970{
14971 if (ga_userinput.ga_len > 0)
14972 {
14973 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14975 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014976 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 }
14978 else if (p_verbose > 1)
14979 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014980 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 }
14983}
14984
14985/*
14986 * "inputsave()" function
14987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014989f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014991 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 if (ga_grow(&ga_userinput, 1) == OK)
14993 {
14994 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14995 + ga_userinput.ga_len);
14996 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014997 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 }
14999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001}
15002
15003/*
15004 * "inputsecret()" function
15005 */
15006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015007f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008{
15009 ++cmdline_star;
15010 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012 --cmdline_star;
15013 --inputsecret_flag;
15014}
15015
15016/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015017 * "insert()" function
15018 */
15019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015020f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015021{
15022 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 listitem_T *item;
15024 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015025 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015026
15027 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015028 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015029 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015030 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015031 {
15032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015033 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 if (error)
15035 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015036
Bram Moolenaar758711c2005-02-02 23:11:38 +000015037 if (before == l->lv_len)
15038 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015039 else
15040 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015041 item = list_find(l, before);
15042 if (item == NULL)
15043 {
15044 EMSGN(_(e_listidx), before);
15045 l = NULL;
15046 }
15047 }
15048 if (l != NULL)
15049 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015050 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015051 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015052 }
15053 }
15054}
15055
15056/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015057 * "invert(expr)" function
15058 */
15059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015061{
15062 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15063}
15064
15065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 * "isdirectory()" function
15067 */
15068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015074/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015075 * "islocked()" function
15076 */
15077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015078f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015079{
15080 lval_T lv;
15081 char_u *end;
15082 dictitem_T *di;
15083
15084 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015085 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15086 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015087 if (end != NULL && lv.ll_name != NULL)
15088 {
15089 if (*end != NUL)
15090 EMSG(_(e_trailing));
15091 else
15092 {
15093 if (lv.ll_tv == NULL)
15094 {
15095 if (check_changedtick(lv.ll_name))
15096 rettv->vval.v_number = 1; /* always locked */
15097 else
15098 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015099 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015100 if (di != NULL)
15101 {
15102 /* Consider a variable locked when:
15103 * 1. the variable itself is locked
15104 * 2. the value of the variable is locked.
15105 * 3. the List or Dict value is locked.
15106 */
15107 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15108 || tv_islocked(&di->di_tv));
15109 }
15110 }
15111 }
15112 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015113 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015114 else if (lv.ll_newkey != NULL)
15115 EMSG2(_(e_dictkey), lv.ll_newkey);
15116 else if (lv.ll_list != NULL)
15117 /* List item. */
15118 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15119 else
15120 /* Dictionary item. */
15121 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15122 }
15123 }
15124
15125 clear_lval(&lv);
15126}
15127
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015128#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15129/*
15130 * "isnan()" function
15131 */
15132 static void
15133f_isnan(typval_T *argvars, typval_T *rettv)
15134{
15135 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15136 && isnan(argvars[0].vval.v_float);
15137}
15138#endif
15139
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015140static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015141
15142/*
15143 * Turn a dict into a list:
15144 * "what" == 0: list of keys
15145 * "what" == 1: list of values
15146 * "what" == 2: list of items
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015150{
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 list_T *l2;
15152 dictitem_T *di;
15153 hashitem_T *hi;
15154 listitem_T *li;
15155 listitem_T *li2;
15156 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015157 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015158
Bram Moolenaar8c711452005-01-14 21:53:12 +000015159 if (argvars[0].v_type != VAR_DICT)
15160 {
15161 EMSG(_(e_dictreq));
15162 return;
15163 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015164 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015165 return;
15166
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015167 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015168 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015169
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015170 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015171 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015172 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015173 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015174 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015175 --todo;
15176 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015177
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015178 li = listitem_alloc();
15179 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015180 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015181 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015182
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015183 if (what == 0)
15184 {
15185 /* keys() */
15186 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015187 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015188 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15189 }
15190 else if (what == 1)
15191 {
15192 /* values() */
15193 copy_tv(&di->di_tv, &li->li_tv);
15194 }
15195 else
15196 {
15197 /* items() */
15198 l2 = list_alloc();
15199 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015200 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015201 li->li_tv.vval.v_list = l2;
15202 if (l2 == NULL)
15203 break;
15204 ++l2->lv_refcount;
15205
15206 li2 = listitem_alloc();
15207 if (li2 == NULL)
15208 break;
15209 list_append(l2, li2);
15210 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015211 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015212 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15213
15214 li2 = listitem_alloc();
15215 if (li2 == NULL)
15216 break;
15217 list_append(l2, li2);
15218 copy_tv(&di->di_tv, &li2->li_tv);
15219 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015220 }
15221 }
15222}
15223
15224/*
15225 * "items(dict)" function
15226 */
15227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015228f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015229{
15230 dict_list(argvars, rettv, 2);
15231}
15232
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015233#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015234/*
15235 * Get the job from the argument.
15236 * Returns NULL if the job is invalid.
15237 */
15238 static job_T *
15239get_job_arg(typval_T *tv)
15240{
15241 job_T *job;
15242
15243 if (tv->v_type != VAR_JOB)
15244 {
15245 EMSG2(_(e_invarg2), get_tv_string(tv));
15246 return NULL;
15247 }
15248 job = tv->vval.v_job;
15249
15250 if (job == NULL)
15251 EMSG(_("E916: not a valid job"));
15252 return job;
15253}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015254
Bram Moolenaar835dc632016-02-07 14:27:38 +010015255/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015256 * "job_getchannel()" function
15257 */
15258 static void
15259f_job_getchannel(typval_T *argvars, typval_T *rettv)
15260{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015261 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015262
Bram Moolenaar65edff82016-02-21 16:40:11 +010015263 if (job != NULL)
15264 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015265 rettv->v_type = VAR_CHANNEL;
15266 rettv->vval.v_channel = job->jv_channel;
15267 if (job->jv_channel != NULL)
15268 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015269 }
15270}
15271
15272/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015273 * "job_info()" function
15274 */
15275 static void
15276f_job_info(typval_T *argvars, typval_T *rettv)
15277{
15278 job_T *job = get_job_arg(&argvars[0]);
15279
15280 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15281 job_info(job, rettv->vval.v_dict);
15282}
15283
15284/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015285 * "job_setoptions()" function
15286 */
15287 static void
15288f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15289{
15290 job_T *job = get_job_arg(&argvars[0]);
15291 jobopt_T opt;
15292
15293 if (job == NULL)
15294 return;
15295 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015296 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15297 job_set_options(job, &opt);
15298 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015299}
15300
15301/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015302 * "job_start()" function
15303 */
15304 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015305f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015306{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015307 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015308 if (check_restricted() || check_secure())
15309 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015310 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015311}
15312
15313/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015314 * "job_status()" function
15315 */
15316 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015317f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015318{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015319 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015320
Bram Moolenaar65edff82016-02-21 16:40:11 +010015321 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015322 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015323 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015324 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015325 }
15326}
15327
15328/*
15329 * "job_stop()" function
15330 */
15331 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015332f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015333{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015334 job_T *job = get_job_arg(&argvars[0]);
15335
15336 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015337 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015338}
15339#endif
15340
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015342 * "join()" function
15343 */
15344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015345f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015346{
15347 garray_T ga;
15348 char_u *sep;
15349
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015350 if (argvars[0].v_type != VAR_LIST)
15351 {
15352 EMSG(_(e_listreq));
15353 return;
15354 }
15355 if (argvars[0].vval.v_list == NULL)
15356 return;
15357 if (argvars[1].v_type == VAR_UNKNOWN)
15358 sep = (char_u *)" ";
15359 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015361
15362 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015363
15364 if (sep != NULL)
15365 {
15366 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015367 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015368 ga_append(&ga, NUL);
15369 rettv->vval.v_string = (char_u *)ga.ga_data;
15370 }
15371 else
15372 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015373}
15374
15375/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015376 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015377 */
15378 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015379f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015380{
15381 js_read_T reader;
15382
15383 reader.js_buf = get_tv_string(&argvars[0]);
15384 reader.js_fill = NULL;
15385 reader.js_used = 0;
15386 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15387 EMSG(_(e_invarg));
15388}
15389
15390/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015391 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015392 */
15393 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015394f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015395{
15396 rettv->v_type = VAR_STRING;
15397 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15398}
15399
15400/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015401 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015402 */
15403 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015404f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015405{
15406 js_read_T reader;
15407
15408 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015409 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015410 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015411 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015412 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015413}
15414
15415/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015416 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015417 */
15418 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015419f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015420{
15421 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015422 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015423}
15424
15425/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015426 * "keys()" function
15427 */
15428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015429f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015430{
15431 dict_list(argvars, rettv, 0);
15432}
15433
15434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 * "last_buffer_nr()" function.
15436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015438f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439{
15440 int n = 0;
15441 buf_T *buf;
15442
15443 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15444 if (n < buf->b_fnum)
15445 n = buf->b_fnum;
15446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448}
15449
15450/*
15451 * "len()" function
15452 */
15453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015454f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015455{
15456 switch (argvars[0].v_type)
15457 {
15458 case VAR_STRING:
15459 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015460 rettv->vval.v_number = (varnumber_T)STRLEN(
15461 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 break;
15463 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015465 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015466 case VAR_DICT:
15467 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15468 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015469 case VAR_UNKNOWN:
15470 case VAR_SPECIAL:
15471 case VAR_FLOAT:
15472 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015473 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015474 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015475 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015476 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015477 break;
15478 }
15479}
15480
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015481static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015482
15483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015484libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485{
15486#ifdef FEAT_LIBCALL
15487 char_u *string_in;
15488 char_u **string_result;
15489 int nr_result;
15490#endif
15491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015493 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015495
15496 if (check_restricted() || check_secure())
15497 return;
15498
15499#ifdef FEAT_LIBCALL
15500 /* The first two args must be strings, otherwise its meaningless */
15501 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15502 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015503 string_in = NULL;
15504 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015505 string_in = argvars[2].vval.v_string;
15506 if (type == VAR_NUMBER)
15507 string_result = NULL;
15508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015510 if (mch_libcall(argvars[0].vval.v_string,
15511 argvars[1].vval.v_string,
15512 string_in,
15513 argvars[2].vval.v_number,
15514 string_result,
15515 &nr_result) == OK
15516 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015517 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015518 }
15519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520}
15521
15522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 * "libcall()" function
15524 */
15525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527{
15528 libcall_common(argvars, rettv, VAR_STRING);
15529}
15530
15531/*
15532 * "libcallnr()" function
15533 */
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015536{
15537 libcall_common(argvars, rettv, VAR_NUMBER);
15538}
15539
15540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 * "line(string)" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
15546 linenr_T lnum = 0;
15547 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015548 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015550 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 if (fp != NULL)
15552 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554}
15555
15556/*
15557 * "line2byte(lnum)" function
15558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561{
15562#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564#else
15565 linenr_T lnum;
15566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15572 if (rettv->vval.v_number >= 0)
15573 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574#endif
15575}
15576
15577/*
15578 * "lispindent(lnum)" function
15579 */
15580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015581f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582{
15583#ifdef FEAT_LISP
15584 pos_T pos;
15585 linenr_T lnum;
15586
15587 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015588 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15590 {
15591 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 curwin->w_cursor = pos;
15594 }
15595 else
15596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598}
15599
15600/*
15601 * "localtime()" function
15602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015604f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015609static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
15614 char_u *keys;
15615 char_u *which;
15616 char_u buf[NUMBUFLEN];
15617 char_u *keys_buf = NULL;
15618 char_u *rhs;
15619 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015620 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015621 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015622 mapblock_T *mp;
15623 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624
15625 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626 rettv->v_type = VAR_STRING;
15627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 if (*keys == NUL)
15631 return;
15632
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015633 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015637 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015638 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015639 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015640 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015641 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 else
15644 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015645 if (which == NULL)
15646 return;
15647
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 mode = get_map_mode(&which, 0);
15649
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015650 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015651 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015653
15654 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015656 /* Return a string. */
15657 if (rhs != NULL)
15658 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659
Bram Moolenaarbd743252010-10-20 21:23:33 +020015660 }
15661 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15662 {
15663 /* Return a dictionary. */
15664 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15665 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15666 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
Bram Moolenaarbd743252010-10-20 21:23:33 +020015668 dict_add_nr_str(dict, "lhs", 0L, lhs);
15669 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15670 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15671 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15672 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15673 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15674 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015675 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015676 dict_add_nr_str(dict, "mode", 0L, mapmode);
15677
15678 vim_free(lhs);
15679 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 }
15681}
15682
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015683#ifdef FEAT_FLOAT
15684/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015685 * "log()" function
15686 */
15687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015688f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015689{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015690 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015691
15692 rettv->v_type = VAR_FLOAT;
15693 if (get_float_arg(argvars, &f) == OK)
15694 rettv->vval.v_float = log(f);
15695 else
15696 rettv->vval.v_float = 0.0;
15697}
15698
15699/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015700 * "log10()" function
15701 */
15702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015703f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015704{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015705 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015706
15707 rettv->v_type = VAR_FLOAT;
15708 if (get_float_arg(argvars, &f) == OK)
15709 rettv->vval.v_float = log10(f);
15710 else
15711 rettv->vval.v_float = 0.0;
15712}
15713#endif
15714
Bram Moolenaar1dced572012-04-05 16:54:08 +020015715#ifdef FEAT_LUA
15716/*
15717 * "luaeval()" function
15718 */
15719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015720f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015721{
15722 char_u *str;
15723 char_u buf[NUMBUFLEN];
15724
15725 str = get_tv_string_buf(&argvars[0], buf);
15726 do_luaeval(str, argvars + 1, rettv);
15727}
15728#endif
15729
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015731 * "map()" function
15732 */
15733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015734f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015735{
15736 filter_map(argvars, rettv, TRUE);
15737}
15738
15739/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015740 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 */
15742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015743f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746}
15747
15748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 */
15751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755}
15756
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015757static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758
15759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015760find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015762 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015763 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 char_u *pat;
15766 regmatch_T regmatch;
15767 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015768 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 char_u *save_cpo;
15770 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015771 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015772 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015773 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 list_T *l = NULL;
15775 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015776 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778
15779 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15780 save_cpo = p_cpo;
15781 p_cpo = (char_u *)"";
15782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015784 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015785 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015786 /* type 3: return empty list when there are no matches.
15787 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015788 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015789 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015790 if (type == 4
15791 && (list_append_string(rettv->vval.v_list,
15792 (char_u *)"", 0) == FAIL
15793 || list_append_number(rettv->vval.v_list,
15794 (varnumber_T)-1) == FAIL
15795 || list_append_number(rettv->vval.v_list,
15796 (varnumber_T)-1) == FAIL
15797 || list_append_number(rettv->vval.v_list,
15798 (varnumber_T)-1) == FAIL))
15799 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015800 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015801 rettv->vval.v_list = NULL;
15802 goto theend;
15803 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015804 }
15805 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807 rettv->v_type = VAR_STRING;
15808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015811 if (argvars[0].v_type == VAR_LIST)
15812 {
15813 if ((l = argvars[0].vval.v_list) == NULL)
15814 goto theend;
15815 li = l->lv_first;
15816 }
15817 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015818 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015819 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015820 len = (long)STRLEN(str);
15821 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15824 if (pat == NULL)
15825 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 int error = FALSE;
15830
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015831 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015832 if (error)
15833 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 if (l != NULL)
15835 {
15836 li = list_find(l, start);
15837 if (li == NULL)
15838 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015839 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 }
15841 else
15842 {
15843 if (start < 0)
15844 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015845 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015846 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015847 /* When "count" argument is there ignore matches before "start",
15848 * otherwise skip part of the string. Differs when pattern is "^"
15849 * or "\<". */
15850 if (argvars[3].v_type != VAR_UNKNOWN)
15851 startcol = start;
15852 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015853 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015854 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015855 len -= start;
15856 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015857 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015859 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015860 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015861 if (error)
15862 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863 }
15864
15865 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15866 if (regmatch.regprog != NULL)
15867 {
15868 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015869
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015870 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015871 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015872 if (l != NULL)
15873 {
15874 if (li == NULL)
15875 {
15876 match = FALSE;
15877 break;
15878 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015879 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015880 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015881 if (str == NULL)
15882 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015883 }
15884
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015885 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015886
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015887 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015888 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015889 if (l == NULL && !match)
15890 break;
15891
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015892 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015893 if (l != NULL)
15894 {
15895 li = li->li_next;
15896 ++idx;
15897 }
15898 else
15899 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015900#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015901 startcol = (colnr_T)(regmatch.startp[0]
15902 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015903#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015904 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015905#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015906 if (startcol > (colnr_T)len
15907 || str + startcol <= regmatch.startp[0])
15908 {
15909 match = FALSE;
15910 break;
15911 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015912 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015913 }
15914
15915 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015917 if (type == 4)
15918 {
15919 listitem_T *li1 = rettv->vval.v_list->lv_first;
15920 listitem_T *li2 = li1->li_next;
15921 listitem_T *li3 = li2->li_next;
15922 listitem_T *li4 = li3->li_next;
15923
Bram Moolenaar3c809342016-06-01 22:34:48 +020015924 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015925 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15926 (int)(regmatch.endp[0] - regmatch.startp[0]));
15927 li3->li_tv.vval.v_number =
15928 (varnumber_T)(regmatch.startp[0] - expr);
15929 li4->li_tv.vval.v_number =
15930 (varnumber_T)(regmatch.endp[0] - expr);
15931 if (l != NULL)
15932 li2->li_tv.vval.v_number = (varnumber_T)idx;
15933 }
15934 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015936 int i;
15937
15938 /* return list with matched string and submatches */
15939 for (i = 0; i < NSUBEXP; ++i)
15940 {
15941 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015942 {
15943 if (list_append_string(rettv->vval.v_list,
15944 (char_u *)"", 0) == FAIL)
15945 break;
15946 }
15947 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015948 regmatch.startp[i],
15949 (int)(regmatch.endp[i] - regmatch.startp[i]))
15950 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015952 }
15953 }
15954 else if (type == 2)
15955 {
15956 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015957 if (l != NULL)
15958 copy_tv(&li->li_tv, rettv);
15959 else
15960 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015962 }
15963 else if (l != NULL)
15964 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 else
15966 {
15967 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 (varnumber_T)(regmatch.startp[0] - str);
15970 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015971 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015973 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 }
15975 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015976 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 }
15978
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015979 if (type == 4 && l == NULL)
15980 /* matchstrpos() without a list: drop the second item. */
15981 listitem_remove(rettv->vval.v_list,
15982 rettv->vval.v_list->lv_first->li_next);
15983
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015985 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 p_cpo = save_cpo;
15987}
15988
15989/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015990 * "match()" function
15991 */
15992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015993f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015994{
15995 find_some_match(argvars, rettv, 1);
15996}
15997
15998/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015999 * "matchadd()" function
16000 */
16001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016002f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016003{
16004#ifdef FEAT_SEARCH_EXTRA
16005 char_u buf[NUMBUFLEN];
16006 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16007 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16008 int prio = 10; /* default priority */
16009 int id = -1;
16010 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016011 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016012
16013 rettv->vval.v_number = -1;
16014
16015 if (grp == NULL || pat == NULL)
16016 return;
16017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016018 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016019 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016020 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016021 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016022 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016023 if (argvars[4].v_type != VAR_UNKNOWN)
16024 {
16025 if (argvars[4].v_type != VAR_DICT)
16026 {
16027 EMSG(_(e_dictreq));
16028 return;
16029 }
16030 if (dict_find(argvars[4].vval.v_dict,
16031 (char_u *)"conceal", -1) != NULL)
16032 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16033 (char_u *)"conceal", FALSE);
16034 }
16035 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016036 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016037 if (error == TRUE)
16038 return;
16039 if (id >= 1 && id <= 3)
16040 {
16041 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16042 return;
16043 }
16044
Bram Moolenaar6561d522015-07-21 15:48:27 +020016045 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16046 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016047#endif
16048}
16049
16050/*
16051 * "matchaddpos()" function
16052 */
16053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016054f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016055{
16056#ifdef FEAT_SEARCH_EXTRA
16057 char_u buf[NUMBUFLEN];
16058 char_u *group;
16059 int prio = 10;
16060 int id = -1;
16061 int error = FALSE;
16062 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016063 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016064
16065 rettv->vval.v_number = -1;
16066
16067 group = get_tv_string_buf_chk(&argvars[0], buf);
16068 if (group == NULL)
16069 return;
16070
16071 if (argvars[1].v_type != VAR_LIST)
16072 {
16073 EMSG2(_(e_listarg), "matchaddpos()");
16074 return;
16075 }
16076 l = argvars[1].vval.v_list;
16077 if (l == NULL)
16078 return;
16079
16080 if (argvars[2].v_type != VAR_UNKNOWN)
16081 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016082 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016083 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016084 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016085 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016086 if (argvars[4].v_type != VAR_UNKNOWN)
16087 {
16088 if (argvars[4].v_type != VAR_DICT)
16089 {
16090 EMSG(_(e_dictreq));
16091 return;
16092 }
16093 if (dict_find(argvars[4].vval.v_dict,
16094 (char_u *)"conceal", -1) != NULL)
16095 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16096 (char_u *)"conceal", FALSE);
16097 }
16098 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016099 }
16100 if (error == TRUE)
16101 return;
16102
16103 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16104 if (id == 1 || id == 2)
16105 {
16106 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16107 return;
16108 }
16109
Bram Moolenaar6561d522015-07-21 15:48:27 +020016110 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16111 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016112#endif
16113}
16114
16115/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016116 * "matcharg()" function
16117 */
16118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016119f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016120{
16121 if (rettv_list_alloc(rettv) == OK)
16122 {
16123#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016124 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016125 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016126
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016127 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016128 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016129 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16130 {
16131 list_append_string(rettv->vval.v_list,
16132 syn_id2name(m->hlg_id), -1);
16133 list_append_string(rettv->vval.v_list, m->pattern, -1);
16134 }
16135 else
16136 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016137 list_append_string(rettv->vval.v_list, NULL, -1);
16138 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016139 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016140 }
16141#endif
16142 }
16143}
16144
16145/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016146 * "matchdelete()" function
16147 */
16148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016149f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016150{
16151#ifdef FEAT_SEARCH_EXTRA
16152 rettv->vval.v_number = match_delete(curwin,
16153 (int)get_tv_number(&argvars[0]), TRUE);
16154#endif
16155}
16156
16157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 * "matchend()" function
16159 */
16160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016161f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162{
16163 find_some_match(argvars, rettv, 0);
16164}
16165
16166/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016167 * "matchlist()" function
16168 */
16169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016170f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016171{
16172 find_some_match(argvars, rettv, 3);
16173}
16174
16175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016176 * "matchstr()" function
16177 */
16178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016179f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180{
16181 find_some_match(argvars, rettv, 2);
16182}
16183
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016184/*
16185 * "matchstrpos()" function
16186 */
16187 static void
16188f_matchstrpos(typval_T *argvars, typval_T *rettv)
16189{
16190 find_some_match(argvars, rettv, 4);
16191}
16192
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016193static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016194
16195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016196max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016197{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016198 varnumber_T n = 0;
16199 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016201
16202 if (argvars[0].v_type == VAR_LIST)
16203 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 list_T *l;
16205 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016206
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016207 l = argvars[0].vval.v_list;
16208 if (l != NULL)
16209 {
16210 li = l->lv_first;
16211 if (li != NULL)
16212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016213 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016214 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016215 {
16216 li = li->li_next;
16217 if (li == NULL)
16218 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016219 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016220 if (domax ? i > n : i < n)
16221 n = i;
16222 }
16223 }
16224 }
16225 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016226 else if (argvars[0].v_type == VAR_DICT)
16227 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016228 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016229 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016231 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016232
16233 d = argvars[0].vval.v_dict;
16234 if (d != NULL)
16235 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016236 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016239 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016241 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016242 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016243 if (first)
16244 {
16245 n = i;
16246 first = FALSE;
16247 }
16248 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016249 n = i;
16250 }
16251 }
16252 }
16253 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016254 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016255 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016257}
16258
16259/*
16260 * "max()" function
16261 */
16262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016263f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016264{
16265 max_min(argvars, rettv, TRUE);
16266}
16267
16268/*
16269 * "min()" function
16270 */
16271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016272f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016273{
16274 max_min(argvars, rettv, FALSE);
16275}
16276
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016277static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016278
16279/*
16280 * Create the directory in which "dir" is located, and higher levels when
16281 * needed.
16282 */
16283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016284mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016285{
16286 char_u *p;
16287 char_u *updir;
16288 int r = FAIL;
16289
16290 /* Get end of directory name in "dir".
16291 * We're done when it's "/" or "c:/". */
16292 p = gettail_sep(dir);
16293 if (p <= get_past_head(dir))
16294 return OK;
16295
16296 /* If the directory exists we're done. Otherwise: create it.*/
16297 updir = vim_strnsave(dir, (int)(p - dir));
16298 if (updir == NULL)
16299 return FAIL;
16300 if (mch_isdir(updir))
16301 r = OK;
16302 else if (mkdir_recurse(updir, prot) == OK)
16303 r = vim_mkdir_emsg(updir, prot);
16304 vim_free(updir);
16305 return r;
16306}
16307
16308#ifdef vim_mkdir
16309/*
16310 * "mkdir()" function
16311 */
16312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016313f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016314{
16315 char_u *dir;
16316 char_u buf[NUMBUFLEN];
16317 int prot = 0755;
16318
16319 rettv->vval.v_number = FAIL;
16320 if (check_restricted() || check_secure())
16321 return;
16322
16323 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016324 if (*dir == NUL)
16325 rettv->vval.v_number = FAIL;
16326 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016327 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016328 if (*gettail(dir) == NUL)
16329 /* remove trailing slashes */
16330 *gettail_sep(dir) = NUL;
16331
16332 if (argvars[1].v_type != VAR_UNKNOWN)
16333 {
16334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016335 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016336 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16337 mkdir_recurse(dir, prot);
16338 }
16339 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016340 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016341}
16342#endif
16343
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 * "mode()" function
16346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016348f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016350 char_u buf[3];
16351
16352 buf[1] = NUL;
16353 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016355 if (time_for_testing == 93784)
16356 {
16357 /* Testing the two-character code. */
16358 buf[0] = 'x';
16359 buf[1] = '!';
16360 }
16361 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 {
16363 if (VIsual_select)
16364 buf[0] = VIsual_mode + 's' - 'v';
16365 else
16366 buf[0] = VIsual_mode;
16367 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016368 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016369 || State == CONFIRM)
16370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016372 if (State == ASKMORE)
16373 buf[1] = 'm';
16374 else if (State == CONFIRM)
16375 buf[1] = '?';
16376 }
16377 else if (State == EXTERNCMD)
16378 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 else if (State & INSERT)
16380 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016381#ifdef FEAT_VREPLACE
16382 if (State & VREPLACE_FLAG)
16383 {
16384 buf[0] = 'R';
16385 buf[1] = 'v';
16386 }
16387 else
16388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 if (State & REPLACE_FLAG)
16390 buf[0] = 'R';
16391 else
16392 buf[0] = 'i';
16393 }
16394 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016397 if (exmode_active)
16398 buf[1] = 'v';
16399 }
16400 else if (exmode_active)
16401 {
16402 buf[0] = 'c';
16403 buf[1] = 'e';
16404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016408 if (finish_op)
16409 buf[1] = 'o';
16410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016412 /* Clear out the minor mode when the argument is not a non-zero number or
16413 * non-empty string. */
16414 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016415 buf[1] = NUL;
16416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016417 rettv->vval.v_string = vim_strsave(buf);
16418 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419}
16420
Bram Moolenaar429fa852013-04-15 12:27:36 +020016421#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016422/*
16423 * "mzeval()" function
16424 */
16425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016427{
16428 char_u *str;
16429 char_u buf[NUMBUFLEN];
16430
16431 str = get_tv_string_buf(&argvars[0], buf);
16432 do_mzeval(str, rettv);
16433}
Bram Moolenaar75676462013-01-30 14:55:42 +010016434
16435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016436mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016437{
16438 typval_T argvars[3];
16439
16440 argvars[0].v_type = VAR_STRING;
16441 argvars[0].vval.v_string = name;
16442 copy_tv(args, &argvars[1]);
16443 argvars[2].v_type = VAR_UNKNOWN;
16444 f_call(argvars, rettv);
16445 clear_tv(&argvars[1]);
16446}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016447#endif
16448
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 * "nextnonblank()" function
16451 */
16452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016453f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016454{
16455 linenr_T lnum;
16456
16457 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460 {
16461 lnum = 0;
16462 break;
16463 }
16464 if (*skipwhite(ml_get(lnum)) != NUL)
16465 break;
16466 }
16467 rettv->vval.v_number = lnum;
16468}
16469
16470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 * "nr2char()" function
16472 */
16473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475{
16476 char_u buf[NUMBUFLEN];
16477
16478#ifdef FEAT_MBYTE
16479 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016480 {
16481 int utf8 = 0;
16482
16483 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016484 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016485 if (utf8)
16486 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16487 else
16488 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490 else
16491#endif
16492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 buf[1] = NUL;
16495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 rettv->v_type = VAR_STRING;
16497 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016498}
16499
16500/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016501 * "or(expr, expr)" function
16502 */
16503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016504f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016505{
16506 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16507 | get_tv_number_chk(&argvars[1], NULL);
16508}
16509
16510/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016511 * "pathshorten()" function
16512 */
16513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016514f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016515{
16516 char_u *p;
16517
16518 rettv->v_type = VAR_STRING;
16519 p = get_tv_string_chk(&argvars[0]);
16520 if (p == NULL)
16521 rettv->vval.v_string = NULL;
16522 else
16523 {
16524 p = vim_strsave(p);
16525 rettv->vval.v_string = p;
16526 if (p != NULL)
16527 shorten_dir(p);
16528 }
16529}
16530
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016531#ifdef FEAT_PERL
16532/*
16533 * "perleval()" function
16534 */
16535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016536f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016537{
16538 char_u *str;
16539 char_u buf[NUMBUFLEN];
16540
16541 str = get_tv_string_buf(&argvars[0], buf);
16542 do_perleval(str, rettv);
16543}
16544#endif
16545
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016546#ifdef FEAT_FLOAT
16547/*
16548 * "pow()" function
16549 */
16550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016551f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016552{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016553 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016554
16555 rettv->v_type = VAR_FLOAT;
16556 if (get_float_arg(argvars, &fx) == OK
16557 && get_float_arg(&argvars[1], &fy) == OK)
16558 rettv->vval.v_float = pow(fx, fy);
16559 else
16560 rettv->vval.v_float = 0.0;
16561}
16562#endif
16563
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016564/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016565 * "prevnonblank()" function
16566 */
16567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569{
16570 linenr_T lnum;
16571
16572 lnum = get_tv_lnum(argvars);
16573 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16574 lnum = 0;
16575 else
16576 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16577 --lnum;
16578 rettv->vval.v_number = lnum;
16579}
16580
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016581/* This dummy va_list is here because:
16582 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16583 * - locally in the function results in a "used before set" warning
16584 * - using va_start() to initialize it gives "function with fixed args" error */
16585static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016586
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016588 * "printf()" function
16589 */
16590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016591f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016592{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016593 char_u buf[NUMBUFLEN];
16594 int len;
16595 char_u *s;
16596 int saved_did_emsg = did_emsg;
16597 char *fmt;
16598
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016599 rettv->v_type = VAR_STRING;
16600 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016601
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016602 /* Get the required length, allocate the buffer and do it for real. */
16603 did_emsg = FALSE;
16604 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16605 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16606 if (!did_emsg)
16607 {
16608 s = alloc(len + 1);
16609 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016610 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016611 rettv->vval.v_string = s;
16612 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016613 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016614 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016615 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016616}
16617
16618/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619 * "pumvisible()" function
16620 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016622f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016623{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624#ifdef FEAT_INS_EXPAND
16625 if (pum_visible())
16626 rettv->vval.v_number = 1;
16627#endif
16628}
16629
Bram Moolenaardb913952012-06-29 12:54:53 +020016630#ifdef FEAT_PYTHON3
16631/*
16632 * "py3eval()" function
16633 */
16634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016635f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016636{
16637 char_u *str;
16638 char_u buf[NUMBUFLEN];
16639
16640 str = get_tv_string_buf(&argvars[0], buf);
16641 do_py3eval(str, rettv);
16642}
16643#endif
16644
16645#ifdef FEAT_PYTHON
16646/*
16647 * "pyeval()" function
16648 */
16649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016651{
16652 char_u *str;
16653 char_u buf[NUMBUFLEN];
16654
16655 str = get_tv_string_buf(&argvars[0], buf);
16656 do_pyeval(str, rettv);
16657}
16658#endif
16659
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016660/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016661 * "range()" function
16662 */
16663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016664f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016665{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016666 varnumber_T start;
16667 varnumber_T end;
16668 varnumber_T stride = 1;
16669 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016670 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016672 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016673 if (argvars[1].v_type == VAR_UNKNOWN)
16674 {
16675 end = start - 1;
16676 start = 0;
16677 }
16678 else
16679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016682 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016683 }
16684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016685 if (error)
16686 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016687 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016688 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016689 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016690 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016691 else
16692 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016693 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016694 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 if (list_append_number(rettv->vval.v_list,
16696 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016697 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016698 }
16699}
16700
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016701/*
16702 * "readfile()" function
16703 */
16704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016705f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016706{
16707 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709 char_u *fname;
16710 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016711 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16712 int io_size = sizeof(buf);
16713 int readlen; /* size of last fread() */
16714 char_u *prev = NULL; /* previously read bytes, if any */
16715 long prevlen = 0; /* length of data in prev */
16716 long prevsize = 0; /* size of prev buffer */
16717 long maxline = MAXLNUM;
16718 long cnt = 0;
16719 char_u *p; /* position in buf */
16720 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016721
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016722 if (argvars[1].v_type != VAR_UNKNOWN)
16723 {
16724 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16725 binary = TRUE;
16726 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016727 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016730 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016731 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732
16733 /* Always open the file in binary mode, library functions have a mind of
16734 * their own about CR-LF conversion. */
16735 fname = get_tv_string(&argvars[0]);
16736 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16737 {
16738 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16739 return;
16740 }
16741
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016742 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016743 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016744 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016746 /* This for loop processes what was read, but is also entered at end
16747 * of file so that either:
16748 * - an incomplete line gets written
16749 * - a "binary" file gets an empty line at the end if it ends in a
16750 * newline. */
16751 for (p = buf, start = buf;
16752 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16753 ++p)
16754 {
16755 if (*p == '\n' || readlen <= 0)
16756 {
16757 listitem_T *li;
16758 char_u *s = NULL;
16759 long_u len = p - start;
16760
16761 /* Finished a line. Remove CRs before NL. */
16762 if (readlen > 0 && !binary)
16763 {
16764 while (len > 0 && start[len - 1] == '\r')
16765 --len;
16766 /* removal may cross back to the "prev" string */
16767 if (len == 0)
16768 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16769 --prevlen;
16770 }
16771 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016772 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773 else
16774 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016775 /* Change "prev" buffer to be the right size. This way
16776 * the bytes are only copied once, and very long lines are
16777 * allocated only once. */
16778 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016779 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016780 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016781 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016782 prev = NULL; /* the list will own the string */
16783 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016784 }
16785 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016786 if (s == NULL)
16787 {
16788 do_outofmem_msg((long_u) prevlen + len + 1);
16789 failed = TRUE;
16790 break;
16791 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016792
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016793 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794 {
16795 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016796 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016797 break;
16798 }
16799 li->li_tv.v_type = VAR_STRING;
16800 li->li_tv.v_lock = 0;
16801 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016802 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016804 start = p + 1; /* step over newline */
16805 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016806 break;
16807 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016808 else if (*p == NUL)
16809 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016810#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016811 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16812 * when finding the BF and check the previous two bytes. */
16813 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016814 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016815 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16816 * + 1, these may be in the "prev" string. */
16817 char_u back1 = p >= buf + 1 ? p[-1]
16818 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16819 char_u back2 = p >= buf + 2 ? p[-2]
16820 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16821 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016822
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016823 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016824 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016825 char_u *dest = p - 2;
16826
16827 /* Usually a BOM is at the beginning of a file, and so at
16828 * the beginning of a line; then we can just step over it.
16829 */
16830 if (start == dest)
16831 start = p + 1;
16832 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016833 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016834 /* have to shuffle buf to close gap */
16835 int adjust_prevlen = 0;
16836
16837 if (dest < buf)
16838 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016839 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016840 dest = buf;
16841 }
16842 if (readlen > p - buf + 1)
16843 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16844 readlen -= 3 - adjust_prevlen;
16845 prevlen -= adjust_prevlen;
16846 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016847 }
16848 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016849 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850#endif
16851 } /* for */
16852
16853 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16854 break;
16855 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016856 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016857 /* There's part of a line in buf, store it in "prev". */
16858 if (p - start + prevlen >= prevsize)
16859 {
16860 /* need bigger "prev" buffer */
16861 char_u *newprev;
16862
16863 /* A common use case is ordinary text files and "prev" gets a
16864 * fragment of a line, so the first allocation is made
16865 * small, to avoid repeatedly 'allocing' large and
16866 * 'reallocing' small. */
16867 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016868 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016869 else
16870 {
16871 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016872 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016873 prevsize = grow50pc > growmin ? grow50pc : growmin;
16874 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016875 newprev = prev == NULL ? alloc(prevsize)
16876 : vim_realloc(prev, prevsize);
16877 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016878 {
16879 do_outofmem_msg((long_u)prevsize);
16880 failed = TRUE;
16881 break;
16882 }
16883 prev = newprev;
16884 }
16885 /* Add the line part to end of "prev". */
16886 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016887 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016888 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016889 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016890
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016891 /*
16892 * For a negative line count use only the lines at the end of the file,
16893 * free the rest.
16894 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016895 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016896 while (cnt > -maxline)
16897 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016898 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016899 --cnt;
16900 }
16901
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016902 if (failed)
16903 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016904 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016905 /* readfile doc says an empty list is returned on error */
16906 rettv->vval.v_list = list_alloc();
16907 }
16908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016909 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016910 fclose(fd);
16911}
16912
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016913#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016914static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016915
16916/*
16917 * Convert a List to proftime_T.
16918 * Return FAIL when there is something wrong.
16919 */
16920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016921list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016922{
16923 long n1, n2;
16924 int error = FALSE;
16925
16926 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16927 || arg->vval.v_list->lv_len != 2)
16928 return FAIL;
16929 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16930 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16931# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016932 tm->HighPart = n1;
16933 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016934# else
16935 tm->tv_sec = n1;
16936 tm->tv_usec = n2;
16937# endif
16938 return error ? FAIL : OK;
16939}
16940#endif /* FEAT_RELTIME */
16941
16942/*
16943 * "reltime()" function
16944 */
16945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016947{
16948#ifdef FEAT_RELTIME
16949 proftime_T res;
16950 proftime_T start;
16951
16952 if (argvars[0].v_type == VAR_UNKNOWN)
16953 {
16954 /* No arguments: get current time. */
16955 profile_start(&res);
16956 }
16957 else if (argvars[1].v_type == VAR_UNKNOWN)
16958 {
16959 if (list2proftime(&argvars[0], &res) == FAIL)
16960 return;
16961 profile_end(&res);
16962 }
16963 else
16964 {
16965 /* Two arguments: compute the difference. */
16966 if (list2proftime(&argvars[0], &start) == FAIL
16967 || list2proftime(&argvars[1], &res) == FAIL)
16968 return;
16969 profile_sub(&res, &start);
16970 }
16971
16972 if (rettv_list_alloc(rettv) == OK)
16973 {
16974 long n1, n2;
16975
16976# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016977 n1 = res.HighPart;
16978 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016979# else
16980 n1 = res.tv_sec;
16981 n2 = res.tv_usec;
16982# endif
16983 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16984 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16985 }
16986#endif
16987}
16988
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016989#ifdef FEAT_FLOAT
16990/*
16991 * "reltimefloat()" function
16992 */
16993 static void
16994f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16995{
16996# ifdef FEAT_RELTIME
16997 proftime_T tm;
16998# endif
16999
17000 rettv->v_type = VAR_FLOAT;
17001 rettv->vval.v_float = 0;
17002# ifdef FEAT_RELTIME
17003 if (list2proftime(&argvars[0], &tm) == OK)
17004 rettv->vval.v_float = profile_float(&tm);
17005# endif
17006}
17007#endif
17008
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017009/*
17010 * "reltimestr()" function
17011 */
17012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017013f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017014{
17015#ifdef FEAT_RELTIME
17016 proftime_T tm;
17017#endif
17018
17019 rettv->v_type = VAR_STRING;
17020 rettv->vval.v_string = NULL;
17021#ifdef FEAT_RELTIME
17022 if (list2proftime(&argvars[0], &tm) == OK)
17023 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17024#endif
17025}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017026
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017028static void make_connection(void);
17029static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030
17031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017032make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017033{
17034 if (X_DISPLAY == NULL
17035# ifdef FEAT_GUI
17036 && !gui.in_use
17037# endif
17038 )
17039 {
17040 x_force_connect = TRUE;
17041 setup_term_clip();
17042 x_force_connect = FALSE;
17043 }
17044}
17045
17046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017047check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048{
17049 make_connection();
17050 if (X_DISPLAY == NULL)
17051 {
17052 EMSG(_("E240: No connection to Vim server"));
17053 return FAIL;
17054 }
17055 return OK;
17056}
17057#endif
17058
17059#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062{
17063 char_u *server_name;
17064 char_u *keys;
17065 char_u *r = NULL;
17066 char_u buf[NUMBUFLEN];
17067# ifdef WIN32
17068 HWND w;
17069# else
17070 Window w;
17071# endif
17072
17073 if (check_restricted() || check_secure())
17074 return;
17075
17076# ifdef FEAT_X11
17077 if (check_connection() == FAIL)
17078 return;
17079# endif
17080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017081 server_name = get_tv_string_chk(&argvars[0]);
17082 if (server_name == NULL)
17083 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084 keys = get_tv_string_buf(&argvars[1], buf);
17085# ifdef WIN32
17086 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17087# else
17088 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17089 < 0)
17090# endif
17091 {
17092 if (r != NULL)
17093 EMSG(r); /* sending worked but evaluation failed */
17094 else
17095 EMSG2(_("E241: Unable to send to %s"), server_name);
17096 return;
17097 }
17098
17099 rettv->vval.v_string = r;
17100
17101 if (argvars[2].v_type != VAR_UNKNOWN)
17102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017103 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017104 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017105 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017107 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 v.di_tv.v_type = VAR_STRING;
17109 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 idvar = get_tv_string_chk(&argvars[2]);
17111 if (idvar != NULL)
17112 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114 }
17115}
17116#endif
17117
17118/*
17119 * "remote_expr()" function
17120 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017122f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123{
17124 rettv->v_type = VAR_STRING;
17125 rettv->vval.v_string = NULL;
17126#ifdef FEAT_CLIENTSERVER
17127 remote_common(argvars, rettv, TRUE);
17128#endif
17129}
17130
17131/*
17132 * "remote_foreground()" function
17133 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017135f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017136{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137#ifdef FEAT_CLIENTSERVER
17138# ifdef WIN32
17139 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017140 {
17141 char_u *server_name = get_tv_string_chk(&argvars[0]);
17142
17143 if (server_name != NULL)
17144 serverForeground(server_name);
17145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146# else
17147 /* Send a foreground() expression to the server. */
17148 argvars[1].v_type = VAR_STRING;
17149 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17150 argvars[2].v_type = VAR_UNKNOWN;
17151 remote_common(argvars, rettv, TRUE);
17152 vim_free(argvars[1].vval.v_string);
17153# endif
17154#endif
17155}
17156
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159{
17160#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017161 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 char_u *s = NULL;
17163# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017164 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167
17168 if (check_restricted() || check_secure())
17169 {
17170 rettv->vval.v_number = -1;
17171 return;
17172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 serverid = get_tv_string_chk(&argvars[0]);
17174 if (serverid == NULL)
17175 {
17176 rettv->vval.v_number = -1;
17177 return; /* type error; errmsg already given */
17178 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017180 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 if (n == 0)
17182 rettv->vval.v_number = -1;
17183 else
17184 {
17185 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17186 rettv->vval.v_number = (s != NULL);
17187 }
17188# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 if (check_connection() == FAIL)
17190 return;
17191
17192 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017194# endif
17195
17196 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 char_u *retvar;
17199
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 v.di_tv.v_type = VAR_STRING;
17201 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017202 retvar = get_tv_string_chk(&argvars[1]);
17203 if (retvar != NULL)
17204 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 }
17207#else
17208 rettv->vval.v_number = -1;
17209#endif
17210}
17211
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017213f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017214{
17215 char_u *r = NULL;
17216
17217#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017218 char_u *serverid = get_tv_string_chk(&argvars[0]);
17219
17220 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 {
17222# ifdef WIN32
17223 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017224 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017226 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 if (n != 0)
17228 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17229 if (r == NULL)
17230# else
17231 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233# endif
17234 EMSG(_("E277: Unable to read a server reply"));
17235 }
17236#endif
17237 rettv->v_type = VAR_STRING;
17238 rettv->vval.v_string = r;
17239}
17240
17241/*
17242 * "remote_send()" function
17243 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017245f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017246{
17247 rettv->v_type = VAR_STRING;
17248 rettv->vval.v_string = NULL;
17249#ifdef FEAT_CLIENTSERVER
17250 remote_common(argvars, rettv, FALSE);
17251#endif
17252}
17253
17254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017255 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017256 */
17257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017258f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017259{
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 list_T *l;
17261 listitem_T *item, *item2;
17262 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017263 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017264 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017265 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 dict_T *d;
17267 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017268 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017269
Bram Moolenaar8c711452005-01-14 21:53:12 +000017270 if (argvars[0].v_type == VAR_DICT)
17271 {
17272 if (argvars[2].v_type != VAR_UNKNOWN)
17273 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017274 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017275 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017277 key = get_tv_string_chk(&argvars[1]);
17278 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 di = dict_find(d, key, -1);
17281 if (di == NULL)
17282 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017283 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17284 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017285 {
17286 *rettv = di->di_tv;
17287 init_tv(&di->di_tv);
17288 dictitem_remove(d, di);
17289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017291 }
17292 }
17293 else if (argvars[0].v_type != VAR_LIST)
17294 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017295 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017296 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017298 int error = FALSE;
17299
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017300 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017301 if (error)
17302 ; /* type error: do nothing, errmsg already given */
17303 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017304 EMSGN(_(e_listidx), idx);
17305 else
17306 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017307 if (argvars[2].v_type == VAR_UNKNOWN)
17308 {
17309 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017310 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017311 *rettv = item->li_tv;
17312 vim_free(item);
17313 }
17314 else
17315 {
17316 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017317 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017318 if (error)
17319 ; /* type error: do nothing */
17320 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017321 EMSGN(_(e_listidx), end);
17322 else
17323 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017324 int cnt = 0;
17325
17326 for (li = item; li != NULL; li = li->li_next)
17327 {
17328 ++cnt;
17329 if (li == item2)
17330 break;
17331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017332 if (li == NULL) /* didn't find "item2" after "item" */
17333 EMSG(_(e_invrange));
17334 else
17335 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017336 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017337 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017339 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017340 l->lv_first = item;
17341 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017342 item->li_prev = NULL;
17343 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017344 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017345 }
17346 }
17347 }
17348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017349 }
17350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351}
17352
17353/*
17354 * "rename({from}, {to})" function
17355 */
17356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017357f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358{
17359 char_u buf[NUMBUFLEN];
17360
17361 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017364 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17365 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366}
17367
17368/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017369 * "repeat()" function
17370 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017372f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017373{
17374 char_u *p;
17375 int n;
17376 int slen;
17377 int len;
17378 char_u *r;
17379 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017380
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017381 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017382 if (argvars[0].v_type == VAR_LIST)
17383 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017384 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017385 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017386 if (list_extend(rettv->vval.v_list,
17387 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017388 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017389 }
17390 else
17391 {
17392 p = get_tv_string(&argvars[0]);
17393 rettv->v_type = VAR_STRING;
17394 rettv->vval.v_string = NULL;
17395
17396 slen = (int)STRLEN(p);
17397 len = slen * n;
17398 if (len <= 0)
17399 return;
17400
17401 r = alloc(len + 1);
17402 if (r != NULL)
17403 {
17404 for (i = 0; i < n; i++)
17405 mch_memmove(r + i * slen, p, (size_t)slen);
17406 r[len] = NUL;
17407 }
17408
17409 rettv->vval.v_string = r;
17410 }
17411}
17412
17413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 * "resolve()" function
17415 */
17416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418{
17419 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017420#ifdef HAVE_READLINK
17421 char_u *buf = NULL;
17422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425#ifdef FEAT_SHORTCUT
17426 {
17427 char_u *v = NULL;
17428
17429 v = mch_resolve_shortcut(p);
17430 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017433 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 }
17435#else
17436# ifdef HAVE_READLINK
17437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 char_u *cpy;
17439 int len;
17440 char_u *remain = NULL;
17441 char_u *q;
17442 int is_relative_to_current = FALSE;
17443 int has_trailing_pathsep = FALSE;
17444 int limit = 100;
17445
17446 p = vim_strsave(p);
17447
17448 if (p[0] == '.' && (vim_ispathsep(p[1])
17449 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17450 is_relative_to_current = TRUE;
17451
17452 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017453 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017456 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458
17459 q = getnextcomp(p);
17460 if (*q != NUL)
17461 {
17462 /* Separate the first path component in "p", and keep the
17463 * remainder (beginning with the path separator). */
17464 remain = vim_strsave(q - 1);
17465 q[-1] = NUL;
17466 }
17467
Bram Moolenaard9462e32011-04-11 21:35:11 +020017468 buf = alloc(MAXPATHL + 1);
17469 if (buf == NULL)
17470 goto fail;
17471
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 for (;;)
17473 {
17474 for (;;)
17475 {
17476 len = readlink((char *)p, (char *)buf, MAXPATHL);
17477 if (len <= 0)
17478 break;
17479 buf[len] = NUL;
17480
17481 if (limit-- == 0)
17482 {
17483 vim_free(p);
17484 vim_free(remain);
17485 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 goto fail;
17488 }
17489
17490 /* Ensure that the result will have a trailing path separator
17491 * if the argument has one. */
17492 if (remain == NULL && has_trailing_pathsep)
17493 add_pathsep(buf);
17494
17495 /* Separate the first path component in the link value and
17496 * concatenate the remainders. */
17497 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17498 if (*q != NUL)
17499 {
17500 if (remain == NULL)
17501 remain = vim_strsave(q - 1);
17502 else
17503 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017504 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 if (cpy != NULL)
17506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 vim_free(remain);
17508 remain = cpy;
17509 }
17510 }
17511 q[-1] = NUL;
17512 }
17513
17514 q = gettail(p);
17515 if (q > p && *q == NUL)
17516 {
17517 /* Ignore trailing path separator. */
17518 q[-1] = NUL;
17519 q = gettail(p);
17520 }
17521 if (q > p && !mch_isFullName(buf))
17522 {
17523 /* symlink is relative to directory of argument */
17524 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17525 if (cpy != NULL)
17526 {
17527 STRCPY(cpy, p);
17528 STRCPY(gettail(cpy), buf);
17529 vim_free(p);
17530 p = cpy;
17531 }
17532 }
17533 else
17534 {
17535 vim_free(p);
17536 p = vim_strsave(buf);
17537 }
17538 }
17539
17540 if (remain == NULL)
17541 break;
17542
17543 /* Append the first path component of "remain" to "p". */
17544 q = getnextcomp(remain + 1);
17545 len = q - remain - (*q != NUL);
17546 cpy = vim_strnsave(p, STRLEN(p) + len);
17547 if (cpy != NULL)
17548 {
17549 STRNCAT(cpy, remain, len);
17550 vim_free(p);
17551 p = cpy;
17552 }
17553 /* Shorten "remain". */
17554 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017555 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 else
17557 {
17558 vim_free(remain);
17559 remain = NULL;
17560 }
17561 }
17562
17563 /* If the result is a relative path name, make it explicitly relative to
17564 * the current directory if and only if the argument had this form. */
17565 if (!vim_ispathsep(*p))
17566 {
17567 if (is_relative_to_current
17568 && *p != NUL
17569 && !(p[0] == '.'
17570 && (p[1] == NUL
17571 || vim_ispathsep(p[1])
17572 || (p[1] == '.'
17573 && (p[2] == NUL
17574 || vim_ispathsep(p[2]))))))
17575 {
17576 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017577 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 if (cpy != NULL)
17579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 vim_free(p);
17581 p = cpy;
17582 }
17583 }
17584 else if (!is_relative_to_current)
17585 {
17586 /* Strip leading "./". */
17587 q = p;
17588 while (q[0] == '.' && vim_ispathsep(q[1]))
17589 q += 2;
17590 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017591 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 }
17593 }
17594
17595 /* Ensure that the result will have no trailing path separator
17596 * if the argument had none. But keep "/" or "//". */
17597 if (!has_trailing_pathsep)
17598 {
17599 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017600 if (after_pathsep(p, q))
17601 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 }
17603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 }
17606# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017607 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608# endif
17609#endif
17610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612
17613#ifdef HAVE_READLINK
17614fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017615 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618}
17619
17620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 */
17623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017624f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
Bram Moolenaar33570922005-01-25 22:26:29 +000017626 list_T *l;
17627 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
Bram Moolenaar0d660222005-01-07 21:51:51 +000017629 if (argvars[0].v_type != VAR_LIST)
17630 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017631 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017632 && !tv_check_lock(l->lv_lock,
17633 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 {
17635 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017636 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017637 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017638 while (li != NULL)
17639 {
17640 ni = li->li_prev;
17641 list_append(l, li);
17642 li = ni;
17643 }
17644 rettv->vval.v_list = l;
17645 rettv->v_type = VAR_LIST;
17646 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017647 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649}
17650
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017651#define SP_NOMOVE 0x01 /* don't move cursor */
17652#define SP_REPEAT 0x02 /* repeat to find outer pair */
17653#define SP_RETCOUNT 0x04 /* return matchcount */
17654#define SP_SETPCMARK 0x08 /* set previous context mark */
17655#define SP_START 0x10 /* accept match at start position */
17656#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17657#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017658#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017659
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017660static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017661
17662/*
17663 * Get flags for a search function.
17664 * Possibly sets "p_ws".
17665 * Returns BACKWARD, FORWARD or zero (for an error).
17666 */
17667 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017668get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017669{
17670 int dir = FORWARD;
17671 char_u *flags;
17672 char_u nbuf[NUMBUFLEN];
17673 int mask;
17674
17675 if (varp->v_type != VAR_UNKNOWN)
17676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017677 flags = get_tv_string_buf_chk(varp, nbuf);
17678 if (flags == NULL)
17679 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017680 while (*flags != NUL)
17681 {
17682 switch (*flags)
17683 {
17684 case 'b': dir = BACKWARD; break;
17685 case 'w': p_ws = TRUE; break;
17686 case 'W': p_ws = FALSE; break;
17687 default: mask = 0;
17688 if (flagsp != NULL)
17689 switch (*flags)
17690 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017691 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017692 case 'e': mask = SP_END; break;
17693 case 'm': mask = SP_RETCOUNT; break;
17694 case 'n': mask = SP_NOMOVE; break;
17695 case 'p': mask = SP_SUBPAT; break;
17696 case 'r': mask = SP_REPEAT; break;
17697 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017698 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699 }
17700 if (mask == 0)
17701 {
17702 EMSG2(_(e_invarg2), flags);
17703 dir = 0;
17704 }
17705 else
17706 *flagsp |= mask;
17707 }
17708 if (dir == 0)
17709 break;
17710 ++flags;
17711 }
17712 }
17713 return dir;
17714}
17715
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017717 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017720search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017722 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 char_u *pat;
17724 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017725 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 int save_p_ws = p_ws;
17727 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017728 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017729 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017730 proftime_T tm;
17731#ifdef FEAT_RELTIME
17732 long time_limit = 0;
17733#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017734 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017735 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017738 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017739 if (dir == 0)
17740 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017741 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017742 if (flags & SP_START)
17743 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017744 if (flags & SP_END)
17745 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017746 if (flags & SP_COLUMN)
17747 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017748
Bram Moolenaar76929292008-01-06 19:07:36 +000017749 /* Optional arguments: line number to stop searching and timeout. */
17750 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017751 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017752 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017753 if (lnum_stop < 0)
17754 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017755#ifdef FEAT_RELTIME
17756 if (argvars[3].v_type != VAR_UNKNOWN)
17757 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017758 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017759 if (time_limit < 0)
17760 goto theend;
17761 }
17762#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017763 }
17764
Bram Moolenaar76929292008-01-06 19:07:36 +000017765#ifdef FEAT_RELTIME
17766 /* Set the time limit, if there is one. */
17767 profile_setlimit(time_limit, &tm);
17768#endif
17769
Bram Moolenaar231334e2005-07-25 20:46:57 +000017770 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017771 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017772 * Check to make sure only those flags are set.
17773 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17774 * flags cannot be set. Check for that condition also.
17775 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017776 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017777 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017780 goto theend;
17781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017783 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017784 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017785 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017786 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017788 if (flags & SP_SUBPAT)
17789 retval = subpatnum;
17790 else
17791 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017792 if (flags & SP_SETPCMARK)
17793 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017795 if (match_pos != NULL)
17796 {
17797 /* Store the match cursor position */
17798 match_pos->lnum = pos.lnum;
17799 match_pos->col = pos.col + 1;
17800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 /* "/$" will put the cursor after the end of the line, may need to
17802 * correct that here */
17803 check_cursor();
17804 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017805
17806 /* If 'n' flag is used: restore cursor position. */
17807 if (flags & SP_NOMOVE)
17808 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017809 else
17810 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017811theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017813
17814 return retval;
17815}
17816
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017817#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017818
17819/*
17820 * round() is not in C90, use ceil() or floor() instead.
17821 */
17822 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017823vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017824{
17825 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17826}
17827
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017828/*
17829 * "round({float})" function
17830 */
17831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017832f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017833{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017834 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017835
17836 rettv->v_type = VAR_FLOAT;
17837 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017838 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017839 else
17840 rettv->vval.v_float = 0.0;
17841}
17842#endif
17843
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017844/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017845 * "screenattr()" function
17846 */
17847 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017848f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017849{
17850 int row;
17851 int col;
17852 int c;
17853
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017854 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17855 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017856 if (row < 0 || row >= screen_Rows
17857 || col < 0 || col >= screen_Columns)
17858 c = -1;
17859 else
17860 c = ScreenAttrs[LineOffset[row] + col];
17861 rettv->vval.v_number = c;
17862}
17863
17864/*
17865 * "screenchar()" function
17866 */
17867 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017868f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017869{
17870 int row;
17871 int col;
17872 int off;
17873 int c;
17874
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017875 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17876 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017877 if (row < 0 || row >= screen_Rows
17878 || col < 0 || col >= screen_Columns)
17879 c = -1;
17880 else
17881 {
17882 off = LineOffset[row] + col;
17883#ifdef FEAT_MBYTE
17884 if (enc_utf8 && ScreenLinesUC[off] != 0)
17885 c = ScreenLinesUC[off];
17886 else
17887#endif
17888 c = ScreenLines[off];
17889 }
17890 rettv->vval.v_number = c;
17891}
17892
17893/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017894 * "screencol()" function
17895 *
17896 * First column is 1 to be consistent with virtcol().
17897 */
17898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017899f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017900{
17901 rettv->vval.v_number = screen_screencol() + 1;
17902}
17903
17904/*
17905 * "screenrow()" function
17906 */
17907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017908f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017909{
17910 rettv->vval.v_number = screen_screenrow() + 1;
17911}
17912
17913/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017914 * "search()" function
17915 */
17916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017917f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017918{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017919 int flags = 0;
17920
17921 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922}
17923
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017925 * "searchdecl()" function
17926 */
17927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017928f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017929{
17930 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017931 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017932 int error = FALSE;
17933 char_u *name;
17934
17935 rettv->vval.v_number = 1; /* default: FAIL */
17936
17937 name = get_tv_string_chk(&argvars[0]);
17938 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017939 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017940 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017941 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017942 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017943 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017944 if (!error && name != NULL)
17945 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017946 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017947}
17948
17949/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017950 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017953searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954{
17955 char_u *spat, *mpat, *epat;
17956 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 int dir;
17959 int flags = 0;
17960 char_u nbuf1[NUMBUFLEN];
17961 char_u nbuf2[NUMBUFLEN];
17962 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017963 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017964 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017965 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017968 spat = get_tv_string_chk(&argvars[0]);
17969 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17970 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17971 if (spat == NULL || mpat == NULL || epat == NULL)
17972 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 /* Handle the optional fourth argument: flags */
17975 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017976 if (dir == 0)
17977 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017978
17979 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017980 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17981 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017982 if ((flags & (SP_END | SP_SUBPAT)) != 0
17983 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017984 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017985 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017986 goto theend;
17987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017989 /* Using 'r' implies 'W', otherwise it doesn't work. */
17990 if (flags & SP_REPEAT)
17991 p_ws = FALSE;
17992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017993 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017994 if (argvars[3].v_type == VAR_UNKNOWN
17995 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 skip = (char_u *)"";
17997 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017999 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 if (argvars[5].v_type != VAR_UNKNOWN)
18001 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018002 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018003 if (lnum_stop < 0)
18004 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018005#ifdef FEAT_RELTIME
18006 if (argvars[6].v_type != VAR_UNKNOWN)
18007 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018008 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018009 if (time_limit < 0)
18010 goto theend;
18011 }
18012#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018013 }
18014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018015 if (skip == NULL)
18016 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018018 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018019 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018020
18021theend:
18022 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018023
18024 return retval;
18025}
18026
18027/*
18028 * "searchpair()" function
18029 */
18030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018031f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018032{
18033 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18034}
18035
18036/*
18037 * "searchpairpos()" function
18038 */
18039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018040f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018041{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018042 pos_T match_pos;
18043 int lnum = 0;
18044 int col = 0;
18045
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018046 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018047 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018048
18049 if (searchpair_cmn(argvars, &match_pos) > 0)
18050 {
18051 lnum = match_pos.lnum;
18052 col = match_pos.col;
18053 }
18054
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018055 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18056 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018057}
18058
18059/*
18060 * Search for a start/middle/end thing.
18061 * Used by searchpair(), see its documentation for the details.
18062 * Returns 0 or -1 for no match,
18063 */
18064 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018065do_searchpair(
18066 char_u *spat, /* start pattern */
18067 char_u *mpat, /* middle pattern */
18068 char_u *epat, /* end pattern */
18069 int dir, /* BACKWARD or FORWARD */
18070 char_u *skip, /* skip expression */
18071 int flags, /* SP_SETPCMARK and other SP_ values */
18072 pos_T *match_pos,
18073 linenr_T lnum_stop, /* stop at this line if not zero */
18074 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018075{
18076 char_u *save_cpo;
18077 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18078 long retval = 0;
18079 pos_T pos;
18080 pos_T firstpos;
18081 pos_T foundpos;
18082 pos_T save_cursor;
18083 pos_T save_pos;
18084 int n;
18085 int r;
18086 int nest = 1;
18087 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018088 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018089 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018090
18091 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18092 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018093 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018094
Bram Moolenaar76929292008-01-06 19:07:36 +000018095#ifdef FEAT_RELTIME
18096 /* Set the time limit, if there is one. */
18097 profile_setlimit(time_limit, &tm);
18098#endif
18099
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018100 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18101 * start/middle/end (pat3, for the top pair). */
18102 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18103 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18104 if (pat2 == NULL || pat3 == NULL)
18105 goto theend;
18106 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18107 if (*mpat == NUL)
18108 STRCPY(pat3, pat2);
18109 else
18110 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18111 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018112 if (flags & SP_START)
18113 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018114
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 save_cursor = curwin->w_cursor;
18116 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018117 clearpos(&firstpos);
18118 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 pat = pat3;
18120 for (;;)
18121 {
18122 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018123 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18125 /* didn't find it or found the first match again: FAIL */
18126 break;
18127
18128 if (firstpos.lnum == 0)
18129 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018130 if (equalpos(pos, foundpos))
18131 {
18132 /* Found the same position again. Can happen with a pattern that
18133 * has "\zs" at the end and searching backwards. Advance one
18134 * character and try again. */
18135 if (dir == BACKWARD)
18136 decl(&pos);
18137 else
18138 incl(&pos);
18139 }
18140 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018142 /* clear the start flag to avoid getting stuck here */
18143 options &= ~SEARCH_START;
18144
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 /* If the skip pattern matches, ignore this match. */
18146 if (*skip != NUL)
18147 {
18148 save_pos = curwin->w_cursor;
18149 curwin->w_cursor = pos;
18150 r = eval_to_bool(skip, &err, NULL, FALSE);
18151 curwin->w_cursor = save_pos;
18152 if (err)
18153 {
18154 /* Evaluating {skip} caused an error, break here. */
18155 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018156 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 break;
18158 }
18159 if (r)
18160 continue;
18161 }
18162
18163 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18164 {
18165 /* Found end when searching backwards or start when searching
18166 * forward: nested pair. */
18167 ++nest;
18168 pat = pat2; /* nested, don't search for middle */
18169 }
18170 else
18171 {
18172 /* Found end when searching forward or start when searching
18173 * backward: end of (nested) pair; or found middle in outer pair. */
18174 if (--nest == 1)
18175 pat = pat3; /* outer level, search for middle */
18176 }
18177
18178 if (nest == 0)
18179 {
18180 /* Found the match: return matchcount or line number. */
18181 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018182 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018184 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018185 if (flags & SP_SETPCMARK)
18186 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 curwin->w_cursor = pos;
18188 if (!(flags & SP_REPEAT))
18189 break;
18190 nest = 1; /* search for next unmatched */
18191 }
18192 }
18193
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018194 if (match_pos != NULL)
18195 {
18196 /* Store the match cursor position */
18197 match_pos->lnum = curwin->w_cursor.lnum;
18198 match_pos->col = curwin->w_cursor.col + 1;
18199 }
18200
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018202 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 curwin->w_cursor = save_cursor;
18204
18205theend:
18206 vim_free(pat2);
18207 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018208 if (p_cpo == empty_option)
18209 p_cpo = save_cpo;
18210 else
18211 /* Darn, evaluating the {skip} expression changed the value. */
18212 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018213
18214 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215}
18216
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018217/*
18218 * "searchpos()" function
18219 */
18220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018221f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018222{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018223 pos_T match_pos;
18224 int lnum = 0;
18225 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018226 int n;
18227 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018228
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018229 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018230 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018231
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018232 n = search_cmn(argvars, &match_pos, &flags);
18233 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018234 {
18235 lnum = match_pos.lnum;
18236 col = match_pos.col;
18237 }
18238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018239 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18240 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018241 if (flags & SP_SUBPAT)
18242 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018243}
18244
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018246f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248#ifdef FEAT_CLIENTSERVER
18249 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018250 char_u *server = get_tv_string_chk(&argvars[0]);
18251 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018254 if (server == NULL || reply == NULL)
18255 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018256 if (check_restricted() || check_secure())
18257 return;
18258# ifdef FEAT_X11
18259 if (check_connection() == FAIL)
18260 return;
18261# endif
18262
18263 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265 EMSG(_("E258: Unable to send to client"));
18266 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018268 rettv->vval.v_number = 0;
18269#else
18270 rettv->vval.v_number = -1;
18271#endif
18272}
18273
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018275f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276{
18277 char_u *r = NULL;
18278
18279#ifdef FEAT_CLIENTSERVER
18280# ifdef WIN32
18281 r = serverGetVimNames();
18282# else
18283 make_connection();
18284 if (X_DISPLAY != NULL)
18285 r = serverGetVimNames(X_DISPLAY);
18286# endif
18287#endif
18288 rettv->v_type = VAR_STRING;
18289 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290}
18291
18292/*
18293 * "setbufvar()" function
18294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018296f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297{
18298 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018301 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 char_u nbuf[NUMBUFLEN];
18303
18304 if (check_restricted() || check_secure())
18305 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018306 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18307 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018308 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 varp = &argvars[2];
18310
18311 if (buf != NULL && varname != NULL && varp != NULL)
18312 {
18313 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315
18316 if (*varname == '&')
18317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018318 long numval;
18319 char_u *strval;
18320 int error = FALSE;
18321
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018323 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018324 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 if (!error && strval != NULL)
18326 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 }
18328 else
18329 {
18330 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18331 if (bufvarname != NULL)
18332 {
18333 STRCPY(bufvarname, "b:");
18334 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018335 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 vim_free(bufvarname);
18337 }
18338 }
18339
18340 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343}
18344
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018347{
18348 dict_T *d;
18349 dictitem_T *di;
18350 char_u *csearch;
18351
18352 if (argvars[0].v_type != VAR_DICT)
18353 {
18354 EMSG(_(e_dictreq));
18355 return;
18356 }
18357
18358 if ((d = argvars[0].vval.v_dict) != NULL)
18359 {
18360 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18361 if (csearch != NULL)
18362 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018363#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018364 if (enc_utf8)
18365 {
18366 int pcc[MAX_MCO];
18367 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018368
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018369 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18370 }
18371 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018372#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018373 set_last_csearch(PTR2CHAR(csearch),
18374 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018375 }
18376
18377 di = dict_find(d, (char_u *)"forward", -1);
18378 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018379 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018380 ? FORWARD : BACKWARD);
18381
18382 di = dict_find(d, (char_u *)"until", -1);
18383 if (di != NULL)
18384 set_csearch_until(!!get_tv_number(&di->di_tv));
18385 }
18386}
18387
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388/*
18389 * "setcmdpos()" function
18390 */
18391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018392f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018394 int pos = (int)get_tv_number(&argvars[0]) - 1;
18395
18396 if (pos >= 0)
18397 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398}
18399
18400/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018401 * "setfperm({fname}, {mode})" function
18402 */
18403 static void
18404f_setfperm(typval_T *argvars, typval_T *rettv)
18405{
18406 char_u *fname;
18407 char_u modebuf[NUMBUFLEN];
18408 char_u *mode_str;
18409 int i;
18410 int mask;
18411 int mode = 0;
18412
18413 rettv->vval.v_number = 0;
18414 fname = get_tv_string_chk(&argvars[0]);
18415 if (fname == NULL)
18416 return;
18417 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18418 if (mode_str == NULL)
18419 return;
18420 if (STRLEN(mode_str) != 9)
18421 {
18422 EMSG2(_(e_invarg2), mode_str);
18423 return;
18424 }
18425
18426 mask = 1;
18427 for (i = 8; i >= 0; --i)
18428 {
18429 if (mode_str[i] != '-')
18430 mode |= mask;
18431 mask = mask << 1;
18432 }
18433 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18434}
18435
18436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 * "setline()" function
18438 */
18439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441{
18442 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018443 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018444 list_T *l = NULL;
18445 listitem_T *li = NULL;
18446 long added = 0;
18447 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018449 lnum = get_tv_lnum(&argvars[0]);
18450 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018452 l = argvars[1].vval.v_list;
18453 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018455 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018457
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018458 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018459 for (;;)
18460 {
18461 if (l != NULL)
18462 {
18463 /* list argument, get next string */
18464 if (li == NULL)
18465 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018466 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018467 li = li->li_next;
18468 }
18469
18470 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018472 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018473
18474 /* When coming here from Insert mode, sync undo, so that this can be
18475 * undone separately from what was previously inserted. */
18476 if (u_sync_once == 2)
18477 {
18478 u_sync_once = 1; /* notify that u_sync() was called */
18479 u_sync(TRUE);
18480 }
18481
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018482 if (lnum <= curbuf->b_ml.ml_line_count)
18483 {
18484 /* existing line, replace it */
18485 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18486 {
18487 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018488 if (lnum == curwin->w_cursor.lnum)
18489 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018490 rettv->vval.v_number = 0; /* OK */
18491 }
18492 }
18493 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18494 {
18495 /* lnum is one past the last line, append the line */
18496 ++added;
18497 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18498 rettv->vval.v_number = 0; /* OK */
18499 }
18500
18501 if (l == NULL) /* only one string argument */
18502 break;
18503 ++lnum;
18504 }
18505
18506 if (added > 0)
18507 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508}
18509
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018510static 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 +000018511
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018513 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018514 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018516set_qf_ll_list(
18517 win_T *wp UNUSED,
18518 typval_T *list_arg UNUSED,
18519 typval_T *action_arg UNUSED,
18520 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018521{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018522#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018523 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018524 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018525 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018526#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018527
Bram Moolenaar2641f772005-03-25 21:58:17 +000018528 rettv->vval.v_number = -1;
18529
18530#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018531 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018532 EMSG(_(e_listreq));
18533 else
18534 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018535 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018536
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018537 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018538 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018539 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018540 if (act == NULL)
18541 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018542 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018543 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018544 else
18545 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018546 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018547 else if (action_arg->v_type == VAR_UNKNOWN)
18548 action = ' ';
18549 else
18550 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018551
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018552 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018553 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018554 rettv->vval.v_number = 0;
18555 }
18556#endif
18557}
18558
18559/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018560 * "setloclist()" function
18561 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018563f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018564{
18565 win_T *win;
18566
18567 rettv->vval.v_number = -1;
18568
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018569 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018570 if (win != NULL)
18571 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18572}
18573
18574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018575 * "setmatches()" function
18576 */
18577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018578f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018579{
18580#ifdef FEAT_SEARCH_EXTRA
18581 list_T *l;
18582 listitem_T *li;
18583 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018584 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018585
18586 rettv->vval.v_number = -1;
18587 if (argvars[0].v_type != VAR_LIST)
18588 {
18589 EMSG(_(e_listreq));
18590 return;
18591 }
18592 if ((l = argvars[0].vval.v_list) != NULL)
18593 {
18594
18595 /* To some extent make sure that we are dealing with a list from
18596 * "getmatches()". */
18597 li = l->lv_first;
18598 while (li != NULL)
18599 {
18600 if (li->li_tv.v_type != VAR_DICT
18601 || (d = li->li_tv.vval.v_dict) == NULL)
18602 {
18603 EMSG(_(e_invarg));
18604 return;
18605 }
18606 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018607 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18608 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018609 && dict_find(d, (char_u *)"priority", -1) != NULL
18610 && dict_find(d, (char_u *)"id", -1) != NULL))
18611 {
18612 EMSG(_(e_invarg));
18613 return;
18614 }
18615 li = li->li_next;
18616 }
18617
18618 clear_matches(curwin);
18619 li = l->lv_first;
18620 while (li != NULL)
18621 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018622 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018623 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018624 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018625 char_u *group;
18626 int priority;
18627 int id;
18628 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018629
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018630 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018631 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18632 {
18633 if (s == NULL)
18634 {
18635 s = list_alloc();
18636 if (s == NULL)
18637 return;
18638 }
18639
18640 /* match from matchaddpos() */
18641 for (i = 1; i < 9; i++)
18642 {
18643 sprintf((char *)buf, (char *)"pos%d", i);
18644 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18645 {
18646 if (di->di_tv.v_type != VAR_LIST)
18647 return;
18648
18649 list_append_tv(s, &di->di_tv);
18650 s->lv_refcount++;
18651 }
18652 else
18653 break;
18654 }
18655 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018656
18657 group = get_dict_string(d, (char_u *)"group", FALSE);
18658 priority = (int)get_dict_number(d, (char_u *)"priority");
18659 id = (int)get_dict_number(d, (char_u *)"id");
18660 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18661 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18662 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018663 if (i == 0)
18664 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018665 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018666 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018667 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018668 }
18669 else
18670 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018671 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018672 list_unref(s);
18673 s = NULL;
18674 }
18675
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018676 li = li->li_next;
18677 }
18678 rettv->vval.v_number = 0;
18679 }
18680#endif
18681}
18682
18683/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018684 * "setpos()" function
18685 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018687f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018688{
18689 pos_T pos;
18690 int fnum;
18691 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018692 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018693
Bram Moolenaar08250432008-02-13 11:42:46 +000018694 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018695 name = get_tv_string_chk(argvars);
18696 if (name != NULL)
18697 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018698 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018699 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018700 if (--pos.col < 0)
18701 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018702 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018703 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018704 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018705 if (fnum == curbuf->b_fnum)
18706 {
18707 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018708 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018709 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018710 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018711 curwin->w_set_curswant = FALSE;
18712 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018713 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018714 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018715 }
18716 else
18717 EMSG(_(e_invarg));
18718 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018719 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18720 {
18721 /* set mark */
18722 if (setmark_pos(name[1], &pos, fnum) == OK)
18723 rettv->vval.v_number = 0;
18724 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018725 else
18726 EMSG(_(e_invarg));
18727 }
18728 }
18729}
18730
18731/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018732 * "setqflist()" function
18733 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018735f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018736{
18737 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18738}
18739
18740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 * "setreg()" function
18742 */
18743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018744f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745{
18746 int regname;
18747 char_u *strregname;
18748 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018749 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 int append;
18751 char_u yank_type;
18752 long block_len;
18753
18754 block_len = -1;
18755 yank_type = MAUTO;
18756 append = FALSE;
18757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018758 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018761 if (strregname == NULL)
18762 return; /* type error; errmsg already given */
18763 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 if (regname == 0 || regname == '@')
18765 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018769 stropt = get_tv_string_chk(&argvars[2]);
18770 if (stropt == NULL)
18771 return; /* type error */
18772 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 switch (*stropt)
18774 {
18775 case 'a': case 'A': /* append */
18776 append = TRUE;
18777 break;
18778 case 'v': case 'c': /* character-wise selection */
18779 yank_type = MCHAR;
18780 break;
18781 case 'V': case 'l': /* line-wise selection */
18782 yank_type = MLINE;
18783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 case 'b': case Ctrl_V: /* block-wise selection */
18785 yank_type = MBLOCK;
18786 if (VIM_ISDIGIT(stropt[1]))
18787 {
18788 ++stropt;
18789 block_len = getdigits(&stropt) - 1;
18790 --stropt;
18791 }
18792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 }
18794 }
18795
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018796 if (argvars[1].v_type == VAR_LIST)
18797 {
18798 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018799 char_u **allocval;
18800 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018801 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018802 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018803 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018804 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018805 int len;
18806
18807 /* If the list is NULL handle like an empty list. */
18808 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018809
Bram Moolenaar7d647822014-04-05 21:28:56 +020018810 /* First half: use for pointers to result lines; second half: use for
18811 * pointers to allocated copies. */
18812 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018813 if (lstval == NULL)
18814 return;
18815 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018816 allocval = lstval + len + 2;
18817 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018818
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018819 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018820 li = li->li_next)
18821 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018822 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018823 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018824 goto free_lstval;
18825 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018826 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018827 /* Need to make a copy, next get_tv_string_buf_chk() will
18828 * overwrite the string. */
18829 strval = vim_strsave(buf);
18830 if (strval == NULL)
18831 goto free_lstval;
18832 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018833 }
18834 *curval++ = strval;
18835 }
18836 *curval++ = NULL;
18837
18838 write_reg_contents_lst(regname, lstval, -1,
18839 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018840free_lstval:
18841 while (curallocval > allocval)
18842 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018843 vim_free(lstval);
18844 }
18845 else
18846 {
18847 strval = get_tv_string_chk(&argvars[1]);
18848 if (strval == NULL)
18849 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018850 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018852 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854}
18855
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018856/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018857 * "settabvar()" function
18858 */
18859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018860f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018861{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018862#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018863 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018864 tabpage_T *tp;
18865#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018866 char_u *varname, *tabvarname;
18867 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018868
18869 rettv->vval.v_number = 0;
18870
18871 if (check_restricted() || check_secure())
18872 return;
18873
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018874#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018875 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018876#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018877 varname = get_tv_string_chk(&argvars[1]);
18878 varp = &argvars[2];
18879
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018880 if (varname != NULL && varp != NULL
18881#ifdef FEAT_WINDOWS
18882 && tp != NULL
18883#endif
18884 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018885 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018886#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018887 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018888 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018889#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018890
18891 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18892 if (tabvarname != NULL)
18893 {
18894 STRCPY(tabvarname, "t:");
18895 STRCPY(tabvarname + 2, varname);
18896 set_var(tabvarname, varp, TRUE);
18897 vim_free(tabvarname);
18898 }
18899
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018900#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018901 /* Restore current tabpage */
18902 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018903 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018904#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018905 }
18906}
18907
18908/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018909 * "settabwinvar()" function
18910 */
18911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018912f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018913{
18914 setwinvar(argvars, rettv, 1);
18915}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916
18917/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018918 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018921f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018923 setwinvar(argvars, rettv, 0);
18924}
18925
18926/*
18927 * "setwinvar()" and "settabwinvar()" functions
18928 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018929
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018931setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018932{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 win_T *win;
18934#ifdef FEAT_WINDOWS
18935 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018936 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018937 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938#endif
18939 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018942 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943
18944 if (check_restricted() || check_secure())
18945 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018946
18947#ifdef FEAT_WINDOWS
18948 if (off == 1)
18949 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18950 else
18951 tp = curtab;
18952#endif
18953 win = find_win_by_nr(&argvars[off], tp);
18954 varname = get_tv_string_chk(&argvars[off + 1]);
18955 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
18957 if (win != NULL && varname != NULL && varp != NULL)
18958 {
18959#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018960 need_switch_win = !(tp == curtab && win == curwin);
18961 if (!need_switch_win
18962 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018965 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018967 long numval;
18968 char_u *strval;
18969 int error = FALSE;
18970
18971 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018972 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018973 strval = get_tv_string_buf_chk(varp, nbuf);
18974 if (!error && strval != NULL)
18975 set_option_value(varname, numval, strval, OPT_LOCAL);
18976 }
18977 else
18978 {
18979 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18980 if (winvarname != NULL)
18981 {
18982 STRCPY(winvarname, "w:");
18983 STRCPY(winvarname + 2, varname);
18984 set_var(winvarname, varp, TRUE);
18985 vim_free(winvarname);
18986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 }
18988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018990 if (need_switch_win)
18991 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992#endif
18993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994}
18995
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018996#ifdef FEAT_CRYPT
18997/*
18998 * "sha256({string})" function
18999 */
19000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019001f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019002{
19003 char_u *p;
19004
19005 p = get_tv_string(&argvars[0]);
19006 rettv->vval.v_string = vim_strsave(
19007 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19008 rettv->v_type = VAR_STRING;
19009}
19010#endif /* FEAT_CRYPT */
19011
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019013 * "shellescape({string})" function
19014 */
19015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019016f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019017{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019018 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019019 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019020 rettv->v_type = VAR_STRING;
19021}
19022
19023/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019024 * shiftwidth() function
19025 */
19026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019027f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019028{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019029 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019030}
19031
19032/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019033 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 */
19035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019036f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 p = get_tv_string(&argvars[0]);
19041 rettv->vval.v_string = vim_strsave(p);
19042 simplify_filename(rettv->vval.v_string); /* simplify in place */
19043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044}
19045
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019046#ifdef FEAT_FLOAT
19047/*
19048 * "sin()" function
19049 */
19050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019051f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019052{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019053 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019054
19055 rettv->v_type = VAR_FLOAT;
19056 if (get_float_arg(argvars, &f) == OK)
19057 rettv->vval.v_float = sin(f);
19058 else
19059 rettv->vval.v_float = 0.0;
19060}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019061
19062/*
19063 * "sinh()" function
19064 */
19065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019066f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019067{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019068 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019069
19070 rettv->v_type = VAR_FLOAT;
19071 if (get_float_arg(argvars, &f) == OK)
19072 rettv->vval.v_float = sinh(f);
19073 else
19074 rettv->vval.v_float = 0.0;
19075}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019076#endif
19077
Bram Moolenaar0d660222005-01-07 21:51:51 +000019078static int
19079#ifdef __BORLANDC__
19080 _RTLENTRYF
19081#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019082 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019083static int
19084#ifdef __BORLANDC__
19085 _RTLENTRYF
19086#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019087 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019088
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019089/* struct used in the array that's given to qsort() */
19090typedef struct
19091{
19092 listitem_T *item;
19093 int idx;
19094} sortItem_T;
19095
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096/* struct storing information about current sort */
19097typedef struct
19098{
19099 int item_compare_ic;
19100 int item_compare_numeric;
19101 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019102#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019104#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019106 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 dict_T *item_compare_selfdict;
19108 int item_compare_func_err;
19109 int item_compare_keep_zero;
19110} sortinfo_T;
19111static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019112static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113#define ITEM_COMPARE_FAIL 999
19114
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019116 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118 static int
19119#ifdef __BORLANDC__
19120_RTLENTRYF
19121#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019122item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019124 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019125 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019127 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 int res;
19129 char_u numbuf1[NUMBUFLEN];
19130 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019132 si1 = (sortItem_T *)s1;
19133 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019134 tv1 = &si1->item->li_tv;
19135 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019136
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019138 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019139 varnumber_T v1 = get_tv_number(tv1);
19140 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019141
19142 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19143 }
19144
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019145#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019147 {
19148 float_T v1 = get_tv_float(tv1);
19149 float_T v2 = get_tv_float(tv2);
19150
19151 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19152 }
19153#endif
19154
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019155 /* tv2string() puts quotes around a string and allocates memory. Don't do
19156 * that for string variables. Use a single quote when comparing with a
19157 * non-string to do what the docs promise. */
19158 if (tv1->v_type == VAR_STRING)
19159 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019161 p1 = (char_u *)"'";
19162 else
19163 p1 = tv1->vval.v_string;
19164 }
19165 else
19166 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19167 if (tv2->v_type == VAR_STRING)
19168 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019170 p2 = (char_u *)"'";
19171 else
19172 p2 = tv2->vval.v_string;
19173 }
19174 else
19175 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019176 if (p1 == NULL)
19177 p1 = (char_u *)"";
19178 if (p2 == NULL)
19179 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019181 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019183 res = STRICMP(p1, p2);
19184 else
19185 res = STRCMP(p1, p2);
19186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019188 {
19189 double n1, n2;
19190 n1 = strtod((char *)p1, (char **)&p1);
19191 n2 = strtod((char *)p2, (char **)&p2);
19192 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19193 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019194
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019195 /* When the result would be zero, compare the item indexes. Makes the
19196 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019198 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019199
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200 vim_free(tofree1);
19201 vim_free(tofree2);
19202 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203}
19204
19205 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019206#ifdef __BORLANDC__
19207_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019209item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019210{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019211 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019212 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019214 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019216 char_u *func_name;
19217 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019219 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019221 return 0;
19222
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019223 si1 = (sortItem_T *)s1;
19224 si2 = (sortItem_T *)s2;
19225
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019226 if (partial == NULL)
19227 func_name = sortinfo->item_compare_func;
19228 else
19229 func_name = partial->pt_name;
19230
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019231 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019232 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019233 copy_tv(&si1->item->li_tv, &argv[0]);
19234 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235
19236 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019237 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019238 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019239 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240 clear_tv(&argv[0]);
19241 clear_tv(&argv[1]);
19242
19243 if (res == FAIL)
19244 res = ITEM_COMPARE_FAIL;
19245 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019246 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019247 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019248 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019250
19251 /* When the result would be zero, compare the pointers themselves. Makes
19252 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019253 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019254 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019255
Bram Moolenaar0d660222005-01-07 21:51:51 +000019256 return res;
19257}
19258
19259/*
19260 * "sort({list})" function
19261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264{
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 list_T *l;
19266 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019267 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019268 sortinfo_T *old_sortinfo;
19269 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019270 long len;
19271 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272
Bram Moolenaar0b962472016-02-22 22:51:33 +010019273 /* Pointer to current info struct used in compare function. Save and
19274 * restore the current one for nested calls. */
19275 old_sortinfo = sortinfo;
19276 sortinfo = &info;
19277
Bram Moolenaar0d660222005-01-07 21:51:51 +000019278 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019279 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 else
19281 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019283 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019284 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19285 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287 rettv->vval.v_list = l;
19288 rettv->v_type = VAR_LIST;
19289 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaar0d660222005-01-07 21:51:51 +000019291 len = list_len(l);
19292 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019293 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294
Bram Moolenaar0b962472016-02-22 22:51:33 +010019295 info.item_compare_ic = FALSE;
19296 info.item_compare_numeric = FALSE;
19297 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019298#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019299 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019300#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019302 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019303 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304 if (argvars[1].v_type != VAR_UNKNOWN)
19305 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019306 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019308 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019309 else if (argvars[1].v_type == VAR_PARTIAL)
19310 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311 else
19312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019313 int error = FALSE;
19314
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019315 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019317 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019320 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019322 else if (i != 0)
19323 {
19324 EMSG(_(e_invarg));
19325 goto theend;
19326 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019327 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019328 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019329 if (*info.item_compare_func == NUL)
19330 {
19331 /* empty string means default sort */
19332 info.item_compare_func = NULL;
19333 }
19334 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019335 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 info.item_compare_func = NULL;
19337 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019338 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019339 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019340 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019341 info.item_compare_func = NULL;
19342 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019343 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019344#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019345 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019346 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019347 info.item_compare_func = NULL;
19348 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019349 }
19350#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019351 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019352 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019353 info.item_compare_func = NULL;
19354 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019355 }
19356 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019358
19359 if (argvars[2].v_type != VAR_UNKNOWN)
19360 {
19361 /* optional third argument: {dict} */
19362 if (argvars[2].v_type != VAR_DICT)
19363 {
19364 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019366 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019367 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019368 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019372 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019373 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019374 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375
Bram Moolenaar327aa022014-03-25 18:24:23 +010019376 i = 0;
19377 if (sort)
19378 {
19379 /* sort(): ptrs will be the list to sort */
19380 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019381 {
19382 ptrs[i].item = li;
19383 ptrs[i].idx = i;
19384 ++i;
19385 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019386
Bram Moolenaar0b962472016-02-22 22:51:33 +010019387 info.item_compare_func_err = FALSE;
19388 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019389 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019390 if ((info.item_compare_func != NULL
19391 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019392 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019394 EMSG(_("E702: Sort compare function failed"));
19395 else
19396 {
19397 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019398 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019399 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019400 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019401 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019402
Bram Moolenaar0b962472016-02-22 22:51:33 +010019403 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019404 {
19405 /* Clear the List and append the items in sorted order. */
19406 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19407 l->lv_len = 0;
19408 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019409 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019410 }
19411 }
19412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019415 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019416
19417 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019418 info.item_compare_func_err = FALSE;
19419 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019420 item_compare_func_ptr = info.item_compare_func != NULL
19421 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019422 ? item_compare2 : item_compare;
19423
19424 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19425 li = li->li_next)
19426 {
19427 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19428 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019429 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019430 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019431 {
19432 EMSG(_("E882: Uniq compare function failed"));
19433 break;
19434 }
19435 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019436
Bram Moolenaar0b962472016-02-22 22:51:33 +010019437 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019438 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019439 while (--i >= 0)
19440 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019441 li = ptrs[i].item->li_next;
19442 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019443 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019444 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019445 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019446 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019447 list_fix_watch(l, li);
19448 listitem_free(li);
19449 l->lv_len--;
19450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452 }
19453
19454 vim_free(ptrs);
19455 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019456theend:
19457 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019458}
19459
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019460/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019461 * "sort({list})" function
19462 */
19463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019464f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019465{
19466 do_sort_uniq(argvars, rettv, TRUE);
19467}
19468
19469/*
19470 * "uniq({list})" function
19471 */
19472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019474{
19475 do_sort_uniq(argvars, rettv, FALSE);
19476}
19477
19478/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019479 * "soundfold({word})" function
19480 */
19481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019482f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019483{
19484 char_u *s;
19485
19486 rettv->v_type = VAR_STRING;
19487 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019488#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019489 rettv->vval.v_string = eval_soundfold(s);
19490#else
19491 rettv->vval.v_string = vim_strsave(s);
19492#endif
19493}
19494
19495/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019496 * "spellbadword()" function
19497 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019499f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019500{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019501 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019502 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019503 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019504
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019505 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019506 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019507
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019508#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019509 if (argvars[0].v_type == VAR_UNKNOWN)
19510 {
19511 /* Find the start and length of the badly spelled word. */
19512 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19513 if (len != 0)
19514 word = ml_get_cursor();
19515 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019516 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019517 {
19518 char_u *str = get_tv_string_chk(&argvars[0]);
19519 int capcol = -1;
19520
19521 if (str != NULL)
19522 {
19523 /* Check the argument for spelling. */
19524 while (*str != NUL)
19525 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019526 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019527 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019528 {
19529 word = str;
19530 break;
19531 }
19532 str += len;
19533 }
19534 }
19535 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019536#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019538 list_append_string(rettv->vval.v_list, word, len);
19539 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019540 attr == HLF_SPB ? "bad" :
19541 attr == HLF_SPR ? "rare" :
19542 attr == HLF_SPL ? "local" :
19543 attr == HLF_SPC ? "caps" :
19544 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019545}
19546
19547/*
19548 * "spellsuggest()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019552{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019553#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019554 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019555 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019556 int maxcount;
19557 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019558 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019559 listitem_T *li;
19560 int need_capital = FALSE;
19561#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019562
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019563 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019564 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019565
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019566#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019567 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019568 {
19569 str = get_tv_string(&argvars[0]);
19570 if (argvars[1].v_type != VAR_UNKNOWN)
19571 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019572 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019573 if (maxcount <= 0)
19574 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019575 if (argvars[2].v_type != VAR_UNKNOWN)
19576 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019577 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019578 if (typeerr)
19579 return;
19580 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019581 }
19582 else
19583 maxcount = 25;
19584
Bram Moolenaar4770d092006-01-12 23:22:24 +000019585 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019586
19587 for (i = 0; i < ga.ga_len; ++i)
19588 {
19589 str = ((char_u **)ga.ga_data)[i];
19590
19591 li = listitem_alloc();
19592 if (li == NULL)
19593 vim_free(str);
19594 else
19595 {
19596 li->li_tv.v_type = VAR_STRING;
19597 li->li_tv.v_lock = 0;
19598 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019599 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019600 }
19601 }
19602 ga_clear(&ga);
19603 }
19604#endif
19605}
19606
Bram Moolenaar0d660222005-01-07 21:51:51 +000019607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019608f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019609{
19610 char_u *str;
19611 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019612 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019613 regmatch_T regmatch;
19614 char_u patbuf[NUMBUFLEN];
19615 char_u *save_cpo;
19616 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019617 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019618 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019619 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019620
19621 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19622 save_cpo = p_cpo;
19623 p_cpo = (char_u *)"";
19624
19625 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019626 if (argvars[1].v_type != VAR_UNKNOWN)
19627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019628 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19629 if (pat == NULL)
19630 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019631 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019632 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019633 }
19634 if (pat == NULL || *pat == NUL)
19635 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019636
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019637 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019639 if (typeerr)
19640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641
Bram Moolenaar0d660222005-01-07 21:51:51 +000019642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19643 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019645 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019646 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019647 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019648 if (*str == NUL)
19649 match = FALSE; /* empty item at the end */
19650 else
19651 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019652 if (match)
19653 end = regmatch.startp[0];
19654 else
19655 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019656 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19657 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019658 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019659 if (list_append_string(rettv->vval.v_list, str,
19660 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019661 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019662 }
19663 if (!match)
19664 break;
19665 /* Advance to just after the match. */
19666 if (regmatch.endp[0] > str)
19667 col = 0;
19668 else
19669 {
19670 /* Don't get stuck at the same match. */
19671#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019672 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019673#else
19674 col = 1;
19675#endif
19676 }
19677 str = regmatch.endp[0];
19678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679
Bram Moolenaar473de612013-06-08 18:19:48 +020019680 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682
Bram Moolenaar0d660222005-01-07 21:51:51 +000019683 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684}
19685
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019686#ifdef FEAT_FLOAT
19687/*
19688 * "sqrt()" function
19689 */
19690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019691f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019692{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019693 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019694
19695 rettv->v_type = VAR_FLOAT;
19696 if (get_float_arg(argvars, &f) == OK)
19697 rettv->vval.v_float = sqrt(f);
19698 else
19699 rettv->vval.v_float = 0.0;
19700}
19701
19702/*
19703 * "str2float()" function
19704 */
19705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019706f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019707{
19708 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19709
19710 if (*p == '+')
19711 p = skipwhite(p + 1);
19712 (void)string2float(p, &rettv->vval.v_float);
19713 rettv->v_type = VAR_FLOAT;
19714}
19715#endif
19716
Bram Moolenaar2c932302006-03-18 21:42:09 +000019717/*
19718 * "str2nr()" function
19719 */
19720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019721f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019722{
19723 int base = 10;
19724 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019725 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019726 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019727
19728 if (argvars[1].v_type != VAR_UNKNOWN)
19729 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019730 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019731 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019732 {
19733 EMSG(_(e_invarg));
19734 return;
19735 }
19736 }
19737
19738 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019739 if (*p == '+')
19740 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019741 switch (base)
19742 {
19743 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19744 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19745 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19746 default: what = 0;
19747 }
19748 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019749 rettv->vval.v_number = n;
19750}
19751
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752#ifdef HAVE_STRFTIME
19753/*
19754 * "strftime({format}[, {time}])" function
19755 */
19756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019757f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758{
19759 char_u result_buf[256];
19760 struct tm *curtime;
19761 time_t seconds;
19762 char_u *p;
19763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019766 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019767 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 seconds = time(NULL);
19769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019770 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 curtime = localtime(&seconds);
19772 /* MSVC returns NULL for an invalid value of seconds. */
19773 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 else
19776 {
19777# ifdef FEAT_MBYTE
19778 vimconv_T conv;
19779 char_u *enc;
19780
19781 conv.vc_type = CONV_NONE;
19782 enc = enc_locale();
19783 convert_setup(&conv, p_enc, enc);
19784 if (conv.vc_type != CONV_NONE)
19785 p = string_convert(&conv, p, NULL);
19786# endif
19787 if (p != NULL)
19788 (void)strftime((char *)result_buf, sizeof(result_buf),
19789 (char *)p, curtime);
19790 else
19791 result_buf[0] = NUL;
19792
19793# ifdef FEAT_MBYTE
19794 if (conv.vc_type != CONV_NONE)
19795 vim_free(p);
19796 convert_setup(&conv, enc, p_enc);
19797 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 else
19800# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802
19803# ifdef FEAT_MBYTE
19804 /* Release conversion descriptors */
19805 convert_setup(&conv, NULL, NULL);
19806 vim_free(enc);
19807# endif
19808 }
19809}
19810#endif
19811
19812/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019813 * "strgetchar()" function
19814 */
19815 static void
19816f_strgetchar(typval_T *argvars, typval_T *rettv)
19817{
19818 char_u *str;
19819 int len;
19820 int error = FALSE;
19821 int charidx;
19822
19823 rettv->vval.v_number = -1;
19824 str = get_tv_string_chk(&argvars[0]);
19825 if (str == NULL)
19826 return;
19827 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019828 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019829 if (error)
19830 return;
19831#ifdef FEAT_MBYTE
19832 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019833 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019834
19835 while (charidx >= 0 && byteidx < len)
19836 {
19837 if (charidx == 0)
19838 {
19839 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19840 break;
19841 }
19842 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019843 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019844 }
19845 }
19846#else
19847 if (charidx < len)
19848 rettv->vval.v_number = str[charidx];
19849#endif
19850}
19851
19852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 * "stridx()" function
19854 */
19855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019856f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857{
19858 char_u buf[NUMBUFLEN];
19859 char_u *needle;
19860 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019865 needle = get_tv_string_chk(&argvars[1]);
19866 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019868 if (needle == NULL || haystack == NULL)
19869 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
Bram Moolenaar33570922005-01-25 22:26:29 +000019871 if (argvars[2].v_type != VAR_UNKNOWN)
19872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873 int error = FALSE;
19874
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019875 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019876 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019878 if (start_idx >= 0)
19879 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 }
19881
19882 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19883 if (pos != NULL)
19884 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885}
19886
19887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 * "string()" function
19889 */
19890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019891f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019892{
19893 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019894 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019896 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019897 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19898 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019899 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019900 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019901 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902}
19903
19904/*
19905 * "strlen()" function
19906 */
19907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019908f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019910 rettv->vval.v_number = (varnumber_T)(STRLEN(
19911 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912}
19913
19914/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019915 * "strchars()" function
19916 */
19917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019919{
19920 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019921 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019922#ifdef FEAT_MBYTE
19923 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019924 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019925#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019926
19927 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019928 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019929 if (skipcc < 0 || skipcc > 1)
19930 EMSG(_(e_invarg));
19931 else
19932 {
19933#ifdef FEAT_MBYTE
19934 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19935 while (*s != NUL)
19936 {
19937 func_mb_ptr2char_adv(&s);
19938 ++len;
19939 }
19940 rettv->vval.v_number = len;
19941#else
19942 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19943#endif
19944 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019945}
19946
19947/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019948 * "strdisplaywidth()" function
19949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019952{
19953 char_u *s = get_tv_string(&argvars[0]);
19954 int col = 0;
19955
19956 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019957 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020019958
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019959 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019960}
19961
19962/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019963 * "strwidth()" function
19964 */
19965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019967{
19968 char_u *s = get_tv_string(&argvars[0]);
19969
19970 rettv->vval.v_number = (varnumber_T)(
19971#ifdef FEAT_MBYTE
19972 mb_string2cells(s, -1)
19973#else
19974 STRLEN(s)
19975#endif
19976 );
19977}
19978
19979/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019980 * "strcharpart()" function
19981 */
19982 static void
19983f_strcharpart(typval_T *argvars, typval_T *rettv)
19984{
19985#ifdef FEAT_MBYTE
19986 char_u *p;
19987 int nchar;
19988 int nbyte = 0;
19989 int charlen;
19990 int len = 0;
19991 int slen;
19992 int error = FALSE;
19993
19994 p = get_tv_string(&argvars[0]);
19995 slen = (int)STRLEN(p);
19996
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019997 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019998 if (!error)
19999 {
20000 if (nchar > 0)
20001 while (nchar > 0 && nbyte < slen)
20002 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020003 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020004 --nchar;
20005 }
20006 else
20007 nbyte = nchar;
20008 if (argvars[2].v_type != VAR_UNKNOWN)
20009 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020010 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020011 while (charlen > 0 && nbyte + len < slen)
20012 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020013 int off = nbyte + len;
20014
20015 if (off < 0)
20016 len += 1;
20017 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020018 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020019 --charlen;
20020 }
20021 }
20022 else
20023 len = slen - nbyte; /* default: all bytes that are available. */
20024 }
20025
20026 /*
20027 * Only return the overlap between the specified part and the actual
20028 * string.
20029 */
20030 if (nbyte < 0)
20031 {
20032 len += nbyte;
20033 nbyte = 0;
20034 }
20035 else if (nbyte > slen)
20036 nbyte = slen;
20037 if (len < 0)
20038 len = 0;
20039 else if (nbyte + len > slen)
20040 len = slen - nbyte;
20041
20042 rettv->v_type = VAR_STRING;
20043 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20044#else
20045 f_strpart(argvars, rettv);
20046#endif
20047}
20048
20049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 * "strpart()" function
20051 */
20052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020053f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054{
20055 char_u *p;
20056 int n;
20057 int len;
20058 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020059 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 slen = (int)STRLEN(p);
20063
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020064 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020065 if (error)
20066 len = 0;
20067 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020068 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 else
20070 len = slen - n; /* default len: all bytes that are available. */
20071
20072 /*
20073 * Only return the overlap between the specified part and the actual
20074 * string.
20075 */
20076 if (n < 0)
20077 {
20078 len += n;
20079 n = 0;
20080 }
20081 else if (n > slen)
20082 n = slen;
20083 if (len < 0)
20084 len = 0;
20085 else if (n + len > slen)
20086 len = slen - n;
20087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 rettv->v_type = VAR_STRING;
20089 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090}
20091
20092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020093 * "strridx()" function
20094 */
20095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020096f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020097{
20098 char_u buf[NUMBUFLEN];
20099 char_u *needle;
20100 char_u *haystack;
20101 char_u *rest;
20102 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020103 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020105 needle = get_tv_string_chk(&argvars[1]);
20106 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020107
20108 rettv->vval.v_number = -1;
20109 if (needle == NULL || haystack == NULL)
20110 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020111
20112 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020113 if (argvars[2].v_type != VAR_UNKNOWN)
20114 {
20115 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020116 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020117 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020118 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020119 }
20120 else
20121 end_idx = haystack_len;
20122
Bram Moolenaar0d660222005-01-07 21:51:51 +000020123 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020125 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020126 lastmatch = haystack + end_idx;
20127 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020128 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020129 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020130 for (rest = haystack; *rest != '\0'; ++rest)
20131 {
20132 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020133 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020134 break;
20135 lastmatch = rest;
20136 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020137 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020138
20139 if (lastmatch == NULL)
20140 rettv->vval.v_number = -1;
20141 else
20142 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20143}
20144
20145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 * "strtrans()" function
20147 */
20148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020149f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020151 rettv->v_type = VAR_STRING;
20152 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153}
20154
20155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020156 * "submatch()" function
20157 */
20158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020160{
Bram Moolenaar41571762014-04-02 19:00:58 +020020161 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020162 int no;
20163 int retList = 0;
20164
20165 no = (int)get_tv_number_chk(&argvars[0], &error);
20166 if (error)
20167 return;
20168 error = FALSE;
20169 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020170 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020171 if (error)
20172 return;
20173
20174 if (retList == 0)
20175 {
20176 rettv->v_type = VAR_STRING;
20177 rettv->vval.v_string = reg_submatch(no);
20178 }
20179 else
20180 {
20181 rettv->v_type = VAR_LIST;
20182 rettv->vval.v_list = reg_submatch_list(no);
20183 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020184}
20185
20186/*
20187 * "substitute()" function
20188 */
20189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020190f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020191{
20192 char_u patbuf[NUMBUFLEN];
20193 char_u subbuf[NUMBUFLEN];
20194 char_u flagsbuf[NUMBUFLEN];
20195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020196 char_u *str = get_tv_string_chk(&argvars[0]);
20197 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20198 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20199 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20200
Bram Moolenaar0d660222005-01-07 21:51:51 +000020201 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020202 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20203 rettv->vval.v_string = NULL;
20204 else
20205 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020206}
20207
20208/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020209 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020212f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
20214 int id = 0;
20215#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020216 linenr_T lnum;
20217 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020219 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020221 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020222 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20223 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020225 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020226 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020227 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228#endif
20229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020230 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231}
20232
20233/*
20234 * "synIDattr(id, what [, mode])" function
20235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020237f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238{
20239 char_u *p = NULL;
20240#ifdef FEAT_SYN_HL
20241 int id;
20242 char_u *what;
20243 char_u *mode;
20244 char_u modebuf[NUMBUFLEN];
20245 int modec;
20246
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020247 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020249 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020253 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 modec = 0; /* replace invalid with current */
20255 }
20256 else
20257 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020258#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020259 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 modec = 'g';
20261 else
20262#endif
20263 if (t_colors > 1)
20264 modec = 'c';
20265 else
20266 modec = 't';
20267 }
20268
20269
20270 switch (TOLOWER_ASC(what[0]))
20271 {
20272 case 'b':
20273 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20274 p = highlight_color(id, what, modec);
20275 else /* bold */
20276 p = highlight_has_attr(id, HL_BOLD, modec);
20277 break;
20278
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020279 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 p = highlight_color(id, what, modec);
20281 break;
20282
20283 case 'i':
20284 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20285 p = highlight_has_attr(id, HL_INVERSE, modec);
20286 else /* italic */
20287 p = highlight_has_attr(id, HL_ITALIC, modec);
20288 break;
20289
20290 case 'n': /* name */
20291 p = get_highlight_name(NULL, id - 1);
20292 break;
20293
20294 case 'r': /* reverse */
20295 p = highlight_has_attr(id, HL_INVERSE, modec);
20296 break;
20297
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020298 case 's':
20299 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20300 p = highlight_color(id, what, modec);
20301 else /* standout */
20302 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 break;
20304
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020305 case 'u':
20306 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20307 /* underline */
20308 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20309 else
20310 /* undercurl */
20311 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 break;
20313 }
20314
20315 if (p != NULL)
20316 p = vim_strsave(p);
20317#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020318 rettv->v_type = VAR_STRING;
20319 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320}
20321
20322/*
20323 * "synIDtrans(id)" function
20324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020326f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327{
20328 int id;
20329
20330#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020331 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332
20333 if (id > 0)
20334 id = syn_get_final_id(id);
20335 else
20336#endif
20337 id = 0;
20338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020339 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340}
20341
20342/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020343 * "synconcealed(lnum, col)" function
20344 */
20345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020346f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020347{
20348#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020349 linenr_T lnum;
20350 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020351 int syntax_flags = 0;
20352 int cchar;
20353 int matchid = 0;
20354 char_u str[NUMBUFLEN];
20355#endif
20356
20357 rettv->v_type = VAR_LIST;
20358 rettv->vval.v_list = NULL;
20359
20360#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20361 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020362 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020363
20364 vim_memset(str, NUL, sizeof(str));
20365
20366 if (rettv_list_alloc(rettv) != FAIL)
20367 {
20368 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20369 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20370 && curwin->w_p_cole > 0)
20371 {
20372 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20373 syntax_flags = get_syntax_info(&matchid);
20374
20375 /* get the conceal character */
20376 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20377 {
20378 cchar = syn_get_sub_char();
20379 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20380 cchar = lcs_conceal;
20381 if (cchar != NUL)
20382 {
20383# ifdef FEAT_MBYTE
20384 if (has_mbyte)
20385 (*mb_char2bytes)(cchar, str);
20386 else
20387# endif
20388 str[0] = cchar;
20389 }
20390 }
20391 }
20392
20393 list_append_number(rettv->vval.v_list,
20394 (syntax_flags & HL_CONCEAL) != 0);
20395 /* -1 to auto-determine strlen */
20396 list_append_string(rettv->vval.v_list, str, -1);
20397 list_append_number(rettv->vval.v_list, matchid);
20398 }
20399#endif
20400}
20401
20402/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020403 * "synstack(lnum, col)" function
20404 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020406f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020407{
20408#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020409 linenr_T lnum;
20410 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020411 int i;
20412 int id;
20413#endif
20414
20415 rettv->v_type = VAR_LIST;
20416 rettv->vval.v_list = NULL;
20417
20418#ifdef FEAT_SYN_HL
20419 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020420 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020421
20422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020423 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020424 && rettv_list_alloc(rettv) != FAIL)
20425 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020426 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020427 for (i = 0; ; ++i)
20428 {
20429 id = syn_get_stack_item(i);
20430 if (id < 0)
20431 break;
20432 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20433 break;
20434 }
20435 }
20436#endif
20437}
20438
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020440get_cmd_output_as_rettv(
20441 typval_T *argvars,
20442 typval_T *rettv,
20443 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020445 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020447 char_u *infile = NULL;
20448 char_u buf[NUMBUFLEN];
20449 int err = FALSE;
20450 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020451 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020452 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020454 rettv->v_type = VAR_STRING;
20455 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020456 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020458
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020459 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020460 {
20461 /*
20462 * Write the string to a temp file, to be used for input of the shell
20463 * command.
20464 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020465 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020466 {
20467 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020468 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020469 }
20470
20471 fd = mch_fopen((char *)infile, WRITEBIN);
20472 if (fd == NULL)
20473 {
20474 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020475 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020476 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020477 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020478 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020479 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20480 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020481 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020482 else
20483 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020484 size_t len;
20485
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020486 p = get_tv_string_buf_chk(&argvars[1], buf);
20487 if (p == NULL)
20488 {
20489 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020490 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020491 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020492 len = STRLEN(p);
20493 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020494 err = TRUE;
20495 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020496 if (fclose(fd) != 0)
20497 err = TRUE;
20498 if (err)
20499 {
20500 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020501 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020502 }
20503 }
20504
Bram Moolenaar52a72462014-08-29 15:53:52 +020020505 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20506 * echoes typeahead, that messes up the display. */
20507 if (!msg_silent)
20508 flags += SHELL_COOKED;
20509
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020510 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020512 int len;
20513 listitem_T *li;
20514 char_u *s = NULL;
20515 char_u *start;
20516 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020517 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518
Bram Moolenaar52a72462014-08-29 15:53:52 +020020519 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020520 if (res == NULL)
20521 goto errret;
20522
20523 list = list_alloc();
20524 if (list == NULL)
20525 goto errret;
20526
20527 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020529 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020530 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020531 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020532 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020533
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020534 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020535 if (s == NULL)
20536 goto errret;
20537
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020538 for (p = s; start < end; ++p, ++start)
20539 *p = *start == NUL ? NL : *start;
20540 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020541
20542 li = listitem_alloc();
20543 if (li == NULL)
20544 {
20545 vim_free(s);
20546 goto errret;
20547 }
20548 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020549 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020550 li->li_tv.vval.v_string = s;
20551 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020553
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020554 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020555 rettv->v_type = VAR_LIST;
20556 rettv->vval.v_list = list;
20557 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020559 else
20560 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020561 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020562#ifdef USE_CR
20563 /* translate <CR> into <NL> */
20564 if (res != NULL)
20565 {
20566 char_u *s;
20567
20568 for (s = res; *s; ++s)
20569 {
20570 if (*s == CAR)
20571 *s = NL;
20572 }
20573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574#else
20575# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020576 /* translate <CR><NL> into <NL> */
20577 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020579 char_u *s, *d;
20580
20581 d = res;
20582 for (s = res; *s; ++s)
20583 {
20584 if (s[0] == CAR && s[1] == NL)
20585 ++s;
20586 *d++ = *s;
20587 }
20588 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590# endif
20591#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020592 rettv->vval.v_string = res;
20593 res = NULL;
20594 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020595
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020596errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020597 if (infile != NULL)
20598 {
20599 mch_remove(infile);
20600 vim_free(infile);
20601 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020602 if (res != NULL)
20603 vim_free(res);
20604 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020605 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020606}
20607
20608/*
20609 * "system()" function
20610 */
20611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020612f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020613{
20614 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20615}
20616
20617/*
20618 * "systemlist()" function
20619 */
20620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020621f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020622{
20623 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624}
20625
20626/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020627 * "tabpagebuflist()" function
20628 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020630f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020631{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020632#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020633 tabpage_T *tp;
20634 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020635
20636 if (argvars[0].v_type == VAR_UNKNOWN)
20637 wp = firstwin;
20638 else
20639 {
20640 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20641 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020642 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020643 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020644 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020645 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020646 for (; wp != NULL; wp = wp->w_next)
20647 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020648 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020649 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020650 }
20651#endif
20652}
20653
20654
20655/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020656 * "tabpagenr()" function
20657 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020659f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020660{
20661 int nr = 1;
20662#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020663 char_u *arg;
20664
20665 if (argvars[0].v_type != VAR_UNKNOWN)
20666 {
20667 arg = get_tv_string_chk(&argvars[0]);
20668 nr = 0;
20669 if (arg != NULL)
20670 {
20671 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020672 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020673 else
20674 EMSG2(_(e_invexpr2), arg);
20675 }
20676 }
20677 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020678 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020679#endif
20680 rettv->vval.v_number = nr;
20681}
20682
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020683
20684#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020685static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020686
20687/*
20688 * Common code for tabpagewinnr() and winnr().
20689 */
20690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020691get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020692{
20693 win_T *twin;
20694 int nr = 1;
20695 win_T *wp;
20696 char_u *arg;
20697
20698 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20699 if (argvar->v_type != VAR_UNKNOWN)
20700 {
20701 arg = get_tv_string_chk(argvar);
20702 if (arg == NULL)
20703 nr = 0; /* type error; errmsg already given */
20704 else if (STRCMP(arg, "$") == 0)
20705 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20706 else if (STRCMP(arg, "#") == 0)
20707 {
20708 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20709 if (twin == NULL)
20710 nr = 0;
20711 }
20712 else
20713 {
20714 EMSG2(_(e_invexpr2), arg);
20715 nr = 0;
20716 }
20717 }
20718
20719 if (nr > 0)
20720 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20721 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020722 {
20723 if (wp == NULL)
20724 {
20725 /* didn't find it in this tabpage */
20726 nr = 0;
20727 break;
20728 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020729 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020730 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020731 return nr;
20732}
20733#endif
20734
20735/*
20736 * "tabpagewinnr()" function
20737 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020739f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020740{
20741 int nr = 1;
20742#ifdef FEAT_WINDOWS
20743 tabpage_T *tp;
20744
20745 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20746 if (tp == NULL)
20747 nr = 0;
20748 else
20749 nr = get_winnr(tp, &argvars[1]);
20750#endif
20751 rettv->vval.v_number = nr;
20752}
20753
20754
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020755/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020756 * "tagfiles()" function
20757 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020759f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020760{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020761 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020762 tagname_T tn;
20763 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020764
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020765 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020766 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020767 fname = alloc(MAXPATHL);
20768 if (fname == NULL)
20769 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020770
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020771 for (first = TRUE; ; first = FALSE)
20772 if (get_tagfname(&tn, first, fname) == FAIL
20773 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020774 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020775 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020776 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020777}
20778
20779/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020780 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020784{
20785 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020786
20787 tag_pattern = get_tv_string(&argvars[0]);
20788
20789 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020790 if (*tag_pattern == NUL)
20791 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020792
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020793 if (rettv_list_alloc(rettv) == OK)
20794 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020795}
20796
20797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 * "tempname()" function
20799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020801f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802{
20803 static int x = 'A';
20804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020806 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807
20808 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20809 * names. Skip 'I' and 'O', they are used for shell redirection. */
20810 do
20811 {
20812 if (x == 'Z')
20813 x = '0';
20814 else if (x == '9')
20815 x = 'A';
20816 else
20817 {
20818#ifdef EBCDIC
20819 if (x == 'I')
20820 x = 'J';
20821 else if (x == 'R')
20822 x = 'S';
20823 else
20824#endif
20825 ++x;
20826 }
20827 } while (x == 'I' || x == 'O');
20828}
20829
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020830#ifdef FEAT_FLOAT
20831/*
20832 * "tan()" function
20833 */
20834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020835f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020836{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020837 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020838
20839 rettv->v_type = VAR_FLOAT;
20840 if (get_float_arg(argvars, &f) == OK)
20841 rettv->vval.v_float = tan(f);
20842 else
20843 rettv->vval.v_float = 0.0;
20844}
20845
20846/*
20847 * "tanh()" function
20848 */
20849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020850f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020851{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020852 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020853
20854 rettv->v_type = VAR_FLOAT;
20855 if (get_float_arg(argvars, &f) == OK)
20856 rettv->vval.v_float = tanh(f);
20857 else
20858 rettv->vval.v_float = 0.0;
20859}
20860#endif
20861
Bram Moolenaar574860b2016-05-24 17:33:34 +020020862/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020863 * "test_alloc_fail(id, countdown, repeat)" function
20864 */
20865 static void
20866f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20867{
20868 if (argvars[0].v_type != VAR_NUMBER
20869 || argvars[0].vval.v_number <= 0
20870 || argvars[1].v_type != VAR_NUMBER
20871 || argvars[1].vval.v_number < 0
20872 || argvars[2].v_type != VAR_NUMBER)
20873 EMSG(_(e_invarg));
20874 else
20875 {
20876 alloc_fail_id = argvars[0].vval.v_number;
20877 if (alloc_fail_id >= aid_last)
20878 EMSG(_(e_invarg));
20879 alloc_fail_countdown = argvars[1].vval.v_number;
20880 alloc_fail_repeat = argvars[2].vval.v_number;
20881 did_outofmem_msg = FALSE;
20882 }
20883}
20884
20885/*
20886 * "test_disable_char_avail({expr})" function
20887 */
20888 static void
20889f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20890{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020891 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020892}
20893
20894/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020895 * "test_garbagecollect_now()" function
20896 */
20897 static void
20898f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20899{
20900 /* This is dangerous, any Lists and Dicts used internally may be freed
20901 * while still in use. */
20902 garbage_collect(TRUE);
20903}
20904
20905#ifdef FEAT_JOB_CHANNEL
20906 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020907f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020908{
20909 rettv->v_type = VAR_CHANNEL;
20910 rettv->vval.v_channel = NULL;
20911}
20912#endif
20913
20914 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020915f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020916{
20917 rettv->v_type = VAR_DICT;
20918 rettv->vval.v_dict = NULL;
20919}
20920
20921#ifdef FEAT_JOB_CHANNEL
20922 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020923f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020924{
20925 rettv->v_type = VAR_JOB;
20926 rettv->vval.v_job = NULL;
20927}
20928#endif
20929
20930 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020931f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020932{
20933 rettv->v_type = VAR_LIST;
20934 rettv->vval.v_list = NULL;
20935}
20936
20937 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020938f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020939{
20940 rettv->v_type = VAR_PARTIAL;
20941 rettv->vval.v_partial = NULL;
20942}
20943
20944 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020945f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020946{
20947 rettv->v_type = VAR_STRING;
20948 rettv->vval.v_string = NULL;
20949}
20950
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020951 static void
20952f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
20953{
20954 time_for_testing = (time_t)get_tv_number(&argvars[0]);
20955}
20956
Bram Moolenaar975b5272016-03-15 23:10:59 +010020957#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20958/*
20959 * Get a callback from "arg". It can be a Funcref or a function name.
20960 * When "arg" is zero return an empty string.
20961 * Return NULL for an invalid argument.
20962 */
20963 char_u *
20964get_callback(typval_T *arg, partial_T **pp)
20965{
20966 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20967 {
20968 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020969 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020970 return (*pp)->pt_name;
20971 }
20972 *pp = NULL;
20973 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20974 return arg->vval.v_string;
20975 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20976 return (char_u *)"";
20977 EMSG(_("E921: Invalid callback argument"));
20978 return NULL;
20979}
20980#endif
20981
20982#ifdef FEAT_TIMERS
20983/*
20984 * "timer_start(time, callback [, options])" function
20985 */
20986 static void
20987f_timer_start(typval_T *argvars, typval_T *rettv)
20988{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020989 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010020990 timer_T *timer;
20991 int repeat = 0;
20992 char_u *callback;
20993 dict_T *dict;
20994
Bram Moolenaar38499922016-04-22 20:46:52 +020020995 if (check_secure())
20996 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020997 if (argvars[2].v_type != VAR_UNKNOWN)
20998 {
20999 if (argvars[2].v_type != VAR_DICT
21000 || (dict = argvars[2].vval.v_dict) == NULL)
21001 {
21002 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21003 return;
21004 }
21005 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21006 repeat = get_dict_number(dict, (char_u *)"repeat");
21007 }
21008
21009 timer = create_timer(msec, repeat);
21010 callback = get_callback(&argvars[1], &timer->tr_partial);
21011 if (callback == NULL)
21012 {
21013 stop_timer(timer);
21014 rettv->vval.v_number = -1;
21015 }
21016 else
21017 {
21018 timer->tr_callback = vim_strsave(callback);
21019 rettv->vval.v_number = timer->tr_id;
21020 }
21021}
21022
21023/*
21024 * "timer_stop(timer)" function
21025 */
21026 static void
21027f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21028{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021029 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021030
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021031 if (argvars[0].v_type != VAR_NUMBER)
21032 {
21033 EMSG(_(e_number_exp));
21034 return;
21035 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021036 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021037 if (timer != NULL)
21038 stop_timer(timer);
21039}
21040#endif
21041
Bram Moolenaard52d9742005-08-21 22:20:28 +000021042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 * "tolower(string)" function
21044 */
21045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047{
21048 char_u *p;
21049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021050 p = vim_strsave(get_tv_string(&argvars[0]));
21051 rettv->v_type = VAR_STRING;
21052 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053
21054 if (p != NULL)
21055 while (*p != NUL)
21056 {
21057#ifdef FEAT_MBYTE
21058 int l;
21059
21060 if (enc_utf8)
21061 {
21062 int c, lc;
21063
21064 c = utf_ptr2char(p);
21065 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021066 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 /* TODO: reallocate string when byte count changes. */
21068 if (utf_char2len(lc) == l)
21069 utf_char2bytes(lc, p);
21070 p += l;
21071 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021072 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 p += l; /* skip multi-byte character */
21074 else
21075#endif
21076 {
21077 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21078 ++p;
21079 }
21080 }
21081}
21082
21083/*
21084 * "toupper(string)" function
21085 */
21086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021087f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021090 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091}
21092
21093/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021094 * "tr(string, fromstr, tostr)" function
21095 */
21096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021097f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021098{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021099 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021100 char_u *fromstr;
21101 char_u *tostr;
21102 char_u *p;
21103#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021104 int inlen;
21105 int fromlen;
21106 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021107 int idx;
21108 char_u *cpstr;
21109 int cplen;
21110 int first = TRUE;
21111#endif
21112 char_u buf[NUMBUFLEN];
21113 char_u buf2[NUMBUFLEN];
21114 garray_T ga;
21115
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021116 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021117 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21118 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021119
21120 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121 rettv->v_type = VAR_STRING;
21122 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021123 if (fromstr == NULL || tostr == NULL)
21124 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021125 ga_init2(&ga, (int)sizeof(char), 80);
21126
21127#ifdef FEAT_MBYTE
21128 if (!has_mbyte)
21129#endif
21130 /* not multi-byte: fromstr and tostr must be the same length */
21131 if (STRLEN(fromstr) != STRLEN(tostr))
21132 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021133#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021134error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021135#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021136 EMSG2(_(e_invarg2), fromstr);
21137 ga_clear(&ga);
21138 return;
21139 }
21140
21141 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021142 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021143 {
21144#ifdef FEAT_MBYTE
21145 if (has_mbyte)
21146 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021147 inlen = (*mb_ptr2len)(in_str);
21148 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021149 cplen = inlen;
21150 idx = 0;
21151 for (p = fromstr; *p != NUL; p += fromlen)
21152 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021153 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021154 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021155 {
21156 for (p = tostr; *p != NUL; p += tolen)
21157 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021158 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021159 if (idx-- == 0)
21160 {
21161 cplen = tolen;
21162 cpstr = p;
21163 break;
21164 }
21165 }
21166 if (*p == NUL) /* tostr is shorter than fromstr */
21167 goto error;
21168 break;
21169 }
21170 ++idx;
21171 }
21172
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021173 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021174 {
21175 /* Check that fromstr and tostr have the same number of
21176 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021177 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021178 first = FALSE;
21179 for (p = tostr; *p != NUL; p += tolen)
21180 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021181 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021182 --idx;
21183 }
21184 if (idx != 0)
21185 goto error;
21186 }
21187
Bram Moolenaarcde88542015-08-11 19:14:00 +020021188 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021189 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021190 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021191
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021192 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021193 }
21194 else
21195#endif
21196 {
21197 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021198 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021199 if (p != NULL)
21200 ga_append(&ga, tostr[p - fromstr]);
21201 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021202 ga_append(&ga, *in_str);
21203 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021204 }
21205 }
21206
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021207 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021208 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021209 ga_append(&ga, NUL);
21210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021211 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021212}
21213
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021214#ifdef FEAT_FLOAT
21215/*
21216 * "trunc({float})" function
21217 */
21218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021219f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021220{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021221 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021222
21223 rettv->v_type = VAR_FLOAT;
21224 if (get_float_arg(argvars, &f) == OK)
21225 /* trunc() is not in C90, use floor() or ceil() instead. */
21226 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21227 else
21228 rettv->vval.v_float = 0.0;
21229}
21230#endif
21231
Bram Moolenaar8299df92004-07-10 09:47:34 +000021232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 * "type(expr)" function
21234 */
21235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021236f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021238 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021239
21240 switch (argvars[0].v_type)
21241 {
21242 case VAR_NUMBER: n = 0; break;
21243 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021244 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021245 case VAR_FUNC: n = 2; break;
21246 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021247 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021248 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021249 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021250 if (argvars[0].vval.v_number == VVAL_FALSE
21251 || argvars[0].vval.v_number == VVAL_TRUE)
21252 n = 6;
21253 else
21254 n = 7;
21255 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021256 case VAR_JOB: n = 8; break;
21257 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021258 case VAR_UNKNOWN:
21259 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21260 n = -1;
21261 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021262 }
21263 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264}
21265
21266/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021267 * "undofile(name)" function
21268 */
21269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021270f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021271{
21272 rettv->v_type = VAR_STRING;
21273#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021274 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021275 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021276
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021277 if (*fname == NUL)
21278 {
21279 /* If there is no file name there will be no undo file. */
21280 rettv->vval.v_string = NULL;
21281 }
21282 else
21283 {
21284 char_u *ffname = FullName_save(fname, FALSE);
21285
21286 if (ffname != NULL)
21287 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21288 vim_free(ffname);
21289 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021290 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021291#else
21292 rettv->vval.v_string = NULL;
21293#endif
21294}
21295
21296/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021297 * "undotree()" function
21298 */
21299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021301{
21302 if (rettv_dict_alloc(rettv) == OK)
21303 {
21304 dict_T *dict = rettv->vval.v_dict;
21305 list_T *list;
21306
Bram Moolenaar730cde92010-06-27 05:18:54 +020021307 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021308 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021309 dict_add_nr_str(dict, "save_last",
21310 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021311 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21312 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021313 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021314
21315 list = list_alloc();
21316 if (list != NULL)
21317 {
21318 u_eval_tree(curbuf->b_u_oldhead, list);
21319 dict_add_list(dict, "entries", list);
21320 }
21321 }
21322}
21323
21324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021325 * "values(dict)" function
21326 */
21327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021329{
21330 dict_list(argvars, rettv, 1);
21331}
21332
21333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 * "virtcol(string)" function
21335 */
21336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021337f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338{
21339 colnr_T vcol = 0;
21340 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021341 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021343 fp = var2fpos(&argvars[0], FALSE, &fnum);
21344 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21345 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 {
21347 getvvcol(curwin, fp, NULL, NULL, &vcol);
21348 ++vcol;
21349 }
21350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352}
21353
21354/*
21355 * "visualmode()" function
21356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021358f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 char_u str[2];
21361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 str[0] = curbuf->b_visual_mode_eval;
21364 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021365 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366
21367 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021368 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370}
21371
21372/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021373 * "wildmenumode()" function
21374 */
21375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021377{
21378#ifdef FEAT_WILDMENU
21379 if (wild_menu_showing)
21380 rettv->vval.v_number = 1;
21381#endif
21382}
21383
21384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 * "winbufnr(nr)" function
21386 */
21387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021388f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389{
21390 win_T *wp;
21391
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021392 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021394 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397}
21398
21399/*
21400 * "wincol()" function
21401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404{
21405 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407}
21408
21409/*
21410 * "winheight(nr)" function
21411 */
21412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021413f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
21415 win_T *wp;
21416
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021417 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422}
21423
21424/*
21425 * "winline()" function
21426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021428f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429{
21430 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021431 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432}
21433
21434/*
21435 * "winnr()" function
21436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021438f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439{
21440 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021441
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021443 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446}
21447
21448/*
21449 * "winrestcmd()" function
21450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
21454#ifdef FEAT_WINDOWS
21455 win_T *wp;
21456 int winnr = 1;
21457 garray_T ga;
21458 char_u buf[50];
21459
21460 ga_init2(&ga, (int)sizeof(char), 70);
21461 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21462 {
21463 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21464 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21466 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 ++winnr;
21468 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021469 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021471 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021473 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476}
21477
21478/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021479 * "winrestview()" function
21480 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021482f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021483{
21484 dict_T *dict;
21485
21486 if (argvars[0].v_type != VAR_DICT
21487 || (dict = argvars[0].vval.v_dict) == NULL)
21488 EMSG(_(e_invarg));
21489 else
21490 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021491 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021492 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021493 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021494 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021495#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021496 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021497 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021498#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021499 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21500 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021501 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021502 curwin->w_set_curswant = FALSE;
21503 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021504
Bram Moolenaar82c25852014-05-28 16:47:16 +020021505 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021506 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021507#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021508 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021509 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021510#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021511 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021512 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021513 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021514 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021515
21516 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021517 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021518# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021519 win_new_width(curwin, W_WIDTH(curwin));
21520# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021521 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021522
Bram Moolenaarb851a962014-10-31 15:45:52 +010021523 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021524 curwin->w_topline = 1;
21525 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21526 curwin->w_topline = curbuf->b_ml.ml_line_count;
21527#ifdef FEAT_DIFF
21528 check_topfill(curwin, TRUE);
21529#endif
21530 }
21531}
21532
21533/*
21534 * "winsaveview()" function
21535 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021538{
21539 dict_T *dict;
21540
Bram Moolenaara800b422010-06-27 01:15:55 +020021541 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021542 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021543 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021544
21545 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21546 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21547#ifdef FEAT_VIRTUALEDIT
21548 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21549#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021550 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021551 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21552
21553 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21554#ifdef FEAT_DIFF
21555 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21556#endif
21557 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21558 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21559}
21560
21561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 * "winwidth(nr)" function
21563 */
21564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021565f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
21567 win_T *wp;
21568
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021569 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021571 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021573#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021574 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577#endif
21578}
21579
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021581 * "wordcount()" function
21582 */
21583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021584f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021585{
21586 if (rettv_dict_alloc(rettv) == FAIL)
21587 return;
21588 cursor_pos_info(rettv->vval.v_dict);
21589}
21590
21591/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021592 * Write list of strings to file
21593 */
21594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021595write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021596{
21597 listitem_T *li;
21598 int c;
21599 int ret = OK;
21600 char_u *s;
21601
21602 for (li = list->lv_first; li != NULL; li = li->li_next)
21603 {
21604 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21605 {
21606 if (*s == '\n')
21607 c = putc(NUL, fd);
21608 else
21609 c = putc(*s, fd);
21610 if (c == EOF)
21611 {
21612 ret = FAIL;
21613 break;
21614 }
21615 }
21616 if (!binary || li->li_next != NULL)
21617 if (putc('\n', fd) == EOF)
21618 {
21619 ret = FAIL;
21620 break;
21621 }
21622 if (ret == FAIL)
21623 {
21624 EMSG(_(e_write));
21625 break;
21626 }
21627 }
21628 return ret;
21629}
21630
21631/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021632 * "writefile()" function
21633 */
21634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021635f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021636{
21637 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021638 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021639 char_u *fname;
21640 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021641 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021642
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021643 if (check_restricted() || check_secure())
21644 return;
21645
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021646 if (argvars[0].v_type != VAR_LIST)
21647 {
21648 EMSG2(_(e_listarg), "writefile()");
21649 return;
21650 }
21651 if (argvars[0].vval.v_list == NULL)
21652 return;
21653
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021654 if (argvars[2].v_type != VAR_UNKNOWN)
21655 {
21656 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21657 binary = TRUE;
21658 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21659 append = TRUE;
21660 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021661
21662 /* Always open the file in binary mode, library functions have a mind of
21663 * their own about CR-LF conversion. */
21664 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021665 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21666 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021667 {
21668 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21669 ret = -1;
21670 }
21671 else
21672 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021673 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21674 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021675 fclose(fd);
21676 }
21677
21678 rettv->vval.v_number = ret;
21679}
21680
21681/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021682 * "xor(expr, expr)" function
21683 */
21684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021685f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021686{
21687 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21688 ^ get_tv_number_chk(&argvars[1], NULL);
21689}
21690
21691
21692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021694 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 */
21696 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021697var2fpos(
21698 typval_T *varp,
21699 int dollar_lnum, /* TRUE when $ is last line */
21700 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021702 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021704 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705
Bram Moolenaara5525202006-03-02 22:52:09 +000021706 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021707 if (varp->v_type == VAR_LIST)
21708 {
21709 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021710 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021711 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021712 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021713
21714 l = varp->vval.v_list;
21715 if (l == NULL)
21716 return NULL;
21717
21718 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021719 pos.lnum = list_find_nr(l, 0L, &error);
21720 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021721 return NULL; /* invalid line number */
21722
21723 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021724 pos.col = list_find_nr(l, 1L, &error);
21725 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021726 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021727 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021728
21729 /* We accept "$" for the column number: last column. */
21730 li = list_find(l, 1L);
21731 if (li != NULL && li->li_tv.v_type == VAR_STRING
21732 && li->li_tv.vval.v_string != NULL
21733 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21734 pos.col = len + 1;
21735
Bram Moolenaara5525202006-03-02 22:52:09 +000021736 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021737 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021738 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021739 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021740
Bram Moolenaara5525202006-03-02 22:52:09 +000021741#ifdef FEAT_VIRTUALEDIT
21742 /* Get the virtual offset. Defaults to zero. */
21743 pos.coladd = list_find_nr(l, 2L, &error);
21744 if (error)
21745 pos.coladd = 0;
21746#endif
21747
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021748 return &pos;
21749 }
21750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021751 name = get_tv_string_chk(varp);
21752 if (name == NULL)
21753 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021754 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021756 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21757 {
21758 if (VIsual_active)
21759 return &VIsual;
21760 return &curwin->w_cursor;
21761 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021762 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021764 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21766 return NULL;
21767 return pp;
21768 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021769
21770#ifdef FEAT_VIRTUALEDIT
21771 pos.coladd = 0;
21772#endif
21773
Bram Moolenaar477933c2007-07-17 14:32:23 +000021774 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021775 {
21776 pos.col = 0;
21777 if (name[1] == '0') /* "w0": first visible line */
21778 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021779 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021780 pos.lnum = curwin->w_topline;
21781 return &pos;
21782 }
21783 else if (name[1] == '$') /* "w$": last visible line */
21784 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021785 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021786 pos.lnum = curwin->w_botline - 1;
21787 return &pos;
21788 }
21789 }
21790 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021792 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 {
21794 pos.lnum = curbuf->b_ml.ml_line_count;
21795 pos.col = 0;
21796 }
21797 else
21798 {
21799 pos.lnum = curwin->w_cursor.lnum;
21800 pos.col = (colnr_T)STRLEN(ml_get_curline());
21801 }
21802 return &pos;
21803 }
21804 return NULL;
21805}
21806
21807/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021808 * Convert list in "arg" into a position and optional file number.
21809 * When "fnump" is NULL there is no file number, only 3 items.
21810 * Note that the column is passed on as-is, the caller may want to decrement
21811 * it to use 1 for the first column.
21812 * Return FAIL when conversion is not possible, doesn't check the position for
21813 * validity.
21814 */
21815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021816list2fpos(
21817 typval_T *arg,
21818 pos_T *posp,
21819 int *fnump,
21820 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021821{
21822 list_T *l = arg->vval.v_list;
21823 long i = 0;
21824 long n;
21825
Bram Moolenaar493c1782014-05-28 14:34:46 +020021826 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21827 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021828 if (arg->v_type != VAR_LIST
21829 || l == NULL
21830 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021831 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021832 return FAIL;
21833
21834 if (fnump != NULL)
21835 {
21836 n = list_find_nr(l, i++, NULL); /* fnum */
21837 if (n < 0)
21838 return FAIL;
21839 if (n == 0)
21840 n = curbuf->b_fnum; /* current buffer */
21841 *fnump = n;
21842 }
21843
21844 n = list_find_nr(l, i++, NULL); /* lnum */
21845 if (n < 0)
21846 return FAIL;
21847 posp->lnum = n;
21848
21849 n = list_find_nr(l, i++, NULL); /* col */
21850 if (n < 0)
21851 return FAIL;
21852 posp->col = n;
21853
21854#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021855 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021856 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021857 posp->coladd = 0;
21858 else
21859 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021860#endif
21861
Bram Moolenaar493c1782014-05-28 14:34:46 +020021862 if (curswantp != NULL)
21863 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21864
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021865 return OK;
21866}
21867
21868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 * Get the length of an environment variable name.
21870 * Advance "arg" to the first character after the name.
21871 * Return 0 for error.
21872 */
21873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021874get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875{
21876 char_u *p;
21877 int len;
21878
21879 for (p = *arg; vim_isIDc(*p); ++p)
21880 ;
21881 if (p == *arg) /* no name found */
21882 return 0;
21883
21884 len = (int)(p - *arg);
21885 *arg = p;
21886 return len;
21887}
21888
21889/*
21890 * Get the length of the name of a function or internal variable.
21891 * "arg" is advanced to the first non-white character after the name.
21892 * Return 0 if something is wrong.
21893 */
21894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896{
21897 char_u *p;
21898 int len;
21899
21900 /* Find the end of the name. */
21901 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021902 {
21903 if (*p == ':')
21904 {
21905 /* "s:" is start of "s:var", but "n:" is not and can be used in
21906 * slice "[n:]". Also "xx:" is not a namespace. */
21907 len = (int)(p - *arg);
21908 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21909 || len > 1)
21910 break;
21911 }
21912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 if (p == *arg) /* no name found */
21914 return 0;
21915
21916 len = (int)(p - *arg);
21917 *arg = skipwhite(p);
21918
21919 return len;
21920}
21921
21922/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021923 * Get the length of the name of a variable or function.
21924 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021926 * Return -1 if curly braces expansion failed.
21927 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 * If the name contains 'magic' {}'s, expand them and return the
21929 * expanded name in an allocated string via 'alias' - caller must free.
21930 */
21931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932get_name_len(
21933 char_u **arg,
21934 char_u **alias,
21935 int evaluate,
21936 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937{
21938 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 char_u *p;
21940 char_u *expr_start;
21941 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942
21943 *alias = NULL; /* default to no alias */
21944
21945 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21946 && (*arg)[2] == (int)KE_SNR)
21947 {
21948 /* hard coded <SNR>, already translated */
21949 *arg += 3;
21950 return get_id_len(arg) + 3;
21951 }
21952 len = eval_fname_script(*arg);
21953 if (len > 0)
21954 {
21955 /* literal "<SID>", "s:" or "<SNR>" */
21956 *arg += len;
21957 }
21958
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021960 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021962 p = find_name_end(*arg, &expr_start, &expr_end,
21963 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 if (expr_start != NULL)
21965 {
21966 char_u *temp_string;
21967
21968 if (!evaluate)
21969 {
21970 len += (int)(p - *arg);
21971 *arg = skipwhite(p);
21972 return len;
21973 }
21974
21975 /*
21976 * Include any <SID> etc in the expanded string:
21977 * Thus the -len here.
21978 */
21979 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21980 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021981 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 *alias = temp_string;
21983 *arg = skipwhite(p);
21984 return (int)STRLEN(temp_string);
21985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986
21987 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021988 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 EMSG2(_(e_invexpr2), *arg);
21990
21991 return len;
21992}
21993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021994/*
21995 * Find the end of a variable or function name, taking care of magic braces.
21996 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21997 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021998 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 * Return a pointer to just after the name. Equal to "arg" if there is no
22000 * valid name.
22001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022003find_name_end(
22004 char_u *arg,
22005 char_u **expr_start,
22006 char_u **expr_end,
22007 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022009 int mb_nest = 0;
22010 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022012 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022014 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022016 *expr_start = NULL;
22017 *expr_end = NULL;
22018 }
22019
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022020 /* Quick check for valid starting character. */
22021 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22022 return arg;
22023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 for (p = arg; *p != NUL
22025 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022027 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022028 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022029 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022030 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022031 if (*p == '\'')
22032 {
22033 /* skip over 'string' to avoid counting [ and ] inside it. */
22034 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22035 ;
22036 if (*p == NUL)
22037 break;
22038 }
22039 else if (*p == '"')
22040 {
22041 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22042 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22043 if (*p == '\\' && p[1] != NUL)
22044 ++p;
22045 if (*p == NUL)
22046 break;
22047 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022048 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22049 {
22050 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022051 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022052 len = (int)(p - arg);
22053 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022054 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022055 break;
22056 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022058 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 if (*p == '[')
22061 ++br_nest;
22062 else if (*p == ']')
22063 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022066 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022068 if (*p == '{')
22069 {
22070 mb_nest++;
22071 if (expr_start != NULL && *expr_start == NULL)
22072 *expr_start = p;
22073 }
22074 else if (*p == '}')
22075 {
22076 mb_nest--;
22077 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22078 *expr_end = p;
22079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 }
22082
22083 return p;
22084}
22085
22086/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022087 * Expands out the 'magic' {}'s in a variable/function name.
22088 * Note that this can call itself recursively, to deal with
22089 * constructs like foo{bar}{baz}{bam}
22090 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22091 * "in_start" ^
22092 * "expr_start" ^
22093 * "expr_end" ^
22094 * "in_end" ^
22095 *
22096 * Returns a new allocated string, which the caller must free.
22097 * Returns NULL for failure.
22098 */
22099 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022100make_expanded_name(
22101 char_u *in_start,
22102 char_u *expr_start,
22103 char_u *expr_end,
22104 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022105{
22106 char_u c1;
22107 char_u *retval = NULL;
22108 char_u *temp_result;
22109 char_u *nextcmd = NULL;
22110
22111 if (expr_end == NULL || in_end == NULL)
22112 return NULL;
22113 *expr_start = NUL;
22114 *expr_end = NUL;
22115 c1 = *in_end;
22116 *in_end = NUL;
22117
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022118 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022119 if (temp_result != NULL && nextcmd == NULL)
22120 {
22121 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22122 + (in_end - expr_end) + 1));
22123 if (retval != NULL)
22124 {
22125 STRCPY(retval, in_start);
22126 STRCAT(retval, temp_result);
22127 STRCAT(retval, expr_end + 1);
22128 }
22129 }
22130 vim_free(temp_result);
22131
22132 *in_end = c1; /* put char back for error messages */
22133 *expr_start = '{';
22134 *expr_end = '}';
22135
22136 if (retval != NULL)
22137 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022138 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022139 if (expr_start != NULL)
22140 {
22141 /* Further expansion! */
22142 temp_result = make_expanded_name(retval, expr_start,
22143 expr_end, temp_result);
22144 vim_free(retval);
22145 retval = temp_result;
22146 }
22147 }
22148
22149 return retval;
22150}
22151
22152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022154 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 */
22156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022157eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022159 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22160}
22161
22162/*
22163 * Return TRUE if character "c" can be used as the first character in a
22164 * variable or function name (excluding '{' and '}').
22165 */
22166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022168{
22169 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170}
22171
22172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 * Set number v: variable to "val".
22174 */
22175 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022176set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022178 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179}
22180
22181/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022182 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022184 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022185get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022187 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188}
22189
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022190/*
22191 * Get string v: variable value. Uses a static buffer, can only be used once.
22192 */
22193 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022194get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022195{
22196 return get_tv_string(&vimvars[idx].vv_tv);
22197}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022198
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022200 * Get List v: variable value. Caller must take care of reference count when
22201 * needed.
22202 */
22203 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022204get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022205{
22206 return vimvars[idx].vv_list;
22207}
22208
22209/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022210 * Set v:char to character "c".
22211 */
22212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022213set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022214{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022215 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022216
22217#ifdef FEAT_MBYTE
22218 if (has_mbyte)
22219 buf[(*mb_char2bytes)(c, buf)] = NUL;
22220 else
22221#endif
22222 {
22223 buf[0] = c;
22224 buf[1] = NUL;
22225 }
22226 set_vim_var_string(VV_CHAR, buf, -1);
22227}
22228
22229/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022230 * Set v:count to "count" and v:count1 to "count1".
22231 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 */
22233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234set_vcount(
22235 long count,
22236 long count1,
22237 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022239 if (set_prevcount)
22240 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022241 vimvars[VV_COUNT].vv_nr = count;
22242 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243}
22244
22245/*
22246 * Set string v: variable to a copy of "val".
22247 */
22248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249set_vim_var_string(
22250 int idx,
22251 char_u *val,
22252 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253{
Bram Moolenaara542c682016-01-31 16:28:04 +010022254 clear_tv(&vimvars[idx].vv_di.di_tv);
22255 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022257 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022259 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262}
22263
22264/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022265 * Set List v: variable to "val".
22266 */
22267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022268set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022269{
Bram Moolenaara542c682016-01-31 16:28:04 +010022270 clear_tv(&vimvars[idx].vv_di.di_tv);
22271 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022272 vimvars[idx].vv_list = val;
22273 if (val != NULL)
22274 ++val->lv_refcount;
22275}
22276
22277/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022278 * Set Dictionary v: variable to "val".
22279 */
22280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022281set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022282{
22283 int todo;
22284 hashitem_T *hi;
22285
Bram Moolenaara542c682016-01-31 16:28:04 +010022286 clear_tv(&vimvars[idx].vv_di.di_tv);
22287 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022288 vimvars[idx].vv_dict = val;
22289 if (val != NULL)
22290 {
22291 ++val->dv_refcount;
22292
22293 /* Set readonly */
22294 todo = (int)val->dv_hashtab.ht_used;
22295 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22296 {
22297 if (HASHITEM_EMPTY(hi))
22298 continue;
22299 --todo;
22300 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22301 }
22302 }
22303}
22304
22305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 * Set v:register if needed.
22307 */
22308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022309set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310{
22311 char_u regname;
22312
22313 if (c == 0 || c == ' ')
22314 regname = '"';
22315 else
22316 regname = c;
22317 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022318 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 set_vim_var_string(VV_REG, &regname, 1);
22320}
22321
22322/*
22323 * Get or set v:exception. If "oldval" == NULL, return the current value.
22324 * Otherwise, restore the value to "oldval" and return NULL.
22325 * Must always be called in pairs to save and restore v:exception! Does not
22326 * take care of memory allocations.
22327 */
22328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022329v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330{
22331 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022332 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333
Bram Moolenaare9a41262005-01-15 22:18:47 +000022334 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 return NULL;
22336}
22337
22338/*
22339 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22340 * Otherwise, restore the value to "oldval" and return NULL.
22341 * Must always be called in pairs to save and restore v:throwpoint! Does not
22342 * take care of memory allocations.
22343 */
22344 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022345v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346{
22347 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022348 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349
Bram Moolenaare9a41262005-01-15 22:18:47 +000022350 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 return NULL;
22352}
22353
22354#if defined(FEAT_AUTOCMD) || defined(PROTO)
22355/*
22356 * Set v:cmdarg.
22357 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22358 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22359 * Must always be called in pairs!
22360 */
22361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022362set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363{
22364 char_u *oldval;
22365 char_u *newval;
22366 unsigned len;
22367
Bram Moolenaare9a41262005-01-15 22:18:47 +000022368 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022369 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022371 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022372 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022373 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 }
22375
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022376 if (eap->force_bin == FORCE_BIN)
22377 len = 6;
22378 else if (eap->force_bin == FORCE_NOBIN)
22379 len = 8;
22380 else
22381 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022382
22383 if (eap->read_edit)
22384 len += 7;
22385
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022386 if (eap->force_ff != 0)
22387 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22388# ifdef FEAT_MBYTE
22389 if (eap->force_enc != 0)
22390 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022391 if (eap->bad_char != 0)
22392 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022393# endif
22394
22395 newval = alloc(len + 1);
22396 if (newval == NULL)
22397 return NULL;
22398
22399 if (eap->force_bin == FORCE_BIN)
22400 sprintf((char *)newval, " ++bin");
22401 else if (eap->force_bin == FORCE_NOBIN)
22402 sprintf((char *)newval, " ++nobin");
22403 else
22404 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022405
22406 if (eap->read_edit)
22407 STRCAT(newval, " ++edit");
22408
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022409 if (eap->force_ff != 0)
22410 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22411 eap->cmd + eap->force_ff);
22412# ifdef FEAT_MBYTE
22413 if (eap->force_enc != 0)
22414 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22415 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022416 if (eap->bad_char == BAD_KEEP)
22417 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22418 else if (eap->bad_char == BAD_DROP)
22419 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22420 else if (eap->bad_char != 0)
22421 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022422# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022423 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022424 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425}
22426#endif
22427
22428/*
22429 * Get the value of internal variable "name".
22430 * Return OK or FAIL.
22431 */
22432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022433get_var_tv(
22434 char_u *name,
22435 int len, /* length of "name" */
22436 typval_T *rettv, /* NULL when only checking existence */
22437 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22438 int verbose, /* may give error message */
22439 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440{
22441 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 typval_T *tv = NULL;
22443 typval_T atv;
22444 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446
22447 /* truncate the name, so that we can use strcmp() */
22448 cc = name[len];
22449 name[len] = NUL;
22450
22451 /*
22452 * Check for "b:changedtick".
22453 */
22454 if (STRCMP(name, "b:changedtick") == 0)
22455 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022456 atv.v_type = VAR_NUMBER;
22457 atv.vval.v_number = curbuf->b_changedtick;
22458 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 }
22460
22461 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 * Check for user-defined variables.
22463 */
22464 else
22465 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022466 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022468 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022469 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022470 if (dip != NULL)
22471 *dip = v;
22472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 }
22474
Bram Moolenaare9a41262005-01-15 22:18:47 +000022475 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022477 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022478 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 ret = FAIL;
22480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022481 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022482 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483
22484 name[len] = cc;
22485
22486 return ret;
22487}
22488
22489/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022490 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22491 * Also handle function call with Funcref variable: func(expr)
22492 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22493 */
22494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022495handle_subscript(
22496 char_u **arg,
22497 typval_T *rettv,
22498 int evaluate, /* do more than finding the end */
22499 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022500{
22501 int ret = OK;
22502 dict_T *selfdict = NULL;
22503 char_u *s;
22504 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022505 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022506
22507 while (ret == OK
22508 && (**arg == '['
22509 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022510 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22511 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022512 && !vim_iswhite(*(*arg - 1)))
22513 {
22514 if (**arg == '(')
22515 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022516 partial_T *pt = NULL;
22517
Bram Moolenaard9fba312005-06-26 22:34:35 +000022518 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022519 if (evaluate)
22520 {
22521 functv = *rettv;
22522 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022523
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022524 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022525 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022526 {
22527 pt = functv.vval.v_partial;
22528 s = pt->pt_name;
22529 }
22530 else
22531 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022532 }
22533 else
22534 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022535 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022536 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022537 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022538
22539 /* Clear the funcref afterwards, so that deleting it while
22540 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022541 if (evaluate)
22542 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022543
22544 /* Stop the expression evaluation when immediately aborting on
22545 * error, or when an interrupt occurred or an exception was thrown
22546 * but not caught. */
22547 if (aborting())
22548 {
22549 if (ret == OK)
22550 clear_tv(rettv);
22551 ret = FAIL;
22552 }
22553 dict_unref(selfdict);
22554 selfdict = NULL;
22555 }
22556 else /* **arg == '[' || **arg == '.' */
22557 {
22558 dict_unref(selfdict);
22559 if (rettv->v_type == VAR_DICT)
22560 {
22561 selfdict = rettv->vval.v_dict;
22562 if (selfdict != NULL)
22563 ++selfdict->dv_refcount;
22564 }
22565 else
22566 selfdict = NULL;
22567 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22568 {
22569 clear_tv(rettv);
22570 ret = FAIL;
22571 }
22572 }
22573 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022574
Bram Moolenaar1d429612016-05-24 15:44:17 +020022575 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22576 * Don't do this when "Func" is already a partial that was bound
22577 * explicitly (pt_auto is FALSE). */
22578 if (selfdict != NULL
22579 && (rettv->v_type == VAR_FUNC
22580 || (rettv->v_type == VAR_PARTIAL
22581 && (rettv->vval.v_partial->pt_auto
22582 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022583 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022584 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22585 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022586 char_u *tofree = NULL;
22587 ufunc_T *fp;
22588 char_u fname_buf[FLEN_FIXED + 1];
22589 int error;
22590
22591 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022592 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022593 fp = find_func(fname);
22594 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022595
Bram Moolenaar65639032016-03-16 21:40:30 +010022596 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022597 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022598 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22599
22600 if (pt != NULL)
22601 {
22602 pt->pt_refcount = 1;
22603 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022604 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022605 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022606 if (rettv->v_type == VAR_FUNC)
22607 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022608 /* Just a function: Take over the function name and use
22609 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022610 pt->pt_name = rettv->vval.v_string;
22611 }
22612 else
22613 {
22614 partial_T *ret_pt = rettv->vval.v_partial;
22615 int i;
22616
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022617 /* Partial: copy the function name, use selfdict and copy
22618 * args. Can't take over name or args, the partial might
22619 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022620 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022621 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022622 if (ret_pt->pt_argc > 0)
22623 {
22624 pt->pt_argv = (typval_T *)alloc(
22625 sizeof(typval_T) * ret_pt->pt_argc);
22626 if (pt->pt_argv == NULL)
22627 /* out of memory: drop the arguments */
22628 pt->pt_argc = 0;
22629 else
22630 {
22631 pt->pt_argc = ret_pt->pt_argc;
22632 for (i = 0; i < pt->pt_argc; i++)
22633 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22634 }
22635 }
22636 partial_unref(ret_pt);
22637 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022638 rettv->v_type = VAR_PARTIAL;
22639 rettv->vval.v_partial = pt;
22640 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022641 }
22642 }
22643
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022644 dict_unref(selfdict);
22645 return ret;
22646}
22647
22648/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022649 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022650 * value).
22651 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022652 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022653alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022654{
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022656}
22657
22658/*
22659 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 * The string "s" must have been allocated, it is consumed.
22661 * Return NULL for out of memory, the variable otherwise.
22662 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022664alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665{
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022668 rettv = alloc_tv();
22669 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022671 rettv->v_type = VAR_STRING;
22672 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 }
22674 else
22675 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022676 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677}
22678
22679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022680 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022683free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684{
22685 if (varp != NULL)
22686 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022687 switch (varp->v_type)
22688 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022689 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022690 func_unref(varp->vval.v_string);
22691 /*FALLTHROUGH*/
22692 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022693 vim_free(varp->vval.v_string);
22694 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022695 case VAR_PARTIAL:
22696 partial_unref(varp->vval.v_partial);
22697 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022698 case VAR_LIST:
22699 list_unref(varp->vval.v_list);
22700 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022701 case VAR_DICT:
22702 dict_unref(varp->vval.v_dict);
22703 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022704 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022705#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022706 job_unref(varp->vval.v_job);
22707 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022708#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022709 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022710#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022711 channel_unref(varp->vval.v_channel);
22712 break;
22713#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022714 case VAR_NUMBER:
22715 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022716 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022717 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 vim_free(varp);
22721 }
22722}
22723
22724/*
22725 * Free the memory for a variable value and set the value to NULL or 0.
22726 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
22730 if (varp != NULL)
22731 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022734 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022735 func_unref(varp->vval.v_string);
22736 /*FALLTHROUGH*/
22737 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022738 vim_free(varp->vval.v_string);
22739 varp->vval.v_string = NULL;
22740 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022741 case VAR_PARTIAL:
22742 partial_unref(varp->vval.v_partial);
22743 varp->vval.v_partial = NULL;
22744 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745 case VAR_LIST:
22746 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022747 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022748 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022749 case VAR_DICT:
22750 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022751 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022752 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022753 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022754 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755 varp->vval.v_number = 0;
22756 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022757 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022758#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022759 varp->vval.v_float = 0.0;
22760 break;
22761#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022762 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022764 job_unref(varp->vval.v_job);
22765 varp->vval.v_job = NULL;
22766#endif
22767 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022768 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022770 channel_unref(varp->vval.v_channel);
22771 varp->vval.v_channel = NULL;
22772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022773 case VAR_UNKNOWN:
22774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022776 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 }
22778}
22779
22780/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022781 * Set the value of a variable to NULL without freeing items.
22782 */
22783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022784init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022785{
22786 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022788}
22789
22790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 * Get the number value of a variable.
22792 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022793 * For incompatible types, return 0.
22794 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22795 * caller of incompatible types: it sets *denote to TRUE if "denote"
22796 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022798 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022801 int error = FALSE;
22802
22803 return get_tv_number_chk(varp, &error); /* return 0L on error */
22804}
22805
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022806 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022807get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022808{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022809 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022811 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022813 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022814 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022815 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022816#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022817 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022818 break;
22819#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022821 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022822 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022823 break;
22824 case VAR_STRING:
22825 if (varp->vval.v_string != NULL)
22826 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022827 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022828 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022829 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022830 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022831 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022832 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022833 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022835 case VAR_SPECIAL:
22836 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22837 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022838 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022840 EMSG(_("E910: Using a Job as a Number"));
22841 break;
22842#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022843 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022845 EMSG(_("E913: Using a Channel as a Number"));
22846 break;
22847#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022848 case VAR_UNKNOWN:
22849 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022852 if (denote == NULL) /* useful for values that must be unsigned */
22853 n = -1;
22854 else
22855 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022856 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857}
22858
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022859#ifdef FEAT_FLOAT
22860 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022861get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022862{
22863 switch (varp->v_type)
22864 {
22865 case VAR_NUMBER:
22866 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022867 case VAR_FLOAT:
22868 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022869 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022870 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022871 EMSG(_("E891: Using a Funcref as a Float"));
22872 break;
22873 case VAR_STRING:
22874 EMSG(_("E892: Using a String as a Float"));
22875 break;
22876 case VAR_LIST:
22877 EMSG(_("E893: Using a List as a Float"));
22878 break;
22879 case VAR_DICT:
22880 EMSG(_("E894: Using a Dictionary as a Float"));
22881 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022882 case VAR_SPECIAL:
22883 EMSG(_("E907: Using a special value as a Float"));
22884 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022885 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022886# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022887 EMSG(_("E911: Using a Job as a Float"));
22888 break;
22889# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022890 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022891# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022892 EMSG(_("E914: Using a Channel as a Float"));
22893 break;
22894# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022895 case VAR_UNKNOWN:
22896 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022897 break;
22898 }
22899 return 0;
22900}
22901#endif
22902
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022904 * Get the lnum from the first argument.
22905 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022906 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 */
22908 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022909get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910{
Bram Moolenaar33570922005-01-25 22:26:29 +000022911 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 linenr_T lnum;
22913
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022914 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 if (lnum == 0) /* no valid number, try using line() */
22916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022917 rettv.v_type = VAR_NUMBER;
22918 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022919 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022920 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 }
22922 return lnum;
22923}
22924
22925/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022926 * Get the lnum from the first argument.
22927 * Also accepts "$", then "buf" is used.
22928 * Returns 0 on error.
22929 */
22930 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022931get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022932{
22933 if (argvars[0].v_type == VAR_STRING
22934 && argvars[0].vval.v_string != NULL
22935 && argvars[0].vval.v_string[0] == '$'
22936 && buf != NULL)
22937 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022938 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000022939}
22940
22941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 * Get the string value of a variable.
22943 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022944 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22945 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 * If the String variable has never been set, return an empty string.
22947 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022948 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22949 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022951 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022952get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953{
22954 static char_u mybuf[NUMBUFLEN];
22955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957}
22958
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022959 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022960get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022962 char_u *res = get_tv_string_buf_chk(varp, buf);
22963
22964 return res != NULL ? res : (char_u *)"";
22965}
22966
Bram Moolenaar7d647822014-04-05 21:28:56 +020022967/*
22968 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22969 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022970 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022971get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022972{
22973 static char_u mybuf[NUMBUFLEN];
22974
22975 return get_tv_string_buf_chk(varp, mybuf);
22976}
22977
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022978 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022979get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022980{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022981 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022983 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022984 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
22985 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022986 return buf;
22987 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022988 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022989 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022990 break;
22991 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022992 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022993 break;
22994 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022995 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022996 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022997 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022998#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022999 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023000 break;
23001#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023002 case VAR_STRING:
23003 if (varp->vval.v_string != NULL)
23004 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023005 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023006 case VAR_SPECIAL:
23007 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23008 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023009 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023010#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023011 {
23012 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023013 char *status;
23014
23015 if (job == NULL)
23016 return (char_u *)"no process";
23017 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023018 : job->jv_status == JOB_ENDED ? "dead"
23019 : "run";
23020# ifdef UNIX
23021 vim_snprintf((char *)buf, NUMBUFLEN,
23022 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023023# elif defined(WIN32)
23024 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023025 "process %ld %s",
23026 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023027 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023028# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023029 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023030 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23031# endif
23032 return buf;
23033 }
23034#endif
23035 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023036 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023037#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023038 {
23039 channel_T *channel = varp->vval.v_channel;
23040 char *status = channel_status(channel);
23041
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023042 if (channel == NULL)
23043 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23044 else
23045 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023046 "channel %d %s", channel->ch_id, status);
23047 return buf;
23048 }
23049#endif
23050 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023051 case VAR_UNKNOWN:
23052 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023053 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023055 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056}
23057
23058/*
23059 * Find variable "name" in the list of variables.
23060 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023061 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023062 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023066find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070
Bram Moolenaara7043832005-01-21 11:56:39 +000023071 ht = find_var_ht(name, &varname);
23072 if (htp != NULL)
23073 *htp = ht;
23074 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023076 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077}
23078
23079/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023080 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023081 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023083 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023084find_var_in_ht(
23085 hashtab_T *ht,
23086 int htname,
23087 char_u *varname,
23088 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023089{
Bram Moolenaar33570922005-01-25 22:26:29 +000023090 hashitem_T *hi;
23091
23092 if (*varname == NUL)
23093 {
23094 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023095 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023096 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023097 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 case 'g': return &globvars_var;
23099 case 'v': return &vimvars_var;
23100 case 'b': return &curbuf->b_bufvar;
23101 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023102#ifdef FEAT_WINDOWS
23103 case 't': return &curtab->tp_winvar;
23104#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023105 case 'l': return current_funccal == NULL
23106 ? NULL : &current_funccal->l_vars_var;
23107 case 'a': return current_funccal == NULL
23108 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 }
23110 return NULL;
23111 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023112
23113 hi = hash_find(ht, varname);
23114 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023115 {
23116 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023117 * worked find the variable again. Don't auto-load a script if it was
23118 * loaded already, otherwise it would be loaded every time when
23119 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023120 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023121 {
23122 /* Note: script_autoload() may make "hi" invalid. It must either
23123 * be obtained again or not used. */
23124 if (!script_autoload(varname, FALSE) || aborting())
23125 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023126 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023127 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023128 if (HASHITEM_EMPTY(hi))
23129 return NULL;
23130 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023132}
23133
23134/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023136 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023137 * Set "varname" to the start of name without ':'.
23138 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023140find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023142 hashitem_T *hi;
23143
Bram Moolenaar73627d02015-08-11 15:46:09 +020023144 if (name[0] == NUL)
23145 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 if (name[1] != ':')
23147 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023148 /* The name must not start with a colon or #. */
23149 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 return NULL;
23151 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023152
23153 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023154 hi = hash_find(&compat_hashtab, name);
23155 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023156 return &compat_hashtab;
23157
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023159 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023160 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 }
23162 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023163 if (*name == 'g') /* global variable */
23164 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023165 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23166 */
23167 if (vim_strchr(name + 2, ':') != NULL
23168 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023169 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023171 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023173 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023174#ifdef FEAT_WINDOWS
23175 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023176 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023177#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 if (*name == 'v') /* v: variable */
23179 return &vimvarht;
23180 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023181 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023183 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 if (*name == 's' /* script variable */
23185 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23186 return &SCRIPT_VARS(current_SID);
23187 return NULL;
23188}
23189
23190/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023191 * Get function call environment based on bactrace debug level
23192 */
23193 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023194get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023195{
23196 int i;
23197 funccall_T *funccal;
23198 funccall_T *temp_funccal;
23199
23200 funccal = current_funccal;
23201 if (debug_backtrace_level > 0)
23202 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023203 for (i = 0; i < debug_backtrace_level; i++)
23204 {
23205 temp_funccal = funccal->caller;
23206 if (temp_funccal)
23207 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023208 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023209 /* backtrace level overflow. reset to max */
23210 debug_backtrace_level = i;
23211 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023212 }
23213 return funccal;
23214}
23215
23216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023218 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 * Returns NULL when it doesn't exist.
23220 */
23221 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023222get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223{
Bram Moolenaar33570922005-01-25 22:26:29 +000023224 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023226 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 if (v == NULL)
23228 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230}
23231
23232/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 * sourcing this script and when executing functions defined in the script.
23235 */
23236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023237new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238{
Bram Moolenaara7043832005-01-21 11:56:39 +000023239 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023240 hashtab_T *ht;
23241 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023242
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23244 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023245 /* Re-allocating ga_data means that an ht_array pointing to
23246 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023247 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023248 for (i = 1; i <= ga_scripts.ga_len; ++i)
23249 {
23250 ht = &SCRIPT_VARS(i);
23251 if (ht->ht_mask == HT_INIT_SIZE - 1)
23252 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023253 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023255 }
23256
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 while (ga_scripts.ga_len < id)
23258 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023259 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023260 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023261 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 }
23264 }
23265}
23266
23267/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23269 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 */
23271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023272init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273{
Bram Moolenaar33570922005-01-25 22:26:29 +000023274 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023275 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023276 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023277 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023278 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023279 dict_var->di_tv.vval.v_dict = dict;
23280 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023281 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023282 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23283 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284}
23285
23286/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023287 * Unreference a dictionary initialized by init_var_dict().
23288 */
23289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023290unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023291{
23292 /* Now the dict needs to be freed if no one else is using it, go back to
23293 * normal reference counting. */
23294 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23295 dict_unref(dict);
23296}
23297
23298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023300 * Frees all allocated variables and the value they contain.
23301 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 */
23303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023304vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023305{
23306 vars_clear_ext(ht, TRUE);
23307}
23308
23309/*
23310 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23311 */
23312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023313vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314{
Bram Moolenaara7043832005-01-21 11:56:39 +000023315 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023316 hashitem_T *hi;
23317 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023320 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023321 for (hi = ht->ht_array; todo > 0; ++hi)
23322 {
23323 if (!HASHITEM_EMPTY(hi))
23324 {
23325 --todo;
23326
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023328 * ht_array might change then. hash_clear() takes care of it
23329 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023330 v = HI2DI(hi);
23331 if (free_val)
23332 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023333 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023334 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023335 }
23336 }
23337 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023338 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339}
23340
Bram Moolenaara7043832005-01-21 11:56:39 +000023341/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023342 * Delete a variable from hashtab "ht" at item "hi".
23343 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023346delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347{
Bram Moolenaar33570922005-01-25 22:26:29 +000023348 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023349
23350 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023351 clear_tv(&di->di_tv);
23352 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353}
23354
23355/*
23356 * List the value of one internal variable.
23357 */
23358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023359list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023361 char_u *tofree;
23362 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023363 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023364
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023365 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023367 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023368 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369}
23370
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023372list_one_var_a(
23373 char_u *prefix,
23374 char_u *name,
23375 int type,
23376 char_u *string,
23377 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378{
Bram Moolenaar31859182007-08-14 20:41:13 +000023379 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23380 msg_start();
23381 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 if (name != NULL) /* "a:" vars don't have a name stored */
23383 msg_puts(name);
23384 msg_putchar(' ');
23385 msg_advance(22);
23386 if (type == VAR_NUMBER)
23387 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023388 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023389 msg_putchar('*');
23390 else if (type == VAR_LIST)
23391 {
23392 msg_putchar('[');
23393 if (*string == '[')
23394 ++string;
23395 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023396 else if (type == VAR_DICT)
23397 {
23398 msg_putchar('{');
23399 if (*string == '{')
23400 ++string;
23401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 else
23403 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023404
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023406
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023407 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023408 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023409 if (*first)
23410 {
23411 msg_clr_eos();
23412 *first = FALSE;
23413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414}
23415
23416/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023417 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 * If the variable already exists, the value is updated.
23419 * Otherwise the variable is created.
23420 */
23421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023422set_var(
23423 char_u *name,
23424 typval_T *tv,
23425 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426{
Bram Moolenaar33570922005-01-25 22:26:29 +000023427 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023431 ht = find_var_ht(name, &varname);
23432 if (ht == NULL || *varname == NUL)
23433 {
23434 EMSG2(_(e_illvar), name);
23435 return;
23436 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023437 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023438
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023439 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23440 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023441 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023442
Bram Moolenaar33570922005-01-25 22:26:29 +000023443 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023445 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023446 if (var_check_ro(v->di_flags, name, FALSE)
23447 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023448 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023449
23450 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023451 * Handle setting internal v: variables separately where needed to
23452 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023453 */
23454 if (ht == &vimvarht)
23455 {
23456 if (v->di_tv.v_type == VAR_STRING)
23457 {
23458 vim_free(v->di_tv.vval.v_string);
23459 if (copy || tv->v_type != VAR_STRING)
23460 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23461 else
23462 {
23463 /* Take over the string to avoid an extra alloc/free. */
23464 v->di_tv.vval.v_string = tv->vval.v_string;
23465 tv->vval.v_string = NULL;
23466 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023467 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023469 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023470 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023472 if (STRCMP(varname, "searchforward") == 0)
23473 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023474#ifdef FEAT_SEARCH_EXTRA
23475 else if (STRCMP(varname, "hlsearch") == 0)
23476 {
23477 no_hlsearch = !v->di_tv.vval.v_number;
23478 redraw_all_later(SOME_VALID);
23479 }
23480#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023481 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023482 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023483 else if (v->di_tv.v_type != tv->v_type)
23484 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023485 }
23486
23487 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023488 }
23489 else /* add a new variable */
23490 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023491 /* Can't add "v:" variable. */
23492 if (ht == &vimvarht)
23493 {
23494 EMSG2(_(e_illvar), name);
23495 return;
23496 }
23497
Bram Moolenaar92124a32005-06-17 22:03:40 +000023498 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023499 if (!valid_varname(varname))
23500 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023501
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023502 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23503 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023504 if (v == NULL)
23505 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023506 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023509 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 return;
23511 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023512 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023514
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023515 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023516 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023517 else
23518 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023519 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023520 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023521 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523}
23524
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023525/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023526 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023527 * Also give an error message.
23528 */
23529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023530var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023531{
23532 if (flags & DI_FLAGS_RO)
23533 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023534 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023535 return TRUE;
23536 }
23537 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23538 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023539 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023540 return TRUE;
23541 }
23542 return FALSE;
23543}
23544
23545/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023546 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23547 * Also give an error message.
23548 */
23549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023550var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023551{
23552 if (flags & DI_FLAGS_FIX)
23553 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023554 EMSG2(_("E795: Cannot delete variable %s"),
23555 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023556 return TRUE;
23557 }
23558 return FALSE;
23559}
23560
23561/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023562 * Check if a funcref is assigned to a valid variable name.
23563 * Return TRUE and give an error if not.
23564 */
23565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023566var_check_func_name(
23567 char_u *name, /* points to start of variable name */
23568 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023569{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023570 /* Allow for w: b: s: and t:. */
23571 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023572 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23573 ? name[2] : name[0]))
23574 {
23575 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23576 name);
23577 return TRUE;
23578 }
23579 /* Don't allow hiding a function. When "v" is not NULL we might be
23580 * assigning another function to the same var, the type is checked
23581 * below. */
23582 if (new_var && function_exists(name))
23583 {
23584 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23585 name);
23586 return TRUE;
23587 }
23588 return FALSE;
23589}
23590
23591/*
23592 * Check if a variable name is valid.
23593 * Return FALSE and give an error if not.
23594 */
23595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023596valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023597{
23598 char_u *p;
23599
23600 for (p = varname; *p != NUL; ++p)
23601 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23602 && *p != AUTOLOAD_CHAR)
23603 {
23604 EMSG2(_(e_illvar), varname);
23605 return FALSE;
23606 }
23607 return TRUE;
23608}
23609
23610/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023611 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023612 * Also give an error message, using "name" or _("name") when use_gettext is
23613 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023614 */
23615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023616tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023617{
23618 if (lock & VAR_LOCKED)
23619 {
23620 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023621 name == NULL ? (char_u *)_("Unknown")
23622 : use_gettext ? (char_u *)_(name)
23623 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023624 return TRUE;
23625 }
23626 if (lock & VAR_FIXED)
23627 {
23628 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023629 name == NULL ? (char_u *)_("Unknown")
23630 : use_gettext ? (char_u *)_(name)
23631 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023632 return TRUE;
23633 }
23634 return FALSE;
23635}
23636
23637/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023638 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023639 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023640 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023641 * It is OK for "from" and "to" to point to the same item. This is used to
23642 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023643 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023645copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023647 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023648 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023649 switch (from->v_type)
23650 {
23651 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023652 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023653 to->vval.v_number = from->vval.v_number;
23654 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023655 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023656#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023657 to->vval.v_float = from->vval.v_float;
23658 break;
23659#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023660 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023661#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023662 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023663 if (to->vval.v_job != NULL)
23664 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023665 break;
23666#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023667 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023668#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023669 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023670 if (to->vval.v_channel != NULL)
23671 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023672 break;
23673#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023674 case VAR_STRING:
23675 case VAR_FUNC:
23676 if (from->vval.v_string == NULL)
23677 to->vval.v_string = NULL;
23678 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023680 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 if (from->v_type == VAR_FUNC)
23682 func_ref(to->vval.v_string);
23683 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023684 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023685 case VAR_PARTIAL:
23686 if (from->vval.v_partial == NULL)
23687 to->vval.v_partial = NULL;
23688 else
23689 {
23690 to->vval.v_partial = from->vval.v_partial;
23691 ++to->vval.v_partial->pt_refcount;
23692 }
23693 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023694 case VAR_LIST:
23695 if (from->vval.v_list == NULL)
23696 to->vval.v_list = NULL;
23697 else
23698 {
23699 to->vval.v_list = from->vval.v_list;
23700 ++to->vval.v_list->lv_refcount;
23701 }
23702 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023703 case VAR_DICT:
23704 if (from->vval.v_dict == NULL)
23705 to->vval.v_dict = NULL;
23706 else
23707 {
23708 to->vval.v_dict = from->vval.v_dict;
23709 ++to->vval.v_dict->dv_refcount;
23710 }
23711 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023712 case VAR_UNKNOWN:
23713 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023714 break;
23715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716}
23717
23718/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023719 * Make a copy of an item.
23720 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023721 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23722 * reference to an already copied list/dict can be used.
23723 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023724 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023726item_copy(
23727 typval_T *from,
23728 typval_T *to,
23729 int deep,
23730 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023731{
23732 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023733 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023734
Bram Moolenaar33570922005-01-25 22:26:29 +000023735 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023736 {
23737 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023738 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023739 }
23740 ++recurse;
23741
23742 switch (from->v_type)
23743 {
23744 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023745 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023746 case VAR_STRING:
23747 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023748 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023749 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023750 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023751 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023752 copy_tv(from, to);
23753 break;
23754 case VAR_LIST:
23755 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023756 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023757 if (from->vval.v_list == NULL)
23758 to->vval.v_list = NULL;
23759 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23760 {
23761 /* use the copy made earlier */
23762 to->vval.v_list = from->vval.v_list->lv_copylist;
23763 ++to->vval.v_list->lv_refcount;
23764 }
23765 else
23766 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23767 if (to->vval.v_list == NULL)
23768 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023769 break;
23770 case VAR_DICT:
23771 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023772 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023773 if (from->vval.v_dict == NULL)
23774 to->vval.v_dict = NULL;
23775 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23776 {
23777 /* use the copy made earlier */
23778 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23779 ++to->vval.v_dict->dv_refcount;
23780 }
23781 else
23782 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23783 if (to->vval.v_dict == NULL)
23784 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023785 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023786 case VAR_UNKNOWN:
23787 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023788 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023789 }
23790 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023791 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023792}
23793
23794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 * ":echo expr1 ..." print each argument separated with a space, add a
23796 * newline at the end.
23797 * ":echon expr1 ..." print each argument plain.
23798 */
23799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023800ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801{
23802 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023804 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 char_u *p;
23806 int needclr = TRUE;
23807 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023808 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809
23810 if (eap->skip)
23811 ++emsg_skip;
23812 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023814 /* If eval1() causes an error message the text from the command may
23815 * still need to be cleared. E.g., "echo 22,44". */
23816 need_clr_eos = needclr;
23817
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023819 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820 {
23821 /*
23822 * Report the invalid expression unless the expression evaluation
23823 * has been cancelled due to an aborting error, an interrupt, or an
23824 * exception.
23825 */
23826 if (!aborting())
23827 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023828 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 break;
23830 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023831 need_clr_eos = FALSE;
23832
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 if (!eap->skip)
23834 {
23835 if (atstart)
23836 {
23837 atstart = FALSE;
23838 /* Call msg_start() after eval1(), evaluating the expression
23839 * may cause a message to appear. */
23840 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023841 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023842 /* Mark the saved text as finishing the line, so that what
23843 * follows is displayed on a new line when scrolling back
23844 * at the more prompt. */
23845 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 }
23849 else if (eap->cmdidx == CMD_echo)
23850 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023851 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023852 if (p != NULL)
23853 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023855 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023857 if (*p != TAB && needclr)
23858 {
23859 /* remove any text still there from the command */
23860 msg_clr_eos();
23861 needclr = FALSE;
23862 }
23863 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 }
23865 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023866 {
23867#ifdef FEAT_MBYTE
23868 if (has_mbyte)
23869 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023870 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023871
23872 (void)msg_outtrans_len_attr(p, i, echo_attr);
23873 p += i - 1;
23874 }
23875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023877 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023880 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 arg = skipwhite(arg);
23884 }
23885 eap->nextcmd = check_nextcmd(arg);
23886
23887 if (eap->skip)
23888 --emsg_skip;
23889 else
23890 {
23891 /* remove text that may still be there from the command */
23892 if (needclr)
23893 msg_clr_eos();
23894 if (eap->cmdidx == CMD_echo)
23895 msg_end();
23896 }
23897}
23898
23899/*
23900 * ":echohl {name}".
23901 */
23902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023903ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904{
23905 int id;
23906
23907 id = syn_name2id(eap->arg);
23908 if (id == 0)
23909 echo_attr = 0;
23910 else
23911 echo_attr = syn_id2attr(id);
23912}
23913
23914/*
23915 * ":execute expr1 ..." execute the result of an expression.
23916 * ":echomsg expr1 ..." Print a message
23917 * ":echoerr expr1 ..." Print an error
23918 * Each gets spaces around each argument and a newline at the end for
23919 * echo commands
23920 */
23921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023922ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923{
23924 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023925 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 int ret = OK;
23927 char_u *p;
23928 garray_T ga;
23929 int len;
23930 int save_did_emsg;
23931
23932 ga_init2(&ga, 1, 80);
23933
23934 if (eap->skip)
23935 ++emsg_skip;
23936 while (*arg != NUL && *arg != '|' && *arg != '\n')
23937 {
23938 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023939 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 {
23941 /*
23942 * Report the invalid expression unless the expression evaluation
23943 * has been cancelled due to an aborting error, an interrupt, or an
23944 * exception.
23945 */
23946 if (!aborting())
23947 EMSG2(_(e_invexpr2), p);
23948 ret = FAIL;
23949 break;
23950 }
23951
23952 if (!eap->skip)
23953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023954 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 len = (int)STRLEN(p);
23956 if (ga_grow(&ga, len + 2) == FAIL)
23957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 ret = FAIL;
23960 break;
23961 }
23962 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 ga.ga_len += len;
23966 }
23967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 arg = skipwhite(arg);
23970 }
23971
23972 if (ret != FAIL && ga.ga_data != NULL)
23973 {
23974 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023977 out_flush();
23978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 else if (eap->cmdidx == CMD_echoerr)
23980 {
23981 /* We don't want to abort following commands, restore did_emsg. */
23982 save_did_emsg = did_emsg;
23983 EMSG((char_u *)ga.ga_data);
23984 if (!force_abort)
23985 did_emsg = save_did_emsg;
23986 }
23987 else if (eap->cmdidx == CMD_execute)
23988 do_cmdline((char_u *)ga.ga_data,
23989 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23990 }
23991
23992 ga_clear(&ga);
23993
23994 if (eap->skip)
23995 --emsg_skip;
23996
23997 eap->nextcmd = check_nextcmd(arg);
23998}
23999
24000/*
24001 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24002 * "arg" points to the "&" or '+' when called, to "option" when returning.
24003 * Returns NULL when no option name found. Otherwise pointer to the char
24004 * after the option name.
24005 */
24006 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024007find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008{
24009 char_u *p = *arg;
24010
24011 ++p;
24012 if (*p == 'g' && p[1] == ':')
24013 {
24014 *opt_flags = OPT_GLOBAL;
24015 p += 2;
24016 }
24017 else if (*p == 'l' && p[1] == ':')
24018 {
24019 *opt_flags = OPT_LOCAL;
24020 p += 2;
24021 }
24022 else
24023 *opt_flags = 0;
24024
24025 if (!ASCII_ISALPHA(*p))
24026 return NULL;
24027 *arg = p;
24028
24029 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24030 p += 4; /* termcap option */
24031 else
24032 while (ASCII_ISALPHA(*p))
24033 ++p;
24034 return p;
24035}
24036
24037/*
24038 * ":function"
24039 */
24040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024041ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042{
24043 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024044 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 int j;
24046 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024048 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 char_u *name = NULL;
24050 char_u *p;
24051 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024052 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 garray_T newargs;
24054 garray_T newlines;
24055 int varargs = FALSE;
24056 int mustend = FALSE;
24057 int flags = 0;
24058 ufunc_T *fp;
24059 int indent;
24060 int nesting;
24061 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024062 dictitem_T *v;
24063 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 static int func_nr = 0; /* number for nameless function */
24065 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 hashtab_T *ht;
24067 int todo;
24068 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024069 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070
24071 /*
24072 * ":function" without argument: list functions.
24073 */
24074 if (ends_excmd(*eap->arg))
24075 {
24076 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024077 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024078 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024079 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024080 {
24081 if (!HASHITEM_EMPTY(hi))
24082 {
24083 --todo;
24084 fp = HI2UF(hi);
24085 if (!isdigit(*fp->uf_name))
24086 list_func_head(fp, FALSE);
24087 }
24088 }
24089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 eap->nextcmd = check_nextcmd(eap->arg);
24091 return;
24092 }
24093
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024094 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024095 * ":function /pat": list functions matching pattern.
24096 */
24097 if (*eap->arg == '/')
24098 {
24099 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24100 if (!eap->skip)
24101 {
24102 regmatch_T regmatch;
24103
24104 c = *p;
24105 *p = NUL;
24106 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24107 *p = c;
24108 if (regmatch.regprog != NULL)
24109 {
24110 regmatch.rm_ic = p_ic;
24111
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024112 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024113 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24114 {
24115 if (!HASHITEM_EMPTY(hi))
24116 {
24117 --todo;
24118 fp = HI2UF(hi);
24119 if (!isdigit(*fp->uf_name)
24120 && vim_regexec(&regmatch, fp->uf_name, 0))
24121 list_func_head(fp, FALSE);
24122 }
24123 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024124 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024125 }
24126 }
24127 if (*p == '/')
24128 ++p;
24129 eap->nextcmd = check_nextcmd(p);
24130 return;
24131 }
24132
24133 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024134 * Get the function name. There are these situations:
24135 * func normal function name
24136 * "name" == func, "fudi.fd_dict" == NULL
24137 * dict.func new dictionary entry
24138 * "name" == NULL, "fudi.fd_dict" set,
24139 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24140 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024141 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024142 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24143 * dict.func existing dict entry that's not a Funcref
24144 * "name" == NULL, "fudi.fd_dict" set,
24145 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024146 * s:func script-local function name
24147 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024150 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 paren = (vim_strchr(p, '(') != NULL);
24152 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 {
24154 /*
24155 * Return on an invalid expression in braces, unless the expression
24156 * evaluation has been cancelled due to an aborting error, an
24157 * interrupt, or an exception.
24158 */
24159 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024161 if (!eap->skip && fudi.fd_newkey != NULL)
24162 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024163 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 else
24167 eap->skip = TRUE;
24168 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024169
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 /* An error in a function call during evaluation of an expression in magic
24171 * braces should not cause the function not to be defined. */
24172 saved_did_emsg = did_emsg;
24173 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174
24175 /*
24176 * ":function func" with only function name: list function.
24177 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024178 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 {
24180 if (!ends_excmd(*skipwhite(p)))
24181 {
24182 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024183 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 }
24185 eap->nextcmd = check_nextcmd(p);
24186 if (eap->nextcmd != NULL)
24187 *p = NUL;
24188 if (!eap->skip && !got_int)
24189 {
24190 fp = find_func(name);
24191 if (fp != NULL)
24192 {
24193 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024194 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024196 if (FUNCLINE(fp, j) == NULL)
24197 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 msg_putchar('\n');
24199 msg_outnum((long)(j + 1));
24200 if (j < 9)
24201 msg_putchar(' ');
24202 if (j < 99)
24203 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024204 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 out_flush(); /* show a line at a time */
24206 ui_breakcheck();
24207 }
24208 if (!got_int)
24209 {
24210 msg_putchar('\n');
24211 msg_puts((char_u *)" endfunction");
24212 }
24213 }
24214 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024215 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 }
24219
24220 /*
24221 * ":function name(arg1, arg2)" Define function.
24222 */
24223 p = skipwhite(p);
24224 if (*p != '(')
24225 {
24226 if (!eap->skip)
24227 {
24228 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 }
24231 /* attempt to continue by skipping some text */
24232 if (vim_strchr(p, '(') != NULL)
24233 p = vim_strchr(p, '(');
24234 }
24235 p = skipwhite(p + 1);
24236
24237 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24238 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24239
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024240 if (!eap->skip)
24241 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024242 /* Check the name of the function. Unless it's a dictionary function
24243 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024244 if (name != NULL)
24245 arg = name;
24246 else
24247 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024248 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024249 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24250 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024251 {
24252 if (*arg == K_SPECIAL)
24253 j = 3;
24254 else
24255 j = 0;
24256 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24257 : eval_isnamec(arg[j])))
24258 ++j;
24259 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024260 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024261 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024262 /* Disallow using the g: dict. */
24263 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24264 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024265 }
24266
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 /*
24268 * Isolate the arguments: "arg1, arg2, ...)"
24269 */
24270 while (*p != ')')
24271 {
24272 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24273 {
24274 varargs = TRUE;
24275 p += 3;
24276 mustend = TRUE;
24277 }
24278 else
24279 {
24280 arg = p;
24281 while (ASCII_ISALNUM(*p) || *p == '_')
24282 ++p;
24283 if (arg == p || isdigit(*arg)
24284 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24285 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24286 {
24287 if (!eap->skip)
24288 EMSG2(_("E125: Illegal argument: %s"), arg);
24289 break;
24290 }
24291 if (ga_grow(&newargs, 1) == FAIL)
24292 goto erret;
24293 c = *p;
24294 *p = NUL;
24295 arg = vim_strsave(arg);
24296 if (arg == NULL)
24297 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024298
24299 /* Check for duplicate argument name. */
24300 for (i = 0; i < newargs.ga_len; ++i)
24301 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24302 {
24303 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024304 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024305 goto erret;
24306 }
24307
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24309 *p = c;
24310 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 if (*p == ',')
24312 ++p;
24313 else
24314 mustend = TRUE;
24315 }
24316 p = skipwhite(p);
24317 if (mustend && *p != ')')
24318 {
24319 if (!eap->skip)
24320 EMSG2(_(e_invarg2), eap->arg);
24321 break;
24322 }
24323 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024324 if (*p != ')')
24325 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 ++p; /* skip the ')' */
24327
Bram Moolenaare9a41262005-01-15 22:18:47 +000024328 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 for (;;)
24330 {
24331 p = skipwhite(p);
24332 if (STRNCMP(p, "range", 5) == 0)
24333 {
24334 flags |= FC_RANGE;
24335 p += 5;
24336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024337 else if (STRNCMP(p, "dict", 4) == 0)
24338 {
24339 flags |= FC_DICT;
24340 p += 4;
24341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342 else if (STRNCMP(p, "abort", 5) == 0)
24343 {
24344 flags |= FC_ABORT;
24345 p += 5;
24346 }
24347 else
24348 break;
24349 }
24350
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024351 /* When there is a line break use what follows for the function body.
24352 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24353 if (*p == '\n')
24354 line_arg = p + 1;
24355 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 EMSG(_(e_trailing));
24357
24358 /*
24359 * Read the body of the function, until ":endfunction" is found.
24360 */
24361 if (KeyTyped)
24362 {
24363 /* Check if the function already exists, don't let the user type the
24364 * whole function before telling him it doesn't work! For a script we
24365 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024366 if (!eap->skip && !eap->forceit)
24367 {
24368 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24369 EMSG(_(e_funcdict));
24370 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024371 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024374 if (!eap->skip && did_emsg)
24375 goto erret;
24376
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 msg_putchar('\n'); /* don't overwrite the function name */
24378 cmdline_row = msg_row;
24379 }
24380
24381 indent = 2;
24382 nesting = 0;
24383 for (;;)
24384 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024385 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024386 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024387 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024388 saved_wait_return = FALSE;
24389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024391 sourcing_lnum_off = sourcing_lnum;
24392
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024393 if (line_arg != NULL)
24394 {
24395 /* Use eap->arg, split up in parts by line breaks. */
24396 theline = line_arg;
24397 p = vim_strchr(theline, '\n');
24398 if (p == NULL)
24399 line_arg += STRLEN(line_arg);
24400 else
24401 {
24402 *p = NUL;
24403 line_arg = p + 1;
24404 }
24405 }
24406 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 theline = getcmdline(':', 0L, indent);
24408 else
24409 theline = eap->getline(':', eap->cookie, indent);
24410 if (KeyTyped)
24411 lines_left = Rows - 1;
24412 if (theline == NULL)
24413 {
24414 EMSG(_("E126: Missing :endfunction"));
24415 goto erret;
24416 }
24417
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024418 /* Detect line continuation: sourcing_lnum increased more than one. */
24419 if (sourcing_lnum > sourcing_lnum_off + 1)
24420 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24421 else
24422 sourcing_lnum_off = 0;
24423
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 if (skip_until != NULL)
24425 {
24426 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24427 * don't check for ":endfunc". */
24428 if (STRCMP(theline, skip_until) == 0)
24429 {
24430 vim_free(skip_until);
24431 skip_until = NULL;
24432 }
24433 }
24434 else
24435 {
24436 /* skip ':' and blanks*/
24437 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24438 ;
24439
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024440 /* Check for "endfunction". */
24441 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024443 if (line_arg == NULL)
24444 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 break;
24446 }
24447
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024448 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 * at "end". */
24450 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24451 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024452 else if (STRNCMP(p, "if", 2) == 0
24453 || STRNCMP(p, "wh", 2) == 0
24454 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 || STRNCMP(p, "try", 3) == 0)
24456 indent += 2;
24457
24458 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024459 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024461 if (*p == '!')
24462 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024464 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024465 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024467 ++nesting;
24468 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469 }
24470 }
24471
24472 /* Check for ":append" or ":insert". */
24473 p = skip_range(p, NULL);
24474 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24475 || (p[0] == 'i'
24476 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24477 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24478 skip_until = vim_strsave((char_u *)".");
24479
24480 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24481 arg = skipwhite(skiptowhite(p));
24482 if (arg[0] == '<' && arg[1] =='<'
24483 && ((p[0] == 'p' && p[1] == 'y'
24484 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24485 || (p[0] == 'p' && p[1] == 'e'
24486 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24487 || (p[0] == 't' && p[1] == 'c'
24488 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024489 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24490 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24492 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024493 || (p[0] == 'm' && p[1] == 'z'
24494 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 ))
24496 {
24497 /* ":python <<" continues until a dot, like ":append" */
24498 p = skipwhite(arg + 2);
24499 if (*p == NUL)
24500 skip_until = vim_strsave((char_u *)".");
24501 else
24502 skip_until = vim_strsave(p);
24503 }
24504 }
24505
24506 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024507 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024508 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024509 if (line_arg == NULL)
24510 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024512 }
24513
24514 /* Copy the line to newly allocated memory. get_one_sourceline()
24515 * allocates 250 bytes per line, this saves 80% on average. The cost
24516 * is an extra alloc/free. */
24517 p = vim_strsave(theline);
24518 if (p != NULL)
24519 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024520 if (line_arg == NULL)
24521 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024522 theline = p;
24523 }
24524
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024525 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24526
24527 /* Add NULL lines for continuation lines, so that the line count is
24528 * equal to the index in the growarray. */
24529 while (sourcing_lnum_off-- > 0)
24530 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024531
24532 /* Check for end of eap->arg. */
24533 if (line_arg != NULL && *line_arg == NUL)
24534 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535 }
24536
24537 /* Don't define the function when skipping commands or when an error was
24538 * detected. */
24539 if (eap->skip || did_emsg)
24540 goto erret;
24541
24542 /*
24543 * If there are no errors, add the function
24544 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024545 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024546 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024547 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024548 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024549 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024550 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024551 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024552 goto erret;
24553 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024554
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024555 fp = find_func(name);
24556 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024558 if (!eap->forceit)
24559 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024560 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024561 goto erret;
24562 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024563 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024564 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024565 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 name);
24567 goto erret;
24568 }
24569 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024570 ga_clear_strings(&(fp->uf_args));
24571 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024572 vim_free(name);
24573 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 }
24576 else
24577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024578 char numbuf[20];
24579
24580 fp = NULL;
24581 if (fudi.fd_newkey == NULL && !eap->forceit)
24582 {
24583 EMSG(_(e_funcdict));
24584 goto erret;
24585 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024586 if (fudi.fd_di == NULL)
24587 {
24588 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024589 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024590 goto erret;
24591 }
24592 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024593 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024594 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024595
24596 /* Give the function a sequential number. Can only be used with a
24597 * Funcref! */
24598 vim_free(name);
24599 sprintf(numbuf, "%d", ++func_nr);
24600 name = vim_strsave((char_u *)numbuf);
24601 if (name == NULL)
24602 goto erret;
24603 }
24604
24605 if (fp == NULL)
24606 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024607 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024608 {
24609 int slen, plen;
24610 char_u *scriptname;
24611
24612 /* Check that the autoload name matches the script name. */
24613 j = FAIL;
24614 if (sourcing_name != NULL)
24615 {
24616 scriptname = autoload_name(name);
24617 if (scriptname != NULL)
24618 {
24619 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024620 plen = (int)STRLEN(p);
24621 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024622 if (slen > plen && fnamecmp(p,
24623 sourcing_name + slen - plen) == 0)
24624 j = OK;
24625 vim_free(scriptname);
24626 }
24627 }
24628 if (j == FAIL)
24629 {
24630 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24631 goto erret;
24632 }
24633 }
24634
24635 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 if (fp == NULL)
24637 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638
24639 if (fudi.fd_dict != NULL)
24640 {
24641 if (fudi.fd_di == NULL)
24642 {
24643 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024644 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024645 if (fudi.fd_di == NULL)
24646 {
24647 vim_free(fp);
24648 goto erret;
24649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024650 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24651 {
24652 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024653 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024654 goto erret;
24655 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024656 }
24657 else
24658 /* overwrite existing dict entry */
24659 clear_tv(&fudi.fd_di->di_tv);
24660 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024661 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024662 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024663 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024664
24665 /* behave like "dict" was used */
24666 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024667 }
24668
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024670 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024671 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24672 {
24673 vim_free(fp);
24674 goto erret;
24675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024677 fp->uf_args = newargs;
24678 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024679#ifdef FEAT_PROFILE
24680 fp->uf_tml_count = NULL;
24681 fp->uf_tml_total = NULL;
24682 fp->uf_tml_self = NULL;
24683 fp->uf_profiling = FALSE;
24684 if (prof_def_func())
24685 func_do_profile(fp);
24686#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024687 fp->uf_varargs = varargs;
24688 fp->uf_flags = flags;
24689 fp->uf_calls = 0;
24690 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024691 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692
24693erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 ga_clear_strings(&newargs);
24695 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024696ret_free:
24697 vim_free(skip_until);
24698 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024701 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702}
24703
24704/*
24705 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024706 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024708 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024709 * TFN_INT: internal function name OK
24710 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024711 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712 * Advances "pp" to just after the function name (if no error).
24713 */
24714 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715trans_function_name(
24716 char_u **pp,
24717 int skip, /* only find the end, don't evaluate */
24718 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024719 funcdict_T *fdp, /* return: info about dictionary used */
24720 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024722 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 char_u *start;
24724 char_u *end;
24725 int lead;
24726 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024728 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024729
24730 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024731 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024733
24734 /* Check for hard coded <SNR>: already translated function ID (from a user
24735 * command). */
24736 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24737 && (*pp)[2] == (int)KE_SNR)
24738 {
24739 *pp += 3;
24740 len = get_id_len(pp) + 3;
24741 return vim_strnsave(start, len);
24742 }
24743
24744 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24745 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024747 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024749
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024750 /* Note that TFN_ flags use the same values as GLV_ flags. */
24751 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024752 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024753 if (end == start)
24754 {
24755 if (!skip)
24756 EMSG(_("E129: Function name required"));
24757 goto theend;
24758 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024759 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024760 {
24761 /*
24762 * Report an invalid expression in braces, unless the expression
24763 * evaluation has been cancelled due to an aborting error, an
24764 * interrupt, or an exception.
24765 */
24766 if (!aborting())
24767 {
24768 if (end != NULL)
24769 EMSG2(_(e_invarg2), start);
24770 }
24771 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024772 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024773 goto theend;
24774 }
24775
24776 if (lv.ll_tv != NULL)
24777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024778 if (fdp != NULL)
24779 {
24780 fdp->fd_dict = lv.ll_dict;
24781 fdp->fd_newkey = lv.ll_newkey;
24782 lv.ll_newkey = NULL;
24783 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024785 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24786 {
24787 name = vim_strsave(lv.ll_tv->vval.v_string);
24788 *pp = end;
24789 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024790 else if (lv.ll_tv->v_type == VAR_PARTIAL
24791 && lv.ll_tv->vval.v_partial != NULL)
24792 {
24793 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24794 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024795 if (partial != NULL)
24796 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024797 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024798 else
24799 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024800 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24801 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024802 EMSG(_(e_funcref));
24803 else
24804 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024805 name = NULL;
24806 }
24807 goto theend;
24808 }
24809
24810 if (lv.ll_name == NULL)
24811 {
24812 /* Error found, but continue after the function name. */
24813 *pp = end;
24814 goto theend;
24815 }
24816
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024817 /* Check if the name is a Funcref. If so, use the value. */
24818 if (lv.ll_exp_name != NULL)
24819 {
24820 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024821 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024822 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024823 if (name == lv.ll_exp_name)
24824 name = NULL;
24825 }
24826 else
24827 {
24828 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024829 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024830 if (name == *pp)
24831 name = NULL;
24832 }
24833 if (name != NULL)
24834 {
24835 name = vim_strsave(name);
24836 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024837 if (STRNCMP(name, "<SNR>", 5) == 0)
24838 {
24839 /* Change "<SNR>" to the byte sequence. */
24840 name[0] = K_SPECIAL;
24841 name[1] = KS_EXTRA;
24842 name[2] = (int)KE_SNR;
24843 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24844 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024845 goto theend;
24846 }
24847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024848 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024849 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024850 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024851 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24852 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24853 {
24854 /* When there was "s:" already or the name expanded to get a
24855 * leading "s:" then remove it. */
24856 lv.ll_name += 2;
24857 len -= 2;
24858 lead = 2;
24859 }
24860 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024861 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024862 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024863 /* skip over "s:" and "g:" */
24864 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024865 lv.ll_name += 2;
24866 len = (int)(end - lv.ll_name);
24867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024868
24869 /*
24870 * Copy the function name to allocated memory.
24871 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24872 * Accept <SNR>123_name() outside a script.
24873 */
24874 if (skip)
24875 lead = 0; /* do nothing */
24876 else if (lead > 0)
24877 {
24878 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024879 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24880 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024881 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024882 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024883 if (current_SID <= 0)
24884 {
24885 EMSG(_(e_usingsid));
24886 goto theend;
24887 }
24888 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24889 lead += (int)STRLEN(sid_buf);
24890 }
24891 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024892 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024893 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024894 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024895 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024896 goto theend;
24897 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024898 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024899 {
24900 char_u *cp = vim_strchr(lv.ll_name, ':');
24901
24902 if (cp != NULL && cp < end)
24903 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024904 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024905 goto theend;
24906 }
24907 }
24908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024909 name = alloc((unsigned)(len + lead + 1));
24910 if (name != NULL)
24911 {
24912 if (lead > 0)
24913 {
24914 name[0] = K_SPECIAL;
24915 name[1] = KS_EXTRA;
24916 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024917 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024918 STRCPY(name + 3, sid_buf);
24919 }
24920 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024921 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024922 }
24923 *pp = end;
24924
24925theend:
24926 clear_lval(&lv);
24927 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928}
24929
24930/*
24931 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24932 * Return 2 if "p" starts with "s:".
24933 * Return 0 otherwise.
24934 */
24935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024936eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024938 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24939 * the standard library function. */
24940 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24941 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 return 5;
24943 if (p[0] == 's' && p[1] == ':')
24944 return 2;
24945 return 0;
24946}
24947
24948/*
24949 * Return TRUE if "p" starts with "<SID>" or "s:".
24950 * Only works if eval_fname_script() returned non-zero for "p"!
24951 */
24952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024953eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954{
24955 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24956}
24957
24958/*
24959 * List the head of the function: "name(arg1, arg2)".
24960 */
24961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024962list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963{
24964 int j;
24965
24966 msg_start();
24967 if (indent)
24968 MSG_PUTS(" ");
24969 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024970 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 {
24972 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024973 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 }
24975 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024976 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024977 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024978 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 {
24980 if (j)
24981 MSG_PUTS(", ");
24982 msg_puts(FUNCARG(fp, j));
24983 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024984 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 {
24986 if (j)
24987 MSG_PUTS(", ");
24988 MSG_PUTS("...");
24989 }
24990 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024991 if (fp->uf_flags & FC_ABORT)
24992 MSG_PUTS(" abort");
24993 if (fp->uf_flags & FC_RANGE)
24994 MSG_PUTS(" range");
24995 if (fp->uf_flags & FC_DICT)
24996 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024997 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024998 if (p_verbose > 0)
24999 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000}
25001
25002/*
25003 * Find a function by name, return pointer to it in ufuncs.
25004 * Return NULL for unknown function.
25005 */
25006 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025007find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025009 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025011 hi = hash_find(&func_hashtab, name);
25012 if (!HASHITEM_EMPTY(hi))
25013 return HI2UF(hi);
25014 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015}
25016
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025017#if defined(EXITFREE) || defined(PROTO)
25018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025019free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025020{
25021 hashitem_T *hi;
25022
25023 /* Need to start all over every time, because func_free() may change the
25024 * hash table. */
25025 while (func_hashtab.ht_used > 0)
25026 for (hi = func_hashtab.ht_array; ; ++hi)
25027 if (!HASHITEM_EMPTY(hi))
25028 {
25029 func_free(HI2UF(hi));
25030 break;
25031 }
25032}
25033#endif
25034
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025036translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025037{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025038 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025039 return find_internal_func(name) >= 0;
25040 return find_func(name) != NULL;
25041}
25042
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025043/*
25044 * Return TRUE if a function "name" exists.
25045 */
25046 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025047function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025048{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025049 char_u *nm = name;
25050 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025051 int n = FALSE;
25052
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025053 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025054 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025055 nm = skipwhite(nm);
25056
25057 /* Only accept "funcname", "funcname ", "funcname (..." and
25058 * "funcname(...", not "funcname!...". */
25059 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025060 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025061 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025062 return n;
25063}
25064
Bram Moolenaara1544c02013-05-30 12:35:52 +020025065 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025066get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025067{
25068 char_u *nm = name;
25069 char_u *p;
25070
Bram Moolenaar65639032016-03-16 21:40:30 +010025071 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025072
25073 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025074 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025075 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025076
Bram Moolenaara1544c02013-05-30 12:35:52 +020025077 vim_free(p);
25078 return NULL;
25079}
25080
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025081/*
25082 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025083 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25084 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025085 */
25086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025087builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025088{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025089 char_u *p;
25090
25091 if (!ASCII_ISLOWER(name[0]))
25092 return FALSE;
25093 p = vim_strchr(name, AUTOLOAD_CHAR);
25094 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025095}
25096
Bram Moolenaar05159a02005-02-26 23:04:13 +000025097#if defined(FEAT_PROFILE) || defined(PROTO)
25098/*
25099 * Start profiling function "fp".
25100 */
25101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025102func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025103{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025104 int len = fp->uf_lines.ga_len;
25105
25106 if (len == 0)
25107 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025108 fp->uf_tm_count = 0;
25109 profile_zero(&fp->uf_tm_self);
25110 profile_zero(&fp->uf_tm_total);
25111 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025112 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025113 if (fp->uf_tml_total == NULL)
25114 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025115 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025116 if (fp->uf_tml_self == NULL)
25117 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025118 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 fp->uf_tml_idx = -1;
25120 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25121 || fp->uf_tml_self == NULL)
25122 return; /* out of memory */
25123
25124 fp->uf_profiling = TRUE;
25125}
25126
25127/*
25128 * Dump the profiling results for all functions in file "fd".
25129 */
25130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025131func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025132{
25133 hashitem_T *hi;
25134 int todo;
25135 ufunc_T *fp;
25136 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025137 ufunc_T **sorttab;
25138 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025140 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025141 if (todo == 0)
25142 return; /* nothing to dump */
25143
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025144 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025145
Bram Moolenaar05159a02005-02-26 23:04:13 +000025146 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25147 {
25148 if (!HASHITEM_EMPTY(hi))
25149 {
25150 --todo;
25151 fp = HI2UF(hi);
25152 if (fp->uf_profiling)
25153 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025154 if (sorttab != NULL)
25155 sorttab[st_len++] = fp;
25156
Bram Moolenaar05159a02005-02-26 23:04:13 +000025157 if (fp->uf_name[0] == K_SPECIAL)
25158 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25159 else
25160 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25161 if (fp->uf_tm_count == 1)
25162 fprintf(fd, "Called 1 time\n");
25163 else
25164 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25165 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25166 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25167 fprintf(fd, "\n");
25168 fprintf(fd, "count total (s) self (s)\n");
25169
25170 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25171 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025172 if (FUNCLINE(fp, i) == NULL)
25173 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025174 prof_func_line(fd, fp->uf_tml_count[i],
25175 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025176 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25177 }
25178 fprintf(fd, "\n");
25179 }
25180 }
25181 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025182
25183 if (sorttab != NULL && st_len > 0)
25184 {
25185 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25186 prof_total_cmp);
25187 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25188 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25189 prof_self_cmp);
25190 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25191 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025192
25193 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025194}
Bram Moolenaar73830342005-02-28 22:48:19 +000025195
25196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025197prof_sort_list(
25198 FILE *fd,
25199 ufunc_T **sorttab,
25200 int st_len,
25201 char *title,
25202 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025203{
25204 int i;
25205 ufunc_T *fp;
25206
25207 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25208 fprintf(fd, "count total (s) self (s) function\n");
25209 for (i = 0; i < 20 && i < st_len; ++i)
25210 {
25211 fp = sorttab[i];
25212 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25213 prefer_self);
25214 if (fp->uf_name[0] == K_SPECIAL)
25215 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25216 else
25217 fprintf(fd, " %s()\n", fp->uf_name);
25218 }
25219 fprintf(fd, "\n");
25220}
25221
25222/*
25223 * Print the count and times for one function or function line.
25224 */
25225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025226prof_func_line(
25227 FILE *fd,
25228 int count,
25229 proftime_T *total,
25230 proftime_T *self,
25231 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025232{
25233 if (count > 0)
25234 {
25235 fprintf(fd, "%5d ", count);
25236 if (prefer_self && profile_equal(total, self))
25237 fprintf(fd, " ");
25238 else
25239 fprintf(fd, "%s ", profile_msg(total));
25240 if (!prefer_self && profile_equal(total, self))
25241 fprintf(fd, " ");
25242 else
25243 fprintf(fd, "%s ", profile_msg(self));
25244 }
25245 else
25246 fprintf(fd, " ");
25247}
25248
25249/*
25250 * Compare function for total time sorting.
25251 */
25252 static int
25253#ifdef __BORLANDC__
25254_RTLENTRYF
25255#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025256prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025257{
25258 ufunc_T *p1, *p2;
25259
25260 p1 = *(ufunc_T **)s1;
25261 p2 = *(ufunc_T **)s2;
25262 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25263}
25264
25265/*
25266 * Compare function for self time sorting.
25267 */
25268 static int
25269#ifdef __BORLANDC__
25270_RTLENTRYF
25271#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025272prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025273{
25274 ufunc_T *p1, *p2;
25275
25276 p1 = *(ufunc_T **)s1;
25277 p2 = *(ufunc_T **)s2;
25278 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25279}
25280
Bram Moolenaar05159a02005-02-26 23:04:13 +000025281#endif
25282
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025283/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025284 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025285 * Return TRUE if a package was loaded.
25286 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025288script_autoload(
25289 char_u *name,
25290 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025291{
25292 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025293 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025294 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025295 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025296
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025297 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025298 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025299 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025300 return FALSE;
25301
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025302 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025303
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025304 /* Find the name in the list of previously loaded package names. Skip
25305 * "autoload/", it's always the same. */
25306 for (i = 0; i < ga_loaded.ga_len; ++i)
25307 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25308 break;
25309 if (!reload && i < ga_loaded.ga_len)
25310 ret = FALSE; /* was loaded already */
25311 else
25312 {
25313 /* Remember the name if it wasn't loaded already. */
25314 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25315 {
25316 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25317 tofree = NULL;
25318 }
25319
25320 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025321 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025322 ret = TRUE;
25323 }
25324
25325 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025326 return ret;
25327}
25328
25329/*
25330 * Return the autoload script name for a function or variable name.
25331 * Returns NULL when out of memory.
25332 */
25333 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025334autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025335{
25336 char_u *p;
25337 char_u *scriptname;
25338
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025339 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025340 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25341 if (scriptname == NULL)
25342 return FALSE;
25343 STRCPY(scriptname, "autoload/");
25344 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025345 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025346 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025347 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025348 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025349 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025350}
25351
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25353
25354/*
25355 * Function given to ExpandGeneric() to obtain the list of user defined
25356 * function names.
25357 */
25358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025359get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025361 static long_u done;
25362 static hashitem_T *hi;
25363 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364
25365 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025367 done = 0;
25368 hi = func_hashtab.ht_array;
25369 }
25370 if (done < func_hashtab.ht_used)
25371 {
25372 if (done++ > 0)
25373 ++hi;
25374 while (HASHITEM_EMPTY(hi))
25375 ++hi;
25376 fp = HI2UF(hi);
25377
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025378 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025379 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025380
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025381 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25382 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383
25384 cat_func_name(IObuff, fp);
25385 if (xp->xp_context != EXPAND_USER_FUNC)
25386 {
25387 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025388 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 STRCAT(IObuff, ")");
25390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 return IObuff;
25392 }
25393 return NULL;
25394}
25395
25396#endif /* FEAT_CMDL_COMPL */
25397
25398/*
25399 * Copy the function name of "fp" to buffer "buf".
25400 * "buf" must be able to hold the function name plus three bytes.
25401 * Takes care of script-local function names.
25402 */
25403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025404cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025406 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 {
25408 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025409 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 }
25411 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025412 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413}
25414
25415/*
25416 * ":delfunction {name}"
25417 */
25418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025419ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025421 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422 char_u *p;
25423 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025424 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425
25426 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025427 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025428 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025430 {
25431 if (fudi.fd_dict != NULL && !eap->skip)
25432 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 if (!ends_excmd(*skipwhite(p)))
25436 {
25437 vim_free(name);
25438 EMSG(_(e_trailing));
25439 return;
25440 }
25441 eap->nextcmd = check_nextcmd(p);
25442 if (eap->nextcmd != NULL)
25443 *p = NUL;
25444
25445 if (!eap->skip)
25446 fp = find_func(name);
25447 vim_free(name);
25448
25449 if (!eap->skip)
25450 {
25451 if (fp == NULL)
25452 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025453 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 return;
25455 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025456 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 {
25458 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25459 return;
25460 }
25461
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025462 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025464 /* Delete the dict item that refers to the function, it will
25465 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025466 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025468 else
25469 func_free(fp);
25470 }
25471}
25472
25473/*
25474 * Free a function and remove it from the list of functions.
25475 */
25476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025477func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025478{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025479 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025480
25481 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025482 ga_clear_strings(&(fp->uf_args));
25483 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025484#ifdef FEAT_PROFILE
25485 vim_free(fp->uf_tml_count);
25486 vim_free(fp->uf_tml_total);
25487 vim_free(fp->uf_tml_self);
25488#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025490 /* remove the function from the function hashtable */
25491 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25492 if (HASHITEM_EMPTY(hi))
25493 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025494 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025495 hash_remove(&func_hashtab, hi);
25496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025497 vim_free(fp);
25498}
25499
25500/*
25501 * Unreference a Function: decrement the reference count and free it when it
25502 * becomes zero. Only for numbered functions.
25503 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025505func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025506{
25507 ufunc_T *fp;
25508
25509 if (name != NULL && isdigit(*name))
25510 {
25511 fp = find_func(name);
25512 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025513 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025514#ifdef EXITFREE
25515 if (!entered_free_all_mem)
25516#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025517 EMSG2(_(e_intern2), "func_unref()");
25518 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025519 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025520 {
25521 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025522 * when "uf_calls" becomes zero. */
25523 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025524 func_free(fp);
25525 }
25526 }
25527}
25528
25529/*
25530 * Count a reference to a Function.
25531 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025533func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025534{
25535 ufunc_T *fp;
25536
25537 if (name != NULL && isdigit(*name))
25538 {
25539 fp = find_func(name);
25540 if (fp == NULL)
25541 EMSG2(_(e_intern2), "func_ref()");
25542 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025543 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 }
25545}
25546
25547/*
25548 * Call a user function.
25549 */
25550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025551call_user_func(
25552 ufunc_T *fp, /* pointer to function */
25553 int argcount, /* nr of args */
25554 typval_T *argvars, /* arguments */
25555 typval_T *rettv, /* return value */
25556 linenr_T firstline, /* first line of range */
25557 linenr_T lastline, /* last line of range */
25558 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559{
Bram Moolenaar33570922005-01-25 22:26:29 +000025560 char_u *save_sourcing_name;
25561 linenr_T save_sourcing_lnum;
25562 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025564 int save_did_emsg;
25565 static int depth = 0;
25566 dictitem_T *v;
25567 int fixvar_idx = 0; /* index in fixvar[] */
25568 int i;
25569 int ai;
25570 char_u numbuf[NUMBUFLEN];
25571 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025572 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025573#ifdef FEAT_PROFILE
25574 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025575 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577
25578 /* If depth of calling is getting too high, don't execute the function */
25579 if (depth >= p_mfd)
25580 {
25581 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025582 rettv->v_type = VAR_NUMBER;
25583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 return;
25585 }
25586 ++depth;
25587
25588 line_breakcheck(); /* check for CTRL-C hit */
25589
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025590 fc = (funccall_T *)alloc(sizeof(funccall_T));
25591 fc->caller = current_funccal;
25592 current_funccal = fc;
25593 fc->func = fp;
25594 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025595 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596 fc->linenr = 0;
25597 fc->returned = FALSE;
25598 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025600 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25601 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602
Bram Moolenaar33570922005-01-25 22:26:29 +000025603 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025604 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025605 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25606 * each argument variable and saves a lot of time.
25607 */
25608 /*
25609 * Init l: variables.
25610 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025611 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025612 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025613 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025614 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25615 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025616 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025617 name = v->di_key;
25618 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025619 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025620 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025621 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025622 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025623 v->di_tv.vval.v_dict = selfdict;
25624 ++selfdict->dv_refcount;
25625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025626
Bram Moolenaar33570922005-01-25 22:26:29 +000025627 /*
25628 * Init a: variables.
25629 * Set a:0 to "argcount".
25630 * Set a:000 to a list with room for the "..." arguments.
25631 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025632 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025633 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025634 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025635 /* Use "name" to avoid a warning from some compiler that checks the
25636 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025637 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025638 name = v->di_key;
25639 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025640 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025641 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025642 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025643 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025644 v->di_tv.vval.v_list = &fc->l_varlist;
25645 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25646 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25647 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025648
25649 /*
25650 * Set a:firstline to "firstline" and a:lastline to "lastline".
25651 * Set a:name to named arguments.
25652 * Set a:N to the "..." arguments.
25653 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025654 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025655 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025656 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025657 (varnumber_T)lastline);
25658 for (i = 0; i < argcount; ++i)
25659 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025660 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025661 if (ai < 0)
25662 /* named argument a:name */
25663 name = FUNCARG(fp, i);
25664 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025666 /* "..." argument a:1, a:2, etc. */
25667 sprintf((char *)numbuf, "%d", ai + 1);
25668 name = numbuf;
25669 }
25670 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25671 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025672 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025673 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25674 }
25675 else
25676 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025677 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25678 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025679 if (v == NULL)
25680 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025681 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025682 }
25683 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025684 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025685
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025686 /* Note: the values are copied directly to avoid alloc/free.
25687 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025688 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025689 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025690
25691 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25692 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025693 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25694 fc->l_listitems[ai].li_tv = argvars[i];
25695 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025696 }
25697 }
25698
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 /* Don't redraw while executing the function. */
25700 ++RedrawingDisabled;
25701 save_sourcing_name = sourcing_name;
25702 save_sourcing_lnum = sourcing_lnum;
25703 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025704 /* need space for function name + ("function " + 3) or "[number]" */
25705 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25706 + STRLEN(fp->uf_name) + 20;
25707 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708 if (sourcing_name != NULL)
25709 {
25710 if (save_sourcing_name != NULL
25711 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025712 sprintf((char *)sourcing_name, "%s[%d]..",
25713 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025714 else
25715 STRCPY(sourcing_name, "function ");
25716 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25717
25718 if (p_verbose >= 12)
25719 {
25720 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025721 verbose_enter_scroll();
25722
Bram Moolenaar555b2802005-05-19 21:08:39 +000025723 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724 if (p_verbose >= 14)
25725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025727 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025728 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025729 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730
25731 msg_puts((char_u *)"(");
25732 for (i = 0; i < argcount; ++i)
25733 {
25734 if (i > 0)
25735 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025736 if (argvars[i].v_type == VAR_NUMBER)
25737 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 else
25739 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025740 /* Do not want errors such as E724 here. */
25741 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025742 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025743 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025744 if (s != NULL)
25745 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025746 if (vim_strsize(s) > MSG_BUF_CLEN)
25747 {
25748 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25749 s = buf;
25750 }
25751 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025752 vim_free(tofree);
25753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 }
25755 }
25756 msg_puts((char_u *)")");
25757 }
25758 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025759
25760 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 --no_wait_return;
25762 }
25763 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025764#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025765 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025766 {
25767 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25768 func_do_profile(fp);
25769 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025770 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025771 {
25772 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025773 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025774 profile_zero(&fp->uf_tm_children);
25775 }
25776 script_prof_save(&wait_start);
25777 }
25778#endif
25779
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025781 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782 save_did_emsg = did_emsg;
25783 did_emsg = FALSE;
25784
25785 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025786 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25788
25789 --RedrawingDisabled;
25790
25791 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025792 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025794 clear_tv(rettv);
25795 rettv->v_type = VAR_NUMBER;
25796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797 }
25798
Bram Moolenaar05159a02005-02-26 23:04:13 +000025799#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025800 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025801 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025802 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025803 profile_end(&call_start);
25804 profile_sub_wait(&wait_start, &call_start);
25805 profile_add(&fp->uf_tm_total, &call_start);
25806 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025807 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025808 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025809 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25810 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025811 }
25812 }
25813#endif
25814
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815 /* when being verbose, mention the return value */
25816 if (p_verbose >= 12)
25817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025819 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025822 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025823 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025824 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025825 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025828 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025829 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025830 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025831 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025832
Bram Moolenaar555b2802005-05-19 21:08:39 +000025833 /* The value may be very long. Skip the middle part, so that we
25834 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025835 * truncate it at the end. Don't want errors such as E724 here. */
25836 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025837 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025838 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025839 if (s != NULL)
25840 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025841 if (vim_strsize(s) > MSG_BUF_CLEN)
25842 {
25843 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25844 s = buf;
25845 }
25846 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025847 vim_free(tofree);
25848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 }
25850 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025851
25852 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853 --no_wait_return;
25854 }
25855
25856 vim_free(sourcing_name);
25857 sourcing_name = save_sourcing_name;
25858 sourcing_lnum = save_sourcing_lnum;
25859 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025860#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025861 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025862 script_prof_restore(&wait_start);
25863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864
25865 if (p_verbose >= 12 && sourcing_name != NULL)
25866 {
25867 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025868 verbose_enter_scroll();
25869
Bram Moolenaar555b2802005-05-19 21:08:39 +000025870 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025871 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025872
25873 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 --no_wait_return;
25875 }
25876
25877 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025878 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025881 /* If the a:000 list and the l: and a: dicts are not referenced we can
25882 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025883 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25884 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25885 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25886 {
25887 free_funccal(fc, FALSE);
25888 }
25889 else
25890 {
25891 hashitem_T *hi;
25892 listitem_T *li;
25893 int todo;
25894
25895 /* "fc" is still in use. This can happen when returning "a:000" or
25896 * assigning "l:" to a global variable.
25897 * Link "fc" in the list for garbage collection later. */
25898 fc->caller = previous_funccal;
25899 previous_funccal = fc;
25900
25901 /* Make a copy of the a: variables, since we didn't do that above. */
25902 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25903 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25904 {
25905 if (!HASHITEM_EMPTY(hi))
25906 {
25907 --todo;
25908 v = HI2DI(hi);
25909 copy_tv(&v->di_tv, &v->di_tv);
25910 }
25911 }
25912
25913 /* Make a copy of the a:000 items, since we didn't do that above. */
25914 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25915 copy_tv(&li->li_tv, &li->li_tv);
25916 }
25917}
25918
25919/*
25920 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025921 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025922 */
25923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025925{
25926 return (fc->l_varlist.lv_copyID != copyID
25927 && fc->l_vars.dv_copyID != copyID
25928 && fc->l_avars.dv_copyID != copyID);
25929}
25930
25931/*
25932 * Free "fc" and what it contains.
25933 */
25934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025935free_funccal(
25936 funccall_T *fc,
25937 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025938{
25939 listitem_T *li;
25940
25941 /* The a: variables typevals may not have been allocated, only free the
25942 * allocated variables. */
25943 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25944
25945 /* free all l: variables */
25946 vars_clear(&fc->l_vars.dv_hashtab);
25947
25948 /* Free the a:000 variables if they were allocated. */
25949 if (free_val)
25950 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25951 clear_tv(&li->li_tv);
25952
25953 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954}
25955
25956/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025957 * Add a number variable "name" to dict "dp" with value "nr".
25958 */
25959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025960add_nr_var(
25961 dict_T *dp,
25962 dictitem_T *v,
25963 char *name,
25964 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025965{
25966 STRCPY(v->di_key, name);
25967 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25968 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25969 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025970 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025971 v->di_tv.vval.v_number = nr;
25972}
25973
25974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975 * ":return [expr]"
25976 */
25977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025978ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979{
25980 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025981 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982 int returning = FALSE;
25983
25984 if (current_funccal == NULL)
25985 {
25986 EMSG(_("E133: :return not inside a function"));
25987 return;
25988 }
25989
25990 if (eap->skip)
25991 ++emsg_skip;
25992
25993 eap->nextcmd = NULL;
25994 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025995 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 {
25997 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025998 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026000 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 }
26002 /* It's safer to return also on error. */
26003 else if (!eap->skip)
26004 {
26005 /*
26006 * Return unless the expression evaluation has been cancelled due to an
26007 * aborting error, an interrupt, or an exception.
26008 */
26009 if (!aborting())
26010 returning = do_return(eap, FALSE, TRUE, NULL);
26011 }
26012
26013 /* When skipping or the return gets pending, advance to the next command
26014 * in this line (!returning). Otherwise, ignore the rest of the line.
26015 * Following lines will be ignored by get_func_line(). */
26016 if (returning)
26017 eap->nextcmd = NULL;
26018 else if (eap->nextcmd == NULL) /* no argument */
26019 eap->nextcmd = check_nextcmd(arg);
26020
26021 if (eap->skip)
26022 --emsg_skip;
26023}
26024
26025/*
26026 * Return from a function. Possibly makes the return pending. Also called
26027 * for a pending return at the ":endtry" or after returning from an extra
26028 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026029 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026030 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 * FALSE when the return gets pending.
26032 */
26033 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026034do_return(
26035 exarg_T *eap,
26036 int reanimate,
26037 int is_cmd,
26038 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039{
26040 int idx;
26041 struct condstack *cstack = eap->cstack;
26042
26043 if (reanimate)
26044 /* Undo the return. */
26045 current_funccal->returned = FALSE;
26046
26047 /*
26048 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26049 * not in its finally clause (which then is to be executed next) is found.
26050 * In this case, make the ":return" pending for execution at the ":endtry".
26051 * Otherwise, return normally.
26052 */
26053 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26054 if (idx >= 0)
26055 {
26056 cstack->cs_pending[idx] = CSTP_RETURN;
26057
26058 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026059 /* A pending return again gets pending. "rettv" points to an
26060 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026062 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 else
26064 {
26065 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026066 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026067 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026068 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026070 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071 {
26072 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026073 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026074 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075 else
26076 EMSG(_(e_outofmem));
26077 }
26078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026079 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080
26081 if (reanimate)
26082 {
26083 /* The pending return value could be overwritten by a ":return"
26084 * without argument in a finally clause; reset the default
26085 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026086 current_funccal->rettv->v_type = VAR_NUMBER;
26087 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088 }
26089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026090 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091 }
26092 else
26093 {
26094 current_funccal->returned = TRUE;
26095
26096 /* If the return is carried out now, store the return value. For
26097 * a return immediately after reanimation, the value is already
26098 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026099 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026101 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026102 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026104 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105 }
26106 }
26107
26108 return idx < 0;
26109}
26110
26111/*
26112 * Free the variable with a pending return value.
26113 */
26114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026115discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116{
Bram Moolenaar33570922005-01-25 22:26:29 +000026117 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118}
26119
26120/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026121 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122 * is an allocated string. Used by report_pending() for verbose messages.
26123 */
26124 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026125get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026127 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026128 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026129 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026131 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026132 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026133 if (s == NULL)
26134 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026135
26136 STRCPY(IObuff, ":return ");
26137 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26138 if (STRLEN(s) + 8 >= IOSIZE)
26139 STRCPY(IObuff + IOSIZE - 4, "...");
26140 vim_free(tofree);
26141 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142}
26143
26144/*
26145 * Get next function line.
26146 * Called by do_cmdline() to get the next line.
26147 * Returns allocated string, or NULL for end of function.
26148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026149 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026150get_func_line(
26151 int c UNUSED,
26152 void *cookie,
26153 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154{
Bram Moolenaar33570922005-01-25 22:26:29 +000026155 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026156 ufunc_T *fp = fcp->func;
26157 char_u *retval;
26158 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026159
26160 /* If breakpoints have been added/deleted need to check for it. */
26161 if (fcp->dbg_tick != debug_tick)
26162 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026163 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 sourcing_lnum);
26165 fcp->dbg_tick = debug_tick;
26166 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026167#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026168 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026169 func_line_end(cookie);
26170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171
Bram Moolenaar05159a02005-02-26 23:04:13 +000026172 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026173 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26174 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175 retval = NULL;
26176 else
26177 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026178 /* Skip NULL lines (continuation lines). */
26179 while (fcp->linenr < gap->ga_len
26180 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26181 ++fcp->linenr;
26182 if (fcp->linenr >= gap->ga_len)
26183 retval = NULL;
26184 else
26185 {
26186 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26187 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026188#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026189 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026190 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026191#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193 }
26194
26195 /* Did we encounter a breakpoint? */
26196 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26197 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026198 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026199 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026200 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 sourcing_lnum);
26202 fcp->dbg_tick = debug_tick;
26203 }
26204
26205 return retval;
26206}
26207
Bram Moolenaar05159a02005-02-26 23:04:13 +000026208#if defined(FEAT_PROFILE) || defined(PROTO)
26209/*
26210 * Called when starting to read a function line.
26211 * "sourcing_lnum" must be correct!
26212 * When skipping lines it may not actually be executed, but we won't find out
26213 * until later and we need to store the time now.
26214 */
26215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026216func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026217{
26218 funccall_T *fcp = (funccall_T *)cookie;
26219 ufunc_T *fp = fcp->func;
26220
26221 if (fp->uf_profiling && sourcing_lnum >= 1
26222 && sourcing_lnum <= fp->uf_lines.ga_len)
26223 {
26224 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026225 /* Skip continuation lines. */
26226 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26227 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026228 fp->uf_tml_execed = FALSE;
26229 profile_start(&fp->uf_tml_start);
26230 profile_zero(&fp->uf_tml_children);
26231 profile_get_wait(&fp->uf_tml_wait);
26232 }
26233}
26234
26235/*
26236 * Called when actually executing a function line.
26237 */
26238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026239func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026240{
26241 funccall_T *fcp = (funccall_T *)cookie;
26242 ufunc_T *fp = fcp->func;
26243
26244 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26245 fp->uf_tml_execed = TRUE;
26246}
26247
26248/*
26249 * Called when done with a function line.
26250 */
26251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026252func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026253{
26254 funccall_T *fcp = (funccall_T *)cookie;
26255 ufunc_T *fp = fcp->func;
26256
26257 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26258 {
26259 if (fp->uf_tml_execed)
26260 {
26261 ++fp->uf_tml_count[fp->uf_tml_idx];
26262 profile_end(&fp->uf_tml_start);
26263 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026264 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026265 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26266 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026267 }
26268 fp->uf_tml_idx = -1;
26269 }
26270}
26271#endif
26272
Bram Moolenaar071d4272004-06-13 20:20:40 +000026273/*
26274 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026275 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276 */
26277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026278func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026279{
Bram Moolenaar33570922005-01-25 22:26:29 +000026280 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281
26282 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26283 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026284 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285 || fcp->returned);
26286}
26287
26288/*
26289 * return TRUE if cookie indicates a function which "abort"s on errors.
26290 */
26291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026292func_has_abort(
26293 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026295 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296}
26297
26298#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26299typedef enum
26300{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026301 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26302 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26303 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026304} var_flavour_T;
26305
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026306static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307
26308 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026309var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310{
26311 char_u *p = varname;
26312
26313 if (ASCII_ISUPPER(*p))
26314 {
26315 while (*(++p))
26316 if (ASCII_ISLOWER(*p))
26317 return VAR_FLAVOUR_SESSION;
26318 return VAR_FLAVOUR_VIMINFO;
26319 }
26320 else
26321 return VAR_FLAVOUR_DEFAULT;
26322}
26323#endif
26324
26325#if defined(FEAT_VIMINFO) || defined(PROTO)
26326/*
26327 * Restore global vars that start with a capital from the viminfo file
26328 */
26329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026330read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331{
26332 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026333 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026334 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026335 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336
26337 if (!writing && (find_viminfo_parameter('!') != NULL))
26338 {
26339 tab = vim_strchr(virp->vir_line + 1, '\t');
26340 if (tab != NULL)
26341 {
26342 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026343 switch (*tab)
26344 {
26345 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026346#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026347 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026348#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026349 case 'D': type = VAR_DICT; break;
26350 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026351 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
26354 tab = vim_strchr(tab, '\t');
26355 if (tab != NULL)
26356 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026357 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026358 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026359 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026361#ifdef FEAT_FLOAT
26362 else if (type == VAR_FLOAT)
26363 (void)string2float(tab + 1, &tv.vval.v_float);
26364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026366 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026367 if (type == VAR_DICT || type == VAR_LIST)
26368 {
26369 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26370
26371 if (etv == NULL)
26372 /* Failed to parse back the dict or list, use it as a
26373 * string. */
26374 tv.v_type = VAR_STRING;
26375 else
26376 {
26377 vim_free(tv.vval.v_string);
26378 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026379 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026380 }
26381 }
26382
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026383 /* when in a function use global variables */
26384 save_funccal = current_funccal;
26385 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026386 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026387 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026388
26389 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026390 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026391 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26392 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026393 }
26394 }
26395 }
26396
26397 return viminfo_readline(virp);
26398}
26399
26400/*
26401 * Write global vars that start with a capital to the viminfo file
26402 */
26403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026404write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405{
Bram Moolenaar33570922005-01-25 22:26:29 +000026406 hashitem_T *hi;
26407 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026408 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026409 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026410 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026411 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413
26414 if (find_viminfo_parameter('!') == NULL)
26415 return;
26416
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026417 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026418
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026419 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026420 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026422 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026424 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026425 this_var = HI2DI(hi);
26426 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026427 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026428 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026429 {
26430 case VAR_STRING: s = "STR"; break;
26431 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026432 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026433 case VAR_DICT: s = "DIC"; break;
26434 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026435 case VAR_SPECIAL: s = "XPL"; break;
26436
26437 case VAR_UNKNOWN:
26438 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026439 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026440 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026441 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026442 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026443 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026444 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026445 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026446 if (p != NULL)
26447 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026448 vim_free(tofree);
26449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450 }
26451 }
26452}
26453#endif
26454
26455#if defined(FEAT_SESSION) || defined(PROTO)
26456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026457store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026458{
Bram Moolenaar33570922005-01-25 22:26:29 +000026459 hashitem_T *hi;
26460 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026461 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026462 char_u *p, *t;
26463
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026464 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026465 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026467 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026469 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026470 this_var = HI2DI(hi);
26471 if ((this_var->di_tv.v_type == VAR_NUMBER
26472 || this_var->di_tv.v_type == VAR_STRING)
26473 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026474 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026475 /* Escape special characters with a backslash. Turn a LF and
26476 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026477 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026478 (char_u *)"\\\"\n\r");
26479 if (p == NULL) /* out of memory */
26480 break;
26481 for (t = p; *t != NUL; ++t)
26482 if (*t == '\n')
26483 *t = 'n';
26484 else if (*t == '\r')
26485 *t = 'r';
26486 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026487 this_var->di_key,
26488 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26489 : ' ',
26490 p,
26491 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26492 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026493 || put_eol(fd) == FAIL)
26494 {
26495 vim_free(p);
26496 return FAIL;
26497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026498 vim_free(p);
26499 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026500#ifdef FEAT_FLOAT
26501 else if (this_var->di_tv.v_type == VAR_FLOAT
26502 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26503 {
26504 float_T f = this_var->di_tv.vval.v_float;
26505 int sign = ' ';
26506
26507 if (f < 0)
26508 {
26509 f = -f;
26510 sign = '-';
26511 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026512 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026513 this_var->di_key, sign, f) < 0)
26514 || put_eol(fd) == FAIL)
26515 return FAIL;
26516 }
26517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518 }
26519 }
26520 return OK;
26521}
26522#endif
26523
Bram Moolenaar661b1822005-07-28 22:36:45 +000026524/*
26525 * Display script name where an item was last set.
26526 * Should only be invoked when 'verbose' is non-zero.
26527 */
26528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026529last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026530{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026531 char_u *p;
26532
Bram Moolenaar661b1822005-07-28 22:36:45 +000026533 if (scriptID != 0)
26534 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026535 p = home_replace_save(NULL, get_scriptname(scriptID));
26536 if (p != NULL)
26537 {
26538 verbose_enter();
26539 MSG_PUTS(_("\n\tLast set from "));
26540 MSG_PUTS(p);
26541 vim_free(p);
26542 verbose_leave();
26543 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026544 }
26545}
26546
Bram Moolenaard812df62008-11-09 12:46:09 +000026547/*
26548 * List v:oldfiles in a nice way.
26549 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026551ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026552{
26553 list_T *l = vimvars[VV_OLDFILES].vv_list;
26554 listitem_T *li;
26555 int nr = 0;
26556
26557 if (l == NULL)
26558 msg((char_u *)_("No old files"));
26559 else
26560 {
26561 msg_start();
26562 msg_scroll = TRUE;
26563 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26564 {
26565 msg_outnum((long)++nr);
26566 MSG_PUTS(": ");
26567 msg_outtrans(get_tv_string(&li->li_tv));
26568 msg_putchar('\n');
26569 out_flush(); /* output one line at a time */
26570 ui_breakcheck();
26571 }
26572 /* Assume "got_int" was set to truncate the listing. */
26573 got_int = FALSE;
26574
26575#ifdef FEAT_BROWSE_CMD
26576 if (cmdmod.browse)
26577 {
26578 quit_more = FALSE;
26579 nr = prompt_for_number(FALSE);
26580 msg_starthere();
26581 if (nr > 0)
26582 {
26583 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26584 (long)nr);
26585
26586 if (p != NULL)
26587 {
26588 p = expand_env_save(p);
26589 eap->arg = p;
26590 eap->cmdidx = CMD_edit;
26591 cmdmod.browse = FALSE;
26592 do_exedit(eap, NULL);
26593 vim_free(p);
26594 }
26595 }
26596 }
26597#endif
26598 }
26599}
26600
Bram Moolenaar53744302015-07-17 17:38:22 +020026601/* reset v:option_new, v:option_old and v:option_type */
26602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026603reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026604{
26605 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26606 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26607 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26608}
26609
26610
Bram Moolenaar071d4272004-06-13 20:20:40 +000026611#endif /* FEAT_EVAL */
26612
Bram Moolenaar071d4272004-06-13 20:20:40 +000026613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026614#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026615
26616#ifdef WIN3264
26617/*
26618 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26619 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026620static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26621static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26622static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623
26624/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026625 * Get the short path (8.3) for the filename in "fnamep".
26626 * Only works for a valid file name.
26627 * When the path gets longer "fnamep" is changed and the allocated buffer
26628 * is put in "bufp".
26629 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26630 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026631 */
26632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026633get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026634{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026635 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026636 char_u *newbuf;
26637
26638 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026639 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026640 if (l > len - 1)
26641 {
26642 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026643 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026644 newbuf = vim_strnsave(*fnamep, l);
26645 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026646 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026647
26648 vim_free(*bufp);
26649 *fnamep = *bufp = newbuf;
26650
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026651 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026652 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026653 }
26654
26655 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026656 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657}
26658
26659/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026660 * Get the short path (8.3) for the filename in "fname". The converted
26661 * path is returned in "bufp".
26662 *
26663 * Some of the directories specified in "fname" may not exist. This function
26664 * will shorten the existing directories at the beginning of the path and then
26665 * append the remaining non-existing path.
26666 *
26667 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026668 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026669 * bufp - Pointer to an allocated buffer for the filename.
26670 * fnamelen - Length of the filename pointed to by fname
26671 *
26672 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026673 */
26674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026675shortpath_for_invalid_fname(
26676 char_u **fname,
26677 char_u **bufp,
26678 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026680 char_u *short_fname, *save_fname, *pbuf_unused;
26681 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026682 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026683 int old_len, len;
26684 int new_len, sfx_len;
26685 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026686
26687 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026688 old_len = *fnamelen;
26689 save_fname = vim_strnsave(*fname, old_len);
26690 pbuf_unused = NULL;
26691 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026693 endp = save_fname + old_len - 1; /* Find the end of the copy */
26694 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026695
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026696 /*
26697 * Try shortening the supplied path till it succeeds by removing one
26698 * directory at a time from the tail of the path.
26699 */
26700 len = 0;
26701 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026702 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026703 /* go back one path-separator */
26704 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26705 --endp;
26706 if (endp <= save_fname)
26707 break; /* processed the complete path */
26708
26709 /*
26710 * Replace the path separator with a NUL and try to shorten the
26711 * resulting path.
26712 */
26713 ch = *endp;
26714 *endp = 0;
26715 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026716 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026717 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26718 {
26719 retval = FAIL;
26720 goto theend;
26721 }
26722 *endp = ch; /* preserve the string */
26723
26724 if (len > 0)
26725 break; /* successfully shortened the path */
26726
26727 /* failed to shorten the path. Skip the path separator */
26728 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026729 }
26730
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026731 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026732 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026733 /*
26734 * Succeeded in shortening the path. Now concatenate the shortened
26735 * path with the remaining path at the tail.
26736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026737
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026738 /* Compute the length of the new path. */
26739 sfx_len = (int)(save_endp - endp) + 1;
26740 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026742 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026744 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026745 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026746 /* There is not enough space in the currently allocated string,
26747 * copy it to a buffer big enough. */
26748 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026749 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026750 {
26751 retval = FAIL;
26752 goto theend;
26753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 }
26755 else
26756 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026757 /* Transfer short_fname to the main buffer (it's big enough),
26758 * unless get_short_pathname() did its work in-place. */
26759 *fname = *bufp = save_fname;
26760 if (short_fname != save_fname)
26761 vim_strncpy(save_fname, short_fname, len);
26762 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026763 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764
26765 /* concat the not-shortened part of the path */
26766 vim_strncpy(*fname + len, endp, sfx_len);
26767 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026769
26770theend:
26771 vim_free(pbuf_unused);
26772 vim_free(save_fname);
26773
26774 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775}
26776
26777/*
26778 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026779 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026780 */
26781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026782shortpath_for_partial(
26783 char_u **fnamep,
26784 char_u **bufp,
26785 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026786{
26787 int sepcount, len, tflen;
26788 char_u *p;
26789 char_u *pbuf, *tfname;
26790 int hasTilde;
26791
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026792 /* Count up the path separators from the RHS.. so we know which part
26793 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026795 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026796 if (vim_ispathsep(*p))
26797 ++sepcount;
26798
26799 /* Need full path first (use expand_env() to remove a "~/") */
26800 hasTilde = (**fnamep == '~');
26801 if (hasTilde)
26802 pbuf = tfname = expand_env_save(*fnamep);
26803 else
26804 pbuf = tfname = FullName_save(*fnamep, FALSE);
26805
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026806 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026807
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026808 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026810
26811 if (len == 0)
26812 {
26813 /* Don't have a valid filename, so shorten the rest of the
26814 * path if we can. This CAN give us invalid 8.3 filenames, but
26815 * there's not a lot of point in guessing what it might be.
26816 */
26817 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026818 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26819 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026820 }
26821
26822 /* Count the paths backward to find the beginning of the desired string. */
26823 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026824 {
26825#ifdef FEAT_MBYTE
26826 if (has_mbyte)
26827 p -= mb_head_off(tfname, p);
26828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026829 if (vim_ispathsep(*p))
26830 {
26831 if (sepcount == 0 || (hasTilde && sepcount == 1))
26832 break;
26833 else
26834 sepcount --;
26835 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026837 if (hasTilde)
26838 {
26839 --p;
26840 if (p >= tfname)
26841 *p = '~';
26842 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026844 }
26845 else
26846 ++p;
26847
26848 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26849 vim_free(*bufp);
26850 *fnamelen = (int)STRLEN(p);
26851 *bufp = pbuf;
26852 *fnamep = p;
26853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026855}
26856#endif /* WIN3264 */
26857
26858/*
26859 * Adjust a filename, according to a string of modifiers.
26860 * *fnamep must be NUL terminated when called. When returning, the length is
26861 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026862 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026863 * When there is an error, *fnamep is set to NULL.
26864 */
26865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026866modify_fname(
26867 char_u *src, /* string with modifiers */
26868 int *usedlen, /* characters after src that are used */
26869 char_u **fnamep, /* file name so far */
26870 char_u **bufp, /* buffer for allocated file name or NULL */
26871 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026872{
26873 int valid = 0;
26874 char_u *tail;
26875 char_u *s, *p, *pbuf;
26876 char_u dirname[MAXPATHL];
26877 int c;
26878 int has_fullname = 0;
26879#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026880 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026881 int has_shortname = 0;
26882#endif
26883
26884repeat:
26885 /* ":p" - full path/file_name */
26886 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26887 {
26888 has_fullname = 1;
26889
26890 valid |= VALID_PATH;
26891 *usedlen += 2;
26892
26893 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26894 if ((*fnamep)[0] == '~'
26895#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26896 && ((*fnamep)[1] == '/'
26897# ifdef BACKSLASH_IN_FILENAME
26898 || (*fnamep)[1] == '\\'
26899# endif
26900 || (*fnamep)[1] == NUL)
26901
26902#endif
26903 )
26904 {
26905 *fnamep = expand_env_save(*fnamep);
26906 vim_free(*bufp); /* free any allocated file name */
26907 *bufp = *fnamep;
26908 if (*fnamep == NULL)
26909 return -1;
26910 }
26911
26912 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026913 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026914 {
26915 if (vim_ispathsep(*p)
26916 && p[1] == '.'
26917 && (p[2] == NUL
26918 || vim_ispathsep(p[2])
26919 || (p[2] == '.'
26920 && (p[3] == NUL || vim_ispathsep(p[3])))))
26921 break;
26922 }
26923
26924 /* FullName_save() is slow, don't use it when not needed. */
26925 if (*p != NUL || !vim_isAbsName(*fnamep))
26926 {
26927 *fnamep = FullName_save(*fnamep, *p != NUL);
26928 vim_free(*bufp); /* free any allocated file name */
26929 *bufp = *fnamep;
26930 if (*fnamep == NULL)
26931 return -1;
26932 }
26933
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026934#ifdef WIN3264
26935# if _WIN32_WINNT >= 0x0500
26936 if (vim_strchr(*fnamep, '~') != NULL)
26937 {
26938 /* Expand 8.3 filename to full path. Needed to make sure the same
26939 * file does not have two different names.
26940 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26941 p = alloc(_MAX_PATH + 1);
26942 if (p != NULL)
26943 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026944 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026945 {
26946 vim_free(*bufp);
26947 *bufp = *fnamep = p;
26948 }
26949 else
26950 vim_free(p);
26951 }
26952 }
26953# endif
26954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026955 /* Append a path separator to a directory. */
26956 if (mch_isdir(*fnamep))
26957 {
26958 /* Make room for one or two extra characters. */
26959 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26960 vim_free(*bufp); /* free any allocated file name */
26961 *bufp = *fnamep;
26962 if (*fnamep == NULL)
26963 return -1;
26964 add_pathsep(*fnamep);
26965 }
26966 }
26967
26968 /* ":." - path relative to the current directory */
26969 /* ":~" - path relative to the home directory */
26970 /* ":8" - shortname path - postponed till after */
26971 while (src[*usedlen] == ':'
26972 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26973 {
26974 *usedlen += 2;
26975 if (c == '8')
26976 {
26977#ifdef WIN3264
26978 has_shortname = 1; /* Postpone this. */
26979#endif
26980 continue;
26981 }
26982 pbuf = NULL;
26983 /* Need full path first (use expand_env() to remove a "~/") */
26984 if (!has_fullname)
26985 {
26986 if (c == '.' && **fnamep == '~')
26987 p = pbuf = expand_env_save(*fnamep);
26988 else
26989 p = pbuf = FullName_save(*fnamep, FALSE);
26990 }
26991 else
26992 p = *fnamep;
26993
26994 has_fullname = 0;
26995
26996 if (p != NULL)
26997 {
26998 if (c == '.')
26999 {
27000 mch_dirname(dirname, MAXPATHL);
27001 s = shorten_fname(p, dirname);
27002 if (s != NULL)
27003 {
27004 *fnamep = s;
27005 if (pbuf != NULL)
27006 {
27007 vim_free(*bufp); /* free any allocated file name */
27008 *bufp = pbuf;
27009 pbuf = NULL;
27010 }
27011 }
27012 }
27013 else
27014 {
27015 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27016 /* Only replace it when it starts with '~' */
27017 if (*dirname == '~')
27018 {
27019 s = vim_strsave(dirname);
27020 if (s != NULL)
27021 {
27022 *fnamep = s;
27023 vim_free(*bufp);
27024 *bufp = s;
27025 }
27026 }
27027 }
27028 vim_free(pbuf);
27029 }
27030 }
27031
27032 tail = gettail(*fnamep);
27033 *fnamelen = (int)STRLEN(*fnamep);
27034
27035 /* ":h" - head, remove "/file_name", can be repeated */
27036 /* Don't remove the first "/" or "c:\" */
27037 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27038 {
27039 valid |= VALID_HEAD;
27040 *usedlen += 2;
27041 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027042 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027043 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027044 *fnamelen = (int)(tail - *fnamep);
27045#ifdef VMS
27046 if (*fnamelen > 0)
27047 *fnamelen += 1; /* the path separator is part of the path */
27048#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027049 if (*fnamelen == 0)
27050 {
27051 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27052 p = vim_strsave((char_u *)".");
27053 if (p == NULL)
27054 return -1;
27055 vim_free(*bufp);
27056 *bufp = *fnamep = tail = p;
27057 *fnamelen = 1;
27058 }
27059 else
27060 {
27061 while (tail > s && !after_pathsep(s, tail))
27062 mb_ptr_back(*fnamep, tail);
27063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027064 }
27065
27066 /* ":8" - shortname */
27067 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27068 {
27069 *usedlen += 2;
27070#ifdef WIN3264
27071 has_shortname = 1;
27072#endif
27073 }
27074
27075#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027076 /*
27077 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027078 */
27079 if (has_shortname)
27080 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027081 /* Copy the string if it is shortened by :h and when it wasn't copied
27082 * yet, because we are going to change it in place. Avoids changing
27083 * the buffer name for "%:8". */
27084 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027085 {
27086 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027087 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027088 return -1;
27089 vim_free(*bufp);
27090 *bufp = *fnamep = p;
27091 }
27092
27093 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027094 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027095 if (!has_fullname && !vim_isAbsName(*fnamep))
27096 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027097 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027098 return -1;
27099 }
27100 else
27101 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027102 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027103
Bram Moolenaardc935552011-08-17 15:23:23 +020027104 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027105 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027106 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027107 return -1;
27108
27109 if (l == 0)
27110 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027111 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027112 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027113 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027114 return -1;
27115 }
27116 *fnamelen = l;
27117 }
27118 }
27119#endif /* WIN3264 */
27120
27121 /* ":t" - tail, just the basename */
27122 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27123 {
27124 *usedlen += 2;
27125 *fnamelen -= (int)(tail - *fnamep);
27126 *fnamep = tail;
27127 }
27128
27129 /* ":e" - extension, can be repeated */
27130 /* ":r" - root, without extension, can be repeated */
27131 while (src[*usedlen] == ':'
27132 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27133 {
27134 /* find a '.' in the tail:
27135 * - for second :e: before the current fname
27136 * - otherwise: The last '.'
27137 */
27138 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27139 s = *fnamep - 2;
27140 else
27141 s = *fnamep + *fnamelen - 1;
27142 for ( ; s > tail; --s)
27143 if (s[0] == '.')
27144 break;
27145 if (src[*usedlen + 1] == 'e') /* :e */
27146 {
27147 if (s > tail)
27148 {
27149 *fnamelen += (int)(*fnamep - (s + 1));
27150 *fnamep = s + 1;
27151#ifdef VMS
27152 /* cut version from the extension */
27153 s = *fnamep + *fnamelen - 1;
27154 for ( ; s > *fnamep; --s)
27155 if (s[0] == ';')
27156 break;
27157 if (s > *fnamep)
27158 *fnamelen = s - *fnamep;
27159#endif
27160 }
27161 else if (*fnamep <= tail)
27162 *fnamelen = 0;
27163 }
27164 else /* :r */
27165 {
27166 if (s > tail) /* remove one extension */
27167 *fnamelen = (int)(s - *fnamep);
27168 }
27169 *usedlen += 2;
27170 }
27171
27172 /* ":s?pat?foo?" - substitute */
27173 /* ":gs?pat?foo?" - global substitute */
27174 if (src[*usedlen] == ':'
27175 && (src[*usedlen + 1] == 's'
27176 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27177 {
27178 char_u *str;
27179 char_u *pat;
27180 char_u *sub;
27181 int sep;
27182 char_u *flags;
27183 int didit = FALSE;
27184
27185 flags = (char_u *)"";
27186 s = src + *usedlen + 2;
27187 if (src[*usedlen + 1] == 'g')
27188 {
27189 flags = (char_u *)"g";
27190 ++s;
27191 }
27192
27193 sep = *s++;
27194 if (sep)
27195 {
27196 /* find end of pattern */
27197 p = vim_strchr(s, sep);
27198 if (p != NULL)
27199 {
27200 pat = vim_strnsave(s, (int)(p - s));
27201 if (pat != NULL)
27202 {
27203 s = p + 1;
27204 /* find end of substitution */
27205 p = vim_strchr(s, sep);
27206 if (p != NULL)
27207 {
27208 sub = vim_strnsave(s, (int)(p - s));
27209 str = vim_strnsave(*fnamep, *fnamelen);
27210 if (sub != NULL && str != NULL)
27211 {
27212 *usedlen = (int)(p + 1 - src);
27213 s = do_string_sub(str, pat, sub, flags);
27214 if (s != NULL)
27215 {
27216 *fnamep = s;
27217 *fnamelen = (int)STRLEN(s);
27218 vim_free(*bufp);
27219 *bufp = s;
27220 didit = TRUE;
27221 }
27222 }
27223 vim_free(sub);
27224 vim_free(str);
27225 }
27226 vim_free(pat);
27227 }
27228 }
27229 /* after using ":s", repeat all the modifiers */
27230 if (didit)
27231 goto repeat;
27232 }
27233 }
27234
Bram Moolenaar26df0922014-02-23 23:39:13 +010027235 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27236 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027237 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027238 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027239 if (c != NUL)
27240 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027241 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027242 if (c != NUL)
27243 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027244 if (p == NULL)
27245 return -1;
27246 vim_free(*bufp);
27247 *bufp = *fnamep = p;
27248 *fnamelen = (int)STRLEN(p);
27249 *usedlen += 2;
27250 }
27251
Bram Moolenaar071d4272004-06-13 20:20:40 +000027252 return valid;
27253}
27254
27255/*
27256 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27257 * "flags" can be "g" to do a global substitute.
27258 * Returns an allocated string, NULL for error.
27259 */
27260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027261do_string_sub(
27262 char_u *str,
27263 char_u *pat,
27264 char_u *sub,
27265 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027266{
27267 int sublen;
27268 regmatch_T regmatch;
27269 int i;
27270 int do_all;
27271 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027272 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027273 garray_T ga;
27274 char_u *ret;
27275 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027276 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027277
27278 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27279 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027280 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027281
27282 ga_init2(&ga, 1, 200);
27283
27284 do_all = (flags[0] == 'g');
27285
27286 regmatch.rm_ic = p_ic;
27287 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27288 if (regmatch.regprog != NULL)
27289 {
27290 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027291 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027292 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27293 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027294 /* Skip empty match except for first match. */
27295 if (regmatch.startp[0] == regmatch.endp[0])
27296 {
27297 if (zero_width == regmatch.startp[0])
27298 {
27299 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027300 i = MB_PTR2LEN(tail);
27301 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27302 (size_t)i);
27303 ga.ga_len += i;
27304 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027305 continue;
27306 }
27307 zero_width = regmatch.startp[0];
27308 }
27309
Bram Moolenaar071d4272004-06-13 20:20:40 +000027310 /*
27311 * Get some space for a temporary buffer to do the substitution
27312 * into. It will contain:
27313 * - The text up to where the match is.
27314 * - The substituted text.
27315 * - The text after the match.
27316 */
27317 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027318 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027319 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27320 {
27321 ga_clear(&ga);
27322 break;
27323 }
27324
27325 /* copy the text up to where the match is */
27326 i = (int)(regmatch.startp[0] - tail);
27327 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27328 /* add the substituted text */
27329 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27330 + ga.ga_len + i, TRUE, TRUE, FALSE);
27331 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027332 tail = regmatch.endp[0];
27333 if (*tail == NUL)
27334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027335 if (!do_all)
27336 break;
27337 }
27338
27339 if (ga.ga_data != NULL)
27340 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27341
Bram Moolenaar473de612013-06-08 18:19:48 +020027342 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027343 }
27344
27345 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27346 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027347 if (p_cpo == empty_option)
27348 p_cpo = save_cpo;
27349 else
27350 /* Darn, evaluating {sub} expression changed the value. */
27351 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027352
27353 return ret;
27354}
27355
27356#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */