blob: 34f2bde85217487edb6cac918d11871bcb9f8902 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200114#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200115static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200116#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000117
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200118static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000122 * Old Vim variables such as "v:version" are also available without the "v:".
123 * Also in functions. We need a special hashtable for them.
124 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000125static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000126
127/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000128 * When recursively copying lists and dicts we need to remember which ones we
129 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000130 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000131 */
132static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000133#define COPYID_INC 2
134#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135
Bram Moolenaar8502c702014-06-17 12:51:16 +0200136/* Abort conversion to string after a recursion error. */
137static int did_echo_string_emsg = FALSE;
138
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000140 * Array to hold the hashtab with variables local to each sourced script.
141 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000143typedef struct
144{
145 dictitem_T sv_var;
146 dict_T sv_dict;
147} scriptvar_T;
148
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200149static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
150#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
151#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153static int echo_attr = 0; /* attributes used for ":echo" */
154
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000155/* Values for trans_function_name() argument: */
156#define TFN_INT 1 /* internal function name OK */
157#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100158#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
159
160/* Values for get_lval() flags argument: */
161#define GLV_QUIET TFN_QUIET /* no error messages */
162#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164/*
165 * Structure to hold info for a user function.
166 */
167typedef struct ufunc ufunc_T;
168
169struct ufunc
170{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000171 int uf_varargs; /* variable nr of arguments */
172 int uf_flags;
173 int uf_calls; /* nr of active calls */
174 garray_T uf_args; /* arguments */
175 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000176#ifdef FEAT_PROFILE
177 int uf_profiling; /* TRUE when func is being profiled */
178 /* profiling the function as a whole */
179 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000180 proftime_T uf_tm_total; /* time spent in function + children */
181 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000182 proftime_T uf_tm_children; /* time spent in children this call */
183 /* profiling the function per line */
184 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000185 proftime_T *uf_tml_total; /* time spent in a line + children */
186 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000187 proftime_T uf_tml_start; /* start time for current line */
188 proftime_T uf_tml_children; /* time spent in children for this line */
189 proftime_T uf_tml_wait; /* start wait time for current line */
190 int uf_tml_idx; /* index of line being timed; -1 if none */
191 int uf_tml_execed; /* line being timed was executed */
192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000195 int uf_refcount; /* for numbered function: reference count */
196 char_u uf_name[1]; /* name of function (actually longer); can
197 start with <SNR>123_ (<SNR> is K_SPECIAL
198 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199};
200
201/* function flags */
202#define FC_ABORT 1 /* abort function on error */
203#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000204#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
206/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000209static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000211/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000212static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
213
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214/* list heads for garbage collection */
215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000372};
373
374/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000375#define vv_type vv_di.di_tv.v_type
376#define vv_nr vv_di.di_tv.vval.v_number
377#define vv_float vv_di.di_tv.vval.v_float
378#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000379#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200380#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000381#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000382
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200383static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000384#define vimvarht vimvardict.dv_hashtab
385
Bram Moolenaara40058a2005-07-11 22:42:07 +0000386static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
387static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
409static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
410static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
411static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
412static void item_lock __ARGS((typval_T *tv, int deep, int lock));
413static int tv_islocked __ARGS((typval_T *tv));
414
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
416static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000421static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
422static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000424static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
427static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000429static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100431static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
432static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
433static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000434static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000436static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
438static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100441static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000443static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200444static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
446static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000450static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000451static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
452static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static int string2float __ARGS((char_u *text, float_T *value));
456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
458static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100459static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200461static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000462static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000463static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#ifdef FEAT_FLOAT
466static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100470static void f_alloc_fail __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100471static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200475static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100477static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara803c7f2016-01-15 15:31:39 +0100478static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +0100479static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200482static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100495static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100497static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200531static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533#ifdef FEAT_FLOAT
534static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
535#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000538static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#ifdef FEAT_FLOAT
545static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000548#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000549static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000550static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000558static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000560static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200564static void f_getcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000567static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200568static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000576static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000577static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200578static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000579static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000580static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200583static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000584static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar825e7ab2015-03-20 17:36:42 +0100590static void f_glob2regpat __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000593static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000607static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100612static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000614static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200627static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200630#ifdef FEAT_LUA
631static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000637static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200638static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000639static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000640static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000642static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000646#ifdef vim_mkdir
647static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100650#ifdef FEAT_MZSCHEME
651static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100655static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000656static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000661static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000662static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200663#ifdef FEAT_PYTHON3
664static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
666#ifdef FEAT_PYTHON
667static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000671static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
684static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
685#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200686static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100688static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
689static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000691static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000693static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200698static void f_setcharsearch __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000701static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000702static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000703static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000704static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200706static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000707static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100709#ifdef FEAT_CRYPT
710static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
711#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000712static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200713static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000715#ifdef FEAT_FLOAT
716static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200717static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000720static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000721static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#ifdef FEAT_FLOAT
725static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
727#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000728static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200729static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730#ifdef HAVE_STRFTIME
731static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
733static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200739static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200740static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000746static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200747static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200749static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000750static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000751static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000752static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000753static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000754static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000756static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200757#ifdef FEAT_FLOAT
758static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
760#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000761static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
765static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
766#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200768static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200769static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100770static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
772static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
773static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100774static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
776static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
777static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
778static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
779static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
780static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000781static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
782static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000784static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaared767a22016-01-03 22:49:16 +0100785static void f_wordcount __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100786static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787
Bram Moolenaar493c1782014-05-28 14:34:46 +0200788static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000789static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static int get_env_len __ARGS((char_u **arg));
791static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000792static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000793static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
794#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
795#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
796 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000797static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000798static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar1cd5e612015-05-04 11:10:27 +0200800static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000801static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static typval_T *alloc_tv __ARGS((void));
803static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void init_tv __ARGS((typval_T *varp));
805static long get_tv_number __ARGS((typval_T *varp));
806static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000807static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static char_u *get_tv_string __ARGS((typval_T *varp));
809static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000810static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
812static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000813static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
814static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
815static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000816static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
817static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200819static int var_check_ro __ARGS((int flags, char_u *name, int use_gettext));
820static int var_check_fixed __ARGS((int flags, char_u *name, int use_gettext));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200821static int var_check_func_name __ARGS((char_u *name, int new_var));
822static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar77354e72015-04-21 16:49:05 +0200823static int tv_check_lock __ARGS((int lock, char_u *name, int use_gettext));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000824static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
826static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
827static int eval_fname_script __ARGS((char_u *p));
828static int eval_fname_sid __ARGS((char_u *p));
829static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static ufunc_T *find_func __ARGS((char_u *name));
831static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200832static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#ifdef FEAT_PROFILE
834static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000835static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
836static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
837static int
838# ifdef __BORLANDC__
839 _RTLENTRYF
840# endif
841 prof_total_cmp __ARGS((const void *s1, const void *s2));
842static int
843# ifdef __BORLANDC__
844 _RTLENTRYF
845# endif
846 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200848static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000850static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000851static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000852static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000853static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
854static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000855static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000856static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
857static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000858static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000859static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000860static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200861static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200862static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200864
865#ifdef EBCDIC
866static int compare_func_name __ARGS((const void *s1, const void *s2));
867static void sortFunctions __ARGS(());
868#endif
869
Bram Moolenaar33570922005-01-25 22:26:29 +0000870/*
871 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000872 */
873 void
874eval_init()
875{
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 int i;
877 struct vimvar *p;
878
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200879 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
880 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200881 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000882 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000884
885 for (i = 0; i < VV_LEN; ++i)
886 {
887 p = &vimvars[i];
888 STRCPY(p->vv_di.di_key, p->vv_name);
889 if (p->vv_flags & VV_RO)
890 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
891 else if (p->vv_flags & VV_RO_SBX)
892 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
893 else
894 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000895
896 /* add to v: scope dict, unless the value is not always available */
897 if (p->vv_type != VAR_UNKNOWN)
898 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000900 /* add to compat scope dict */
901 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000902 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000903 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100904 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200905 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100906 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200907 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908
909#ifdef EBCDIC
910 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100911 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912 */
913 sortFunctions();
914#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000915}
916
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917#if defined(EXITFREE) || defined(PROTO)
918 void
919eval_clear()
920{
921 int i;
922 struct vimvar *p;
923
924 for (i = 0; i < VV_LEN; ++i)
925 {
926 p = &vimvars[i];
927 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000928 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000929 vim_free(p->vv_str);
930 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000931 }
932 else if (p->vv_di.di_tv.v_type == VAR_LIST)
933 {
934 list_unref(p->vv_list);
935 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000936 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937 }
938 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000939 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000940 hash_clear(&compat_hashtab);
941
Bram Moolenaard9fba312005-06-26 22:34:35 +0000942 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100943# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200944 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100945# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000946
947 /* global variables */
948 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000949
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000950 /* autoloaded script names */
951 ga_clear_strings(&ga_loaded);
952
Bram Moolenaarcca74132013-09-25 21:00:28 +0200953 /* Script-local variables. First clear all the variables and in a second
954 * loop free the scriptvar_T, because a variable in one script might hold
955 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200956 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200957 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200958 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200959 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200960 ga_clear(&ga_scripts);
961
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000962 /* unreferenced lists and dicts */
963 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000964
965 /* functions */
966 free_all_functions();
967 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968}
969#endif
970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 * Return the name of the executed function.
973 */
974 char_u *
975func_name(cookie)
976 void *cookie;
977{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000978 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
982 * Return the address holding the next breakpoint line for a funccall cookie.
983 */
984 linenr_T *
985func_breakpoint(cookie)
986 void *cookie;
987{
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/*
992 * Return the address holding the debug tick for a funccall cookie.
993 */
994 int *
995func_dbg_tick(cookie)
996 void *cookie;
997{
Bram Moolenaar33570922005-01-25 22:26:29 +0000998 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * Return the nesting level for a funccall cookie.
1003 */
1004 int
1005func_level(cookie)
1006 void *cookie;
1007{
Bram Moolenaar33570922005-01-25 22:26:29 +00001008 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001012funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001014/* pointer to list of previously used funccal, still around because some
1015 * item in it is still being used. */
1016funccall_T *previous_funccal = NULL;
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018/*
1019 * Return TRUE when a function was ended by a ":return" command.
1020 */
1021 int
1022current_func_returned()
1023{
1024 return current_funccal->returned;
1025}
1026
1027
1028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * Set an internal variable to a string value. Creates the variable if it does
1030 * not already exist.
1031 */
1032 void
1033set_internal_string_var(name, value)
1034 char_u *name;
1035 char_u *value;
1036{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001037 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 val = vim_strsave(value);
1041 if (val != NULL)
1042 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001043 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001044 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001046 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 }
1050}
1051
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054static char_u *redir_endp = NULL;
1055static char_u *redir_varname = NULL;
1056
1057/*
1058 * Start recording command output to a variable
1059 * Returns OK if successfully completed the setup. FAIL otherwise.
1060 */
1061 int
1062var_redir_start(name, append)
1063 char_u *name;
1064 int append; /* append to an existing variable */
1065{
1066 int save_emsg;
1067 int err;
1068 typval_T tv;
1069
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 {
1073 EMSG(_(e_invarg));
1074 return FAIL;
1075 }
1076
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 redir_varname = vim_strsave(name);
1079 if (redir_varname == NULL)
1080 return FAIL;
1081
1082 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1083 if (redir_lval == NULL)
1084 {
1085 var_redir_stop();
1086 return FAIL;
1087 }
1088
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001089 /* The output is stored in growarray "redir_ga" until redirection ends. */
1090 ga_init2(&redir_ga, (int)sizeof(char), 500);
1091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001093 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001094 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1096 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001097 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (redir_endp != NULL && *redir_endp != NUL)
1099 /* Trailing characters are present after the variable name */
1100 EMSG(_(e_trailing));
1101 else
1102 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001103 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 var_redir_stop();
1105 return FAIL;
1106 }
1107
1108 /* check if we can write to the variable: set it to or append an empty
1109 * string */
1110 save_emsg = did_emsg;
1111 did_emsg = FALSE;
1112 tv.v_type = VAR_STRING;
1113 tv.vval.v_string = (char_u *)"";
1114 if (append)
1115 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1116 else
1117 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001118 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001120 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 if (err)
1122 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 var_redir_stop();
1125 return FAIL;
1126 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127
1128 return OK;
1129}
1130
1131/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 * Append "value[value_len]" to the variable set by var_redir_start().
1133 * The actual appending is postponed until redirection ends, because the value
1134 * appended may in fact be the string we write to, changing it may cause freed
1135 * memory to be used:
1136 * :redir => foo
1137 * :let foo
1138 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 */
1140 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001141var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001145 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 if (redir_lval == NULL)
1148 return;
1149
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001150 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001151 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001153 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001155 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156 {
1157 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001158 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159 }
1160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162}
1163
1164/*
1165 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 */
1168 void
1169var_redir_stop()
1170{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 typval_T tv;
1172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_lval != NULL)
1174 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001175 /* If there was no error: assign the text to the variable. */
1176 if (redir_endp != NULL)
1177 {
1178 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1179 tv.v_type = VAR_STRING;
1180 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 /* Call get_lval() again, if it's inside a Dict or List it may
1182 * have changed. */
1183 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001184 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1186 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1187 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001188 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001190 /* free the collected output */
1191 vim_free(redir_ga.ga_data);
1192 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 vim_free(redir_lval);
1195 redir_lval = NULL;
1196 }
1197 vim_free(redir_varname);
1198 redir_varname = NULL;
1199}
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201# if defined(FEAT_MBYTE) || defined(PROTO)
1202 int
1203eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1204 char_u *enc_from;
1205 char_u *enc_to;
1206 char_u *fname_from;
1207 char_u *fname_to;
1208{
1209 int err = FALSE;
1210
1211 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1212 set_vim_var_string(VV_CC_TO, enc_to, -1);
1213 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1214 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1215 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_CC_FROM, NULL, -1);
1218 set_vim_var_string(VV_CC_TO, NULL, -1);
1219 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1220 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1221
1222 if (err)
1223 return FAIL;
1224 return OK;
1225}
1226# endif
1227
1228# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1229 int
1230eval_printexpr(fname, args)
1231 char_u *fname;
1232 char_u *args;
1233{
1234 int err = FALSE;
1235
1236 set_vim_var_string(VV_FNAME_IN, fname, -1);
1237 set_vim_var_string(VV_CMDARG, args, -1);
1238 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1239 err = TRUE;
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_CMDARG, NULL, -1);
1242
1243 if (err)
1244 {
1245 mch_remove(fname);
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250# endif
1251
1252# if defined(FEAT_DIFF) || defined(PROTO)
1253 void
1254eval_diff(origfile, newfile, outfile)
1255 char_u *origfile;
1256 char_u *newfile;
1257 char_u *outfile;
1258{
1259 int err = FALSE;
1260
1261 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1262 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1263 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1264 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268}
1269
1270 void
1271eval_patch(origfile, difffile, outfile)
1272 char_u *origfile;
1273 char_u *difffile;
1274 char_u *outfile;
1275{
1276 int err;
1277
1278 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1279 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1280 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1281 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285}
1286# endif
1287
1288/*
1289 * Top level evaluation function, returning a boolean.
1290 * Sets "error" to TRUE if there was an error.
1291 * Return TRUE or FALSE.
1292 */
1293 int
1294eval_to_bool(arg, error, nextcmd, skip)
1295 char_u *arg;
1296 int *error;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int retval = FALSE;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 else
1308 {
1309 *error = FALSE;
1310 if (!skip)
1311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
1323 * Top level evaluation function, returning a string. If "skip" is TRUE,
1324 * only parsing to "nextcmd" is done, without reporting errors. Return
1325 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1326 */
1327 char_u *
1328eval_to_string_skip(arg, nextcmd, skip)
1329 char_u *arg;
1330 char_u **nextcmd;
1331 int skip; /* only parse, don't execute */
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *retval;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 retval = NULL;
1340 else
1341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 retval = vim_strsave(get_tv_string(&tv));
1343 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 if (skip)
1346 --emsg_skip;
1347
1348 return retval;
1349}
1350
1351/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001352 * Skip over an expression at "*pp".
1353 * Return FAIL for an error, OK otherwise.
1354 */
1355 int
1356skip_expr(pp)
1357 char_u **pp;
1358{
Bram Moolenaar33570922005-01-25 22:26:29 +00001359 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001360
1361 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001363}
1364
1365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367 * When "convert" is TRUE convert a List into a sequence of lines and convert
1368 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 * Return pointer to allocated memory, or NULL for failure.
1370 */
1371 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *arg;
1374 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001379 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001380#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001381 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 retval = NULL;
1386 else
1387 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001388 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 {
1390 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001391 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001392 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001393 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001394 if (tv.vval.v_list->lv_len > 0)
1395 ga_append(&ga, NL);
1396 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 ga_append(&ga, NUL);
1398 retval = (char_u *)ga.ga_data;
1399 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001400#ifdef FEAT_FLOAT
1401 else if (convert && tv.v_type == VAR_FLOAT)
1402 {
1403 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1404 retval = vim_strsave(numbuf);
1405 }
1406#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 else
1408 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001416 * Call eval_to_string() without using current local variables and using
1417 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001420eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *arg;
1422 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001423 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 char_u *retval;
1426 void *save_funccalp;
1427
1428 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001429 if (use_sandbox)
1430 ++sandbox;
1431 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001433 if (use_sandbox)
1434 --sandbox;
1435 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 restore_funccal(save_funccalp);
1437 return retval;
1438}
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Top level evaluation function, returning a number.
1442 * Evaluates "expr" silently.
1443 * Returns -1 for an error.
1444 */
1445 int
1446eval_to_number(expr)
1447 char_u *expr;
1448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001451 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 ++emsg_off;
1454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 retval = -1;
1457 else
1458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001459 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 --emsg_off;
1463
1464 return retval;
1465}
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467/*
1468 * Prepare v: variable "idx" to be used.
1469 * Save the current typeval in "save_tv".
1470 * When not used yet add the variable to the v: hashtable.
1471 */
1472 static void
1473prepare_vimvar(idx, save_tv)
1474 int idx;
1475 typval_T *save_tv;
1476{
1477 *save_tv = vimvars[idx].vv_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1480}
1481
1482/*
1483 * Restore v: variable "idx" to typeval "save_tv".
1484 * When no longer defined, remove the variable from the v: hashtable.
1485 */
1486 static void
1487restore_vimvar(idx, save_tv)
1488 int idx;
1489 typval_T *save_tv;
1490{
1491 hashitem_T *hi;
1492
Bram Moolenaara40058a2005-07-11 22:42:07 +00001493 vimvars[idx].vv_tv = *save_tv;
1494 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1495 {
1496 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1497 if (HASHITEM_EMPTY(hi))
1498 EMSG2(_(e_intern2), "restore_vimvar()");
1499 else
1500 hash_remove(&vimvarht, hi);
1501 }
1502}
1503
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001504#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001505/*
1506 * Evaluate an expression to a list with suggestions.
1507 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001508 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001509 */
1510 list_T *
1511eval_spell_expr(badword, expr)
1512 char_u *badword;
1513 char_u *expr;
1514{
1515 typval_T save_val;
1516 typval_T rettv;
1517 list_T *list = NULL;
1518 char_u *p = skipwhite(expr);
1519
1520 /* Set "v:val" to the bad word. */
1521 prepare_vimvar(VV_VAL, &save_val);
1522 vimvars[VV_VAL].vv_type = VAR_STRING;
1523 vimvars[VV_VAL].vv_str = badword;
1524 if (p_verbose == 0)
1525 ++emsg_off;
1526
1527 if (eval1(&p, &rettv, TRUE) == OK)
1528 {
1529 if (rettv.v_type != VAR_LIST)
1530 clear_tv(&rettv);
1531 else
1532 list = rettv.vval.v_list;
1533 }
1534
1535 if (p_verbose == 0)
1536 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537 restore_vimvar(VV_VAL, &save_val);
1538
1539 return list;
1540}
1541
1542/*
1543 * "list" is supposed to contain two items: a word and a number. Return the
1544 * word in "pp" and the number as the return value.
1545 * Return -1 if anything isn't right.
1546 * Used to get the good word and score from the eval_spell_expr() result.
1547 */
1548 int
1549get_spellword(list, pp)
1550 list_T *list;
1551 char_u **pp;
1552{
1553 listitem_T *li;
1554
1555 li = list->lv_first;
1556 if (li == NULL)
1557 return -1;
1558 *pp = get_tv_string(&li->li_tv);
1559
1560 li = li->li_next;
1561 if (li == NULL)
1562 return -1;
1563 return get_tv_number(&li->li_tv);
1564}
1565#endif
1566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001568 * Top level evaluation function.
1569 * Returns an allocated typval_T with the result.
1570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001571 */
1572 typval_T *
1573eval_expr(arg, nextcmd)
1574 char_u *arg;
1575 char_u **nextcmd;
1576{
1577 typval_T *tv;
1578
1579 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001580 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001581 {
1582 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001583 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584 }
1585
1586 return tv;
1587}
1588
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001591 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 * Uses argv[argc] for the function arguments. Only Number and String
1593 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001594 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001596 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001597call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 char_u *func;
1599 int argc;
1600 char_u **argv;
1601 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001602 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
Bram Moolenaar33570922005-01-25 22:26:29 +00001605 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long n;
1607 int len;
1608 int i;
1609 int doesrange;
1610 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001613 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 for (i = 0; i < argc; i++)
1618 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001619 /* Pass a NULL or empty argument as an empty string */
1620 if (argv[i] == NULL || *argv[i] == NUL)
1621 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001622 argvars[i].v_type = VAR_STRING;
1623 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001624 continue;
1625 }
1626
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001627 if (str_arg_only)
1628 len = 0;
1629 else
1630 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001631 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (len != 0 && len == (int)STRLEN(argv[i]))
1633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001634 argvars[i].v_type = VAR_NUMBER;
1635 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
1637 else
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 }
1643
1644 if (safe)
1645 {
1646 save_funccalp = save_funccal();
1647 ++sandbox;
1648 }
1649
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1651 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (safe)
1655 {
1656 --sandbox;
1657 restore_funccal(save_funccalp);
1658 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 vim_free(argvars);
1660
1661 if (ret == FAIL)
1662 clear_tv(rettv);
1663
1664 return ret;
1665}
1666
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001667/*
1668 * Call vimL function "func" and return the result as a number.
1669 * Returns -1 when calling the function fails.
1670 * Uses argv[argc] for the function arguments.
1671 */
1672 long
1673call_func_retnr(func, argc, argv, safe)
1674 char_u *func;
1675 int argc;
1676 char_u **argv;
1677 int safe; /* use the sandbox */
1678{
1679 typval_T rettv;
1680 long retval;
1681
1682 /* All arguments are passed as strings, no conversion to number. */
1683 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1684 return -1;
1685
1686 retval = get_tv_number_chk(&rettv, NULL);
1687 clear_tv(&rettv);
1688 return retval;
1689}
1690
1691#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1692 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1693
Bram Moolenaar4f688582007-07-24 12:34:30 +00001694# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001696 * Call vimL function "func" and return the result as a string.
1697 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 * Uses argv[argc] for the function arguments.
1699 */
1700 void *
1701call_func_retstr(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001708 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001709
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001710 /* All arguments are passed as strings, no conversion to number. */
1711 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 return NULL;
1713
1714 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return retval;
1717}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001721 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001723 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 */
1725 void *
1726call_func_retlist(func, argc, argv, safe)
1727 char_u *func;
1728 int argc;
1729 char_u **argv;
1730 int safe; /* use the sandbox */
1731{
1732 typval_T rettv;
1733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 if (rettv.v_type != VAR_LIST)
1739 {
1740 clear_tv(&rettv);
1741 return NULL;
1742 }
1743
1744 return rettv.vval.v_list;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
1747
1748/*
1749 * Save the current function call pointer, and set it to NULL.
1750 * Used when executing autocommands and for ":source".
1751 */
1752 void *
1753save_funccal()
1754{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001755 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 current_funccal = NULL;
1758 return (void *)fc;
1759}
1760
1761 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001762restore_funccal(vfc)
1763 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001765 funccall_T *fc = (funccall_T *)vfc;
1766
1767 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
Bram Moolenaar05159a02005-02-26 23:04:13 +00001770#if defined(FEAT_PROFILE) || defined(PROTO)
1771/*
1772 * Prepare profiling for entering a child or something else that is not
1773 * counted for the script/function itself.
1774 * Should always be called in pair with prof_child_exit().
1775 */
1776 void
1777prof_child_enter(tm)
1778 proftime_T *tm; /* place to store waittime */
1779{
1780 funccall_T *fc = current_funccal;
1781
1782 if (fc != NULL && fc->func->uf_profiling)
1783 profile_start(&fc->prof_child);
1784 script_prof_save(tm);
1785}
1786
1787/*
1788 * Take care of time spent in a child.
1789 * Should always be called after prof_child_enter().
1790 */
1791 void
1792prof_child_exit(tm)
1793 proftime_T *tm; /* where waittime was stored */
1794{
1795 funccall_T *fc = current_funccal;
1796
1797 if (fc != NULL && fc->func->uf_profiling)
1798 {
1799 profile_end(&fc->prof_child);
1800 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1801 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1802 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1803 }
1804 script_prof_restore(tm);
1805}
1806#endif
1807
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_FOLDING
1810/*
1811 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1812 * it in "*cp". Doesn't give error messages.
1813 */
1814 int
1815eval_foldexpr(arg, cp)
1816 char_u *arg;
1817 int *cp;
1818{
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int retval;
1821 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001822 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1823 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 ++sandbox;
1828 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 retval = 0;
1832 else
1833 {
1834 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 if (tv.v_type == VAR_NUMBER)
1836 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001837 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 retval = 0;
1839 else
1840 {
1841 /* If the result is a string, check if there is a non-digit before
1842 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (!VIM_ISDIGIT(*s) && *s != '-')
1845 *cp = *s++;
1846 retval = atol((char *)s);
1847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001851 if (use_sandbox)
1852 --sandbox;
1853 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 return retval;
1856}
1857#endif
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 * ":let" list all variable values
1861 * ":let var1 var2" list variable values
1862 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 * ":let var += expr" assignment command.
1864 * ":let var -= expr" assignment command.
1865 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 */
1868 void
1869ex_let(eap)
1870 exarg_T *eap;
1871{
1872 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001874 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 int var_count = 0;
1877 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001878 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001879 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 argend = skip_var_list(arg, &var_count, &semicolon);
1883 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001885 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1886 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001887 expr = skipwhite(argend);
1888 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1889 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001891 /*
1892 * ":let" without "=": list variables
1893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 if (*arg == '[')
1895 EMSG(_(e_invarg));
1896 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001898 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 list_glob_vars(&first);
1903 list_buf_vars(&first);
1904 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001905#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001907#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 list_script_vars(&first);
1909 list_func_vars(&first);
1910 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 eap->nextcmd = check_nextcmd(arg);
1913 }
1914 else
1915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op[0] = '=';
1917 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001918 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001919 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001920 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1921 op[0] = *expr; /* +=, -= or .= */
1922 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 else
1925 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (eap->skip)
1928 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 if (eap->skip)
1931 {
1932 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --emsg_skip;
1935 }
1936 else if (i != FAIL)
1937 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 }
1943}
1944
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945/*
1946 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1947 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 * When "nextchars" is not NULL it points to a string with characters that
1949 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1950 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 * Returns OK or FAIL;
1952 */
1953 static int
1954ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1955 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 int copy; /* copy values from "tv", don't move */
1958 int semicolon; /* from skip_var_list() */
1959 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961{
1962 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001963 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001965 listitem_T *item;
1966 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967
1968 if (*arg != '[')
1969 {
1970 /*
1971 * ":let var = expr" or ":for var in list"
1972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 return OK;
1976 }
1977
1978 /*
1979 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1980 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001981 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 {
1983 EMSG(_(e_listreq));
1984 return FAIL;
1985 }
1986
1987 i = list_len(l);
1988 if (semicolon == 0 && var_count < i)
1989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001990 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 }
1993 if (var_count - semicolon > i)
1994 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001995 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 }
1998
1999 item = l->lv_first;
2000 while (*arg != ']')
2001 {
2002 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 item = item->li_next;
2005 if (arg == NULL)
2006 return FAIL;
2007
2008 arg = skipwhite(arg);
2009 if (*arg == ';')
2010 {
2011 /* Put the rest of the list (may be empty) in the var after ';'.
2012 * Create a new list for this. */
2013 l = list_alloc();
2014 if (l == NULL)
2015 return FAIL;
2016 while (item != NULL)
2017 {
2018 list_append_tv(l, &item->li_tv);
2019 item = item->li_next;
2020 }
2021
2022 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002023 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 ltv.vval.v_list = l;
2025 l->lv_refcount = 1;
2026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2028 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 clear_tv(&ltv);
2030 if (arg == NULL)
2031 return FAIL;
2032 break;
2033 }
2034 else if (*arg != ',' && *arg != ']')
2035 {
2036 EMSG2(_(e_intern2), "ex_let_vars()");
2037 return FAIL;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
2044/*
2045 * Skip over assignable variable "var" or list of variables "[var, var]".
2046 * Used for ":let varvar = expr" and ":for varvar in expr".
2047 * For "[var, var]" increment "*var_count" for each variable.
2048 * for "[var, var; var]" set "semicolon".
2049 * Return NULL for an error.
2050 */
2051 static char_u *
2052skip_var_list(arg, var_count, semicolon)
2053 char_u *arg;
2054 int *var_count;
2055 int *semicolon;
2056{
2057 char_u *p, *s;
2058
2059 if (*arg == '[')
2060 {
2061 /* "[var, var]": find the matching ']'. */
2062 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002063 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 {
2065 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2066 s = skip_var_one(p);
2067 if (s == p)
2068 {
2069 EMSG2(_(e_invarg2), p);
2070 return NULL;
2071 }
2072 ++*var_count;
2073
2074 p = skipwhite(s);
2075 if (*p == ']')
2076 break;
2077 else if (*p == ';')
2078 {
2079 if (*semicolon == 1)
2080 {
2081 EMSG(_("Double ; in list of variables"));
2082 return NULL;
2083 }
2084 *semicolon = 1;
2085 }
2086 else if (*p != ',')
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 }
2092 return p + 1;
2093 }
2094 else
2095 return skip_var_one(arg);
2096}
2097
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002099 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002100 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002102 static char_u *
2103skip_var_one(arg)
2104 char_u *arg;
2105{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002106 if (*arg == '@' && arg[1] != NUL)
2107 return arg + 2;
2108 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2109 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110}
2111
Bram Moolenaara7043832005-01-21 11:56:39 +00002112/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 * List variables for hashtab "ht" with prefix "prefix".
2114 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002118 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar33570922005-01-25 22:26:29 +00002123 hashitem_T *hi;
2124 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125 int todo;
2126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002127 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2129 {
2130 if (!HASHITEM_EMPTY(hi))
2131 {
2132 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 di = HI2DI(hi);
2134 if (empty || di->di_tv.v_type != VAR_STRING
2135 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 }
2138 }
2139}
2140
2141/*
2142 * List global variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_glob_vars(first)
2146 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002149}
2150
2151/*
2152 * List buffer variables.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_buf_vars(first)
2156 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 char_u numbuf[NUMBUFLEN];
2159
Bram Moolenaar429fa852013-04-15 12:27:36 +02002160 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162
2163 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2165 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List window variables.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_win_vars(first)
2173 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002175 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002177}
2178
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179#ifdef FEAT_WINDOWS
2180/*
2181 * List tab page variables.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_tab_vars(first)
2185 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002186{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002189}
2190#endif
2191
Bram Moolenaara7043832005-01-21 11:56:39 +00002192/*
2193 * List Vim variables.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_vim_vars(first)
2197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200}
2201
2202/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002203 * List script-local variables, if there is a script.
2204 */
2205 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206list_script_vars(first)
2207 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208{
2209 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2211 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002212}
2213
2214/*
2215 * List function variables, if there is a function.
2216 */
2217 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218list_func_vars(first)
2219 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_funccal != NULL)
2222 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 * List variables in "arg".
2228 */
2229 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 exarg_T *eap;
2232 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234{
2235 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002238 char_u *name_start;
2239 char_u *arg_subsc;
2240 char_u *tofree;
2241 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242
2243 while (!ends_excmd(*arg) && !got_int)
2244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002247 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2249 {
2250 emsg_severe = TRUE;
2251 EMSG(_(e_trailing));
2252 break;
2253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* get_name_len() takes care of expanding curly braces */
2258 name_start = name = arg;
2259 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2260 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 /* This is mainly to keep test 49 working: when expanding
2263 * curly braces fails overrule the exception error message. */
2264 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 emsg_severe = TRUE;
2267 EMSG2(_(e_invarg2), arg);
2268 break;
2269 }
2270 error = TRUE;
2271 }
2272 else
2273 {
2274 if (tofree != NULL)
2275 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002276 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 else
2279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 /* handle d.key, l[idx], f(expr) */
2281 arg_subsc = arg;
2282 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 case 'g': list_glob_vars(first); break;
2291 case 'b': list_buf_vars(first); break;
2292 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002294 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002295#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'v': list_vim_vars(first); break;
2297 case 's': list_script_vars(first); break;
2298 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 default:
2300 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 }
2303 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 {
2305 char_u numbuf[NUMBUFLEN];
2306 char_u *tf;
2307 int c;
2308 char_u *s;
2309
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002310 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 c = *arg;
2312 *arg = NUL;
2313 list_one_var_a((char_u *)"",
2314 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002315 tv.v_type,
2316 s == NULL ? (char_u *)"" : s,
2317 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 *arg = c;
2319 vim_free(tf);
2320 }
2321 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 }
2324 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325
2326 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328
2329 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 }
2331
2332 return arg;
2333}
2334
2335/*
2336 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2337 * Returns a pointer to the char just after the var name.
2338 * Returns NULL if there is an error.
2339 */
2340 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002345 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347{
2348 int c1;
2349 char_u *name;
2350 char_u *p;
2351 char_u *arg_end = NULL;
2352 int len;
2353 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355
2356 /*
2357 * ":let $VAR = expr": Set environment variable.
2358 */
2359 if (*arg == '$')
2360 {
2361 /* Find the end of the name. */
2362 ++arg;
2363 name = arg;
2364 len = get_env_len(&arg);
2365 if (len == 0)
2366 EMSG2(_(e_invarg2), name - 1);
2367 else
2368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 if (op != NULL && (*op == '+' || *op == '-'))
2370 EMSG2(_(e_letwrong), op);
2371 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002374 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 {
2376 c1 = name[len];
2377 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 p = get_tv_string_chk(tv);
2379 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 int mustfree = FALSE;
2382 char_u *s = vim_getenv(name, &mustfree);
2383
2384 if (s != NULL)
2385 {
2386 p = tofree = concat_str(s, p);
2387 if (mustfree)
2388 vim_free(s);
2389 }
2390 }
2391 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (STRICMP(name, "HOME") == 0)
2395 init_homedir();
2396 else if (didset_vim && STRICMP(name, "VIM") == 0)
2397 didset_vim = FALSE;
2398 else if (didset_vimruntime
2399 && STRICMP(name, "VIMRUNTIME") == 0)
2400 didset_vimruntime = FALSE;
2401 arg_end = arg;
2402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 }
2406 }
2407 }
2408
2409 /*
2410 * ":let &option = expr": Set option value.
2411 * ":let &l:option = expr": Set local option value.
2412 * ":let &g:option = expr": Set global option value.
2413 */
2414 else if (*arg == '&')
2415 {
2416 /* Find the end of the name. */
2417 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 if (p == NULL || (endchars != NULL
2419 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
2421 else
2422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 long n;
2424 int opt_type;
2425 long numval;
2426 char_u *stringval = NULL;
2427 char_u *s;
2428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 c1 = *p;
2430 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431
2432 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 s = get_tv_string_chk(tv); /* != NULL if number or string */
2434 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
2436 opt_type = get_option_value(arg, &numval,
2437 &stringval, opt_flags);
2438 if ((opt_type == 1 && *op == '.')
2439 || (opt_type == 0 && *op != '.'))
2440 EMSG2(_(e_letwrong), op);
2441 else
2442 {
2443 if (opt_type == 1) /* number */
2444 {
2445 if (*op == '+')
2446 n = numval + n;
2447 else
2448 n = numval - n;
2449 }
2450 else if (opt_type == 0 && stringval != NULL) /* string */
2451 {
2452 s = concat_str(stringval, s);
2453 vim_free(stringval);
2454 stringval = s;
2455 }
2456 }
2457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 if (s != NULL)
2459 {
2460 set_option_value(arg, n, s, opt_flags);
2461 arg_end = p;
2462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466 }
2467
2468 /*
2469 * ":let @r = expr": Set register contents.
2470 */
2471 else if (*arg == '@')
2472 {
2473 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 if (op != NULL && (*op == '+' || *op == '-'))
2475 EMSG2(_(e_letwrong), op);
2476 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 EMSG(_(e_letunexp));
2479 else
2480 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002481 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 char_u *s;
2483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 p = get_tv_string_chk(tv);
2485 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002487 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (s != NULL)
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 vim_free(s);
2492 }
2493 }
2494 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 arg_end = arg + 1;
2498 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501 }
2502
2503 /*
2504 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002505 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002507 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002509 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002511 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 arg_end = p;
2520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
2524
2525 else
2526 EMSG2(_(e_invarg2), arg);
2527
2528 return arg_end;
2529}
2530
2531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2533 */
2534 static int
2535check_changedtick(arg)
2536 char_u *arg;
2537{
2538 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2539 {
2540 EMSG2(_(e_readonlyvar), arg);
2541 return TRUE;
2542 }
2543 return FALSE;
2544}
2545
2546/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 * Get an lval: variable, Dict item or List item that can be assigned a value
2548 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2549 * "name.key", "name.key[expr]" etc.
2550 * Indexing only works if "name" is an existing List or Dictionary.
2551 * "name" points to the start of the name.
2552 * If "rettv" is not NULL it points to the value to be assigned.
2553 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2554 * wrong; must end in space or cmd separator.
2555 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002556 * flags:
2557 * GLV_QUIET: do not give error messages
2558 * GLV_NO_AUTOLOAD: do not use script autoloading
2559 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002561 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 */
2564 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002566 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002567 typval_T *rettv;
2568 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 int unlet;
2570 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002571 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 char_u *expr_start, *expr_end;
2576 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002577 dictitem_T *v;
2578 typval_T var1;
2579 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002581 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002585 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589
2590 if (skip)
2591 {
2592 /* When skipping just find the end of the name. */
2593 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002594 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 }
2596
2597 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002598 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (expr_start != NULL)
2600 {
2601 /* Don't expand the name when we already know there is an error. */
2602 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2603 && *p != '[' && *p != '.')
2604 {
2605 EMSG(_(e_trailing));
2606 return NULL;
2607 }
2608
2609 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2610 if (lp->ll_exp_name == NULL)
2611 {
2612 /* Report an invalid expression in braces, unless the
2613 * expression evaluation has been cancelled due to an
2614 * aborting error, an interrupt, or an exception. */
2615 if (!aborting() && !quiet)
2616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002617 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 EMSG2(_(e_invarg2), name);
2619 return NULL;
2620 }
2621 }
2622 lp->ll_name = lp->ll_exp_name;
2623 }
2624 else
2625 lp->ll_name = name;
2626
2627 /* Without [idx] or .key we are done. */
2628 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2629 return p;
2630
2631 cc = *p;
2632 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (v == NULL && !quiet)
2635 EMSG2(_(e_undefvar), lp->ll_name);
2636 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 if (v == NULL)
2638 return NULL;
2639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 /*
2641 * Loop until no more [idx] or .key is following.
2642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2647 && !(lp->ll_tv->v_type == VAR_DICT
2648 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
2651 EMSG(_("E689: Can only index a List or Dictionary"));
2652 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
2657 EMSG(_("E708: [:] must come last"));
2658 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 len = -1;
2662 if (*p == '.')
2663 {
2664 key = p + 1;
2665 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2666 ;
2667 if (len == 0)
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_(e_emptykey));
2671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = key + len;
2674 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 else
2676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (*p == ':')
2680 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 else
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 empty1 = FALSE;
2684 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 if (get_tv_string_chk(&var1) == NULL)
2687 {
2688 /* not a number or string */
2689 clear_tv(&var1);
2690 return NULL;
2691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 }
2693
2694 /* Optionally get the second index [ :expr]. */
2695 if (*p == ':')
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (rettv != NULL && (rettv->v_type != VAR_LIST
2706 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 }
2714 p = skipwhite(p + 1);
2715 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 else
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (get_tv_string_chk(&var2) == NULL)
2727 {
2728 /* not a number or string */
2729 if (!empty1)
2730 clear_tv(&var1);
2731 clear_tv(&var2);
2732 return NULL;
2733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (!quiet)
2743 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 if (!empty1)
2745 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
2751 /* Skip to past ']'. */
2752 ++p;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
2757 if (len == -1)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (*key == NUL)
2762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (!quiet)
2764 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 lp->ll_list = NULL;
2770 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002771 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002773 /* When assigning to a scope dictionary check that a function and
2774 * variable name is valid (only variable name unless it is l: or
2775 * g: dictionary). Disallow overwriting a builtin function. */
2776 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 int prevval;
2779 int wrong;
2780
2781 if (len != -1)
2782 {
2783 prevval = key[len];
2784 key[len] = NUL;
2785 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002786 else
2787 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2789 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 || !valid_varname(key);
2792 if (len != -1)
2793 key[len] = prevval;
2794 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 return NULL;
2796 }
2797
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* Can't add "v:" variable. */
2801 if (lp->ll_dict == &vimvardict)
2802 {
2803 EMSG2(_(e_illvar), name);
2804 return NULL;
2805 }
2806
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002807 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 if (len == -1)
2813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 p = NULL;
2824 break;
2825 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002826 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002827 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 return NULL;
2829
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 else
2835 {
2836 /*
2837 * Get the number and item for the only or first index of the List.
2838 */
2839 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
2842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002843 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var1);
2845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_list = lp->ll_tv->vval.v_list;
2848 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2849 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002851 if (lp->ll_n1 < 0)
2852 {
2853 lp->ll_n1 = 0;
2854 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2855 }
2856 }
2857 if (lp->ll_li == NULL)
2858 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
2866 /*
2867 * May need to find the item or absolute index for the second
2868 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 * When no index given: "lp->ll_empty2" is TRUE.
2870 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002874 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 {
2881 if (!quiet)
2882 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 }
2887
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2889 if (lp->ll_n1 < 0)
2890 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2891 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 {
2893 if (!quiet)
2894 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002901 }
2902
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return p;
2904}
2905
2906/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002907 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 */
2909 static void
2910clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002911 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912{
2913 vim_free(lp->ll_exp_name);
2914 vim_free(lp->ll_newkey);
2915}
2916
2917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002923set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002931 listitem_T *ri;
2932 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933
2934 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 cc = *endp;
2939 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940 if (op != NULL && *op != '=')
2941 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002942 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002944 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002945 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002947 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 if ((di == NULL
2950 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2951 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2952 FALSE)))
2953 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 set_var(lp->ll_name, &tv, FALSE);
2955 clear_tv(&tv);
2956 }
2957 }
2958 else
2959 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 else if (tv_check_lock(lp->ll_newkey == NULL
2964 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002965 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 else if (lp->ll_range)
2968 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002969 listitem_T *ll_li = lp->ll_li;
2970 int ll_n1 = lp->ll_n1;
2971
2972 /*
2973 * Check whether any of the list items is locked
2974 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002975 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 return;
2979 ri = ri->li_next;
2980 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2981 break;
2982 ll_li = ll_li->li_next;
2983 ++ll_n1;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the List values to the list items.
2988 */
2989 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002990 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 if (op != NULL && *op != '=')
2992 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2993 else
2994 {
2995 clear_tv(&lp->ll_li->li_tv);
2996 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 ri = ri->li_next;
2999 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3000 break;
3001 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003004 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = NULL;
3007 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 lp->ll_li = lp->ll_li->li_next;
3011 ++lp->ll_n1;
3012 }
3013 if (ri != NULL)
3014 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 else if (lp->ll_empty2
3016 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 : lp->ll_n1 != lp->ll_n2)
3018 EMSG(_("E711: List value has not enough items"));
3019 }
3020 else
3021 {
3022 /*
3023 * Assign to a List or Dictionary item.
3024 */
3025 if (lp->ll_newkey != NULL)
3026 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (op != NULL && *op != '=')
3028 {
3029 EMSG2(_(e_letwrong), op);
3030 return;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003034 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003035 if (di == NULL)
3036 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003037 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3038 {
3039 vim_free(di);
3040 return;
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003043 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 else if (op != NULL && *op != '=')
3045 {
3046 tv_op(lp->ll_tv, rettv, op);
3047 return;
3048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 /*
3053 * Assign the value to the variable or list item.
3054 */
3055 if (copy)
3056 copy_tv(rettv, lp->ll_tv);
3057 else
3058 {
3059 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003060 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 }
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064}
3065
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3068 * Returns OK or FAIL.
3069 */
3070 static int
3071tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 typval_T *tv1;
3073 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 char_u *op;
3075{
3076 long n;
3077 char_u numbuf[NUMBUFLEN];
3078 char_u *s;
3079
3080 /* Can't do anything with a Funcref or a Dict on the right. */
3081 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3082 {
3083 switch (tv1->v_type)
3084 {
3085 case VAR_DICT:
3086 case VAR_FUNC:
3087 break;
3088
3089 case VAR_LIST:
3090 if (*op != '+' || tv2->v_type != VAR_LIST)
3091 break;
3092 /* List += List */
3093 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3094 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3095 return OK;
3096
3097 case VAR_NUMBER:
3098 case VAR_STRING:
3099 if (tv2->v_type == VAR_LIST)
3100 break;
3101 if (*op == '+' || *op == '-')
3102 {
3103 /* nr += nr or nr -= nr*/
3104 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105#ifdef FEAT_FLOAT
3106 if (tv2->v_type == VAR_FLOAT)
3107 {
3108 float_T f = n;
3109
3110 if (*op == '+')
3111 f += tv2->vval.v_float;
3112 else
3113 f -= tv2->vval.v_float;
3114 clear_tv(tv1);
3115 tv1->v_type = VAR_FLOAT;
3116 tv1->vval.v_float = f;
3117 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119#endif
3120 {
3121 if (*op == '+')
3122 n += get_tv_number(tv2);
3123 else
3124 n -= get_tv_number(tv2);
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_NUMBER;
3127 tv1->vval.v_number = n;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 }
3130 else
3131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (tv2->v_type == VAR_FLOAT)
3133 break;
3134
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 /* str .= str */
3136 s = get_tv_string(tv1);
3137 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3138 clear_tv(tv1);
3139 tv1->v_type = VAR_STRING;
3140 tv1->vval.v_string = s;
3141 }
3142 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143
3144#ifdef FEAT_FLOAT
3145 case VAR_FLOAT:
3146 {
3147 float_T f;
3148
3149 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3150 && tv2->v_type != VAR_NUMBER
3151 && tv2->v_type != VAR_STRING))
3152 break;
3153 if (tv2->v_type == VAR_FLOAT)
3154 f = tv2->vval.v_float;
3155 else
3156 f = get_tv_number(tv2);
3157 if (*op == '+')
3158 tv1->vval.v_float += f;
3159 else
3160 tv1->vval.v_float -= f;
3161 }
3162 return OK;
3163#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 }
3165 }
3166
3167 EMSG2(_(e_letwrong), op);
3168 return FAIL;
3169}
3170
3171/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172 * Add a watcher to a list.
3173 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003174 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 list_T *l;
3177 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178{
3179 lw->lw_next = l->lv_watch;
3180 l->lv_watch = lw;
3181}
3182
3183/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003184 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185 * No warning when it isn't found...
3186 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003187 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 list_T *l;
3190 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193
3194 lwp = &l->lv_watch;
3195 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3196 {
3197 if (lw == lwrem)
3198 {
3199 *lwp = lw->lw_next;
3200 break;
3201 }
3202 lwp = &lw->lw_next;
3203 }
3204}
3205
3206/*
3207 * Just before removing an item from a list: advance watchers to the next
3208 * item.
3209 */
3210 static void
3211list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003212 list_T *l;
3213 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
3229eval_for_line(arg, errp, nextcmdp, skip)
3230 char_u *arg;
3231 int *errp;
3232 char_u **nextcmdp;
3233 int skip;
3234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
3293next_for_item(fi_void, arg)
3294 void *fi_void;
3295 char_u *arg;
3296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
3317free_for_info(fi_void)
3318 void *fi_void;
3319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
3333set_context_for_expression(xp, arg, cmdidx)
3334 expand_T *xp;
3335 char_u *arg;
3336 cmdidx_T cmdidx;
3337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
3446ex_call(eap)
3447 exarg_T *eap;
3448{
3449 char_u *arg = eap->arg;
3450 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003452 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 linenr_T lnum;
3456 int doesrange;
3457 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003460 if (eap->skip)
3461 {
3462 /* trans_function_name() doesn't work well when skipping, use eval0()
3463 * instead to skip to any following command, e.g. for:
3464 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003465 ++emsg_skip;
3466 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3467 clear_tv(&rettv);
3468 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 return;
3470 }
3471
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003472 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003473 if (fudi.fd_newkey != NULL)
3474 {
3475 /* Still need to give an error message for missing key. */
3476 EMSG2(_(e_dictkey), fudi.fd_newkey);
3477 vim_free(fudi.fd_newkey);
3478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003479 if (tofree == NULL)
3480 return;
3481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 /* Increase refcount on dictionary, it could get deleted when evaluating
3483 * the arguments. */
3484 if (fudi.fd_dict != NULL)
3485 ++fudi.fd_dict->dv_refcount;
3486
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003487 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003489 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
Bram Moolenaar532c7802005-01-27 14:44:31 +00003491 /* Skip white space to allow ":call func ()". Not good, but required for
3492 * backward compatibility. */
3493 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (*startarg != '(')
3497 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003498 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 goto end;
3500 }
3501
3502 /*
3503 * When skipping, evaluate the function once, to find the end of the
3504 * arguments.
3505 * When the function takes a range, this is discovered after the first
3506 * call, and the loop is broken.
3507 */
3508 if (eap->skip)
3509 {
3510 ++emsg_skip;
3511 lnum = eap->line2; /* do it once, also with an invalid range */
3512 }
3513 else
3514 lnum = eap->line1;
3515 for ( ; lnum <= eap->line2; ++lnum)
3516 {
3517 if (!eap->skip && eap->addr_count > 0)
3518 {
3519 curwin->w_cursor.lnum = lnum;
3520 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003521#ifdef FEAT_VIRTUALEDIT
3522 curwin->w_cursor.coladd = 0;
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 eap->line1, eap->line2, &doesrange,
3528 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003533
3534 /* Handle a function returning a Funcref, Dictionary or List. */
3535 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3536 {
3537 failed = TRUE;
3538 break;
3539 }
3540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (doesrange || eap->skip)
3543 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (aborting())
3550 break;
3551 }
3552 if (eap->skip)
3553 --emsg_skip;
3554
3555 if (!failed)
3556 {
3557 /* Check for trailing illegal characters and a following command. */
3558 if (!ends_excmd(*arg))
3559 {
3560 emsg_severe = TRUE;
3561 EMSG(_(e_trailing));
3562 }
3563 else
3564 eap->nextcmd = check_nextcmd(arg);
3565 }
3566
3567end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
3571
3572/*
3573 * ":unlet[!] var1 ... " command.
3574 */
3575 void
3576ex_unlet(eap)
3577 exarg_T *eap;
3578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
3586ex_lockvar(eap)
3587 exarg_T *eap;
3588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
3607ex_unletlock(eap, argstart, deep)
3608 exarg_T *eap;
3609 char_u *argstart;
3610 int deep;
3611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 int forceit;
3666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
3691 int ll_n1 = lp->ll_n1;
3692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 hashtab_T *ht;
3734 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003735 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003737 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 ht = find_var_ht(name, &varname);
3740 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003742 if (ht == &globvarht)
3743 d = &globvardict;
3744 else if (current_funccal != NULL
3745 && ht == &current_funccal->l_vars.dv_hashtab)
3746 d = &current_funccal->l_vars;
3747 else if (ht == &compat_hashtab)
3748 d = &vimvardict;
3749 else
3750 {
3751 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3752 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3753 }
3754 if (d == NULL)
3755 {
3756 EMSG2(_(e_intern2), "do_unlet()");
3757 return FAIL;
3758 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hi = hash_find(ht, varname);
3760 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003762 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003764 || var_check_ro(di->di_flags, name, FALSE)
3765 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003766 return FAIL;
3767
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003768 delete_var(ht, hi);
3769 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772 if (forceit)
3773 return OK;
3774 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return FAIL;
3776}
3777
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778/*
3779 * Lock or unlock variable indicated by "lp".
3780 * "deep" is the levels to go (-1 for unlimited);
3781 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3782 */
3783 static int
3784do_lock_var(lp, name_end, deep, lock)
3785 lval_T *lp;
3786 char_u *name_end;
3787 int deep;
3788 int lock;
3789{
3790 int ret = OK;
3791 int cc;
3792 dictitem_T *di;
3793
3794 if (deep == 0) /* nothing to do */
3795 return OK;
3796
3797 if (lp->ll_tv == NULL)
3798 {
3799 cc = *name_end;
3800 *name_end = NUL;
3801
3802 /* Normal name or expanded name. */
3803 if (check_changedtick(lp->ll_name))
3804 ret = FAIL;
3805 else
3806 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003807 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003808 if (di == NULL)
3809 ret = FAIL;
3810 else
3811 {
3812 if (lock)
3813 di->di_flags |= DI_FLAGS_LOCK;
3814 else
3815 di->di_flags &= ~DI_FLAGS_LOCK;
3816 item_lock(&di->di_tv, deep, lock);
3817 }
3818 }
3819 *name_end = cc;
3820 }
3821 else if (lp->ll_range)
3822 {
3823 listitem_T *li = lp->ll_li;
3824
3825 /* (un)lock a range of List items. */
3826 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3827 {
3828 item_lock(&li->li_tv, deep, lock);
3829 li = li->li_next;
3830 ++lp->ll_n1;
3831 }
3832 }
3833 else if (lp->ll_list != NULL)
3834 /* (un)lock a List item. */
3835 item_lock(&lp->ll_li->li_tv, deep, lock);
3836 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003837 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838 item_lock(&lp->ll_di->di_tv, deep, lock);
3839
3840 return ret;
3841}
3842
3843/*
3844 * Lock or unlock an item. "deep" is nr of levels to go.
3845 */
3846 static void
3847item_lock(tv, deep, lock)
3848 typval_T *tv;
3849 int deep;
3850 int lock;
3851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
3876 case VAR_LIST:
3877 if ((l = tv->vval.v_list) != NULL)
3878 {
3879 if (lock)
3880 l->lv_lock |= VAR_LOCKED;
3881 else
3882 l->lv_lock &= ~VAR_LOCKED;
3883 if (deep < 0 || deep > 1)
3884 /* recursive: lock/unlock the items the List contains */
3885 for (li = l->lv_first; li != NULL; li = li->li_next)
3886 item_lock(&li->li_tv, deep - 1, lock);
3887 }
3888 break;
3889 case VAR_DICT:
3890 if ((d = tv->vval.v_dict) != NULL)
3891 {
3892 if (lock)
3893 d->dv_lock |= VAR_LOCKED;
3894 else
3895 d->dv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 {
3898 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003900 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3901 {
3902 if (!HASHITEM_EMPTY(hi))
3903 {
3904 --todo;
3905 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3906 }
3907 }
3908 }
3909 }
3910 }
3911 --recurse;
3912}
3913
Bram Moolenaara40058a2005-07-11 22:42:07 +00003914/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003915 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3916 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003917 */
3918 static int
3919tv_islocked(tv)
3920 typval_T *tv;
3921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
3936del_menutrans_vars()
3937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
3964static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
3973cat_prefix_varname(prefix, name)
3974 int prefix;
3975 char_u *name;
3976{
3977 int len;
3978
3979 len = (int)STRLEN(name) + 3;
3980 if (len > varnamebuflen)
3981 {
3982 vim_free(varnamebuf);
3983 len += 10; /* some additional space */
3984 varnamebuf = alloc(len);
3985 if (varnamebuf == NULL)
3986 {
3987 varnamebuflen = 0;
3988 return NULL;
3989 }
3990 varnamebuflen = len;
3991 }
3992 *varnamebuf = prefix;
3993 varnamebuf[1] = ':';
3994 STRCPY(varnamebuf + 2, name);
3995 return varnamebuf;
3996}
3997
3998/*
3999 * Function given to ExpandGeneric() to obtain the list of user defined
4000 * (global/buffer/window/built-in) variable names.
4001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u *
4003get_user_var_name(xp, idx)
4004 expand_T *xp;
4005 int idx;
4006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u **nextcmd;
4131 int evaluate;
4132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int evaluate;
4173{
4174 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004175 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /*
4178 * Get the first variable.
4179 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FAIL;
4182
4183 if ((*arg)[0] == '?')
4184 {
4185 result = FALSE;
4186 if (evaluate)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 int error = FALSE;
4189
4190 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 /*
4198 * Get the second variable.
4199 */
4200 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return FAIL;
4203
4204 /*
4205 * Check for the ":".
4206 */
4207 if ((*arg)[0] != ':')
4208 {
4209 EMSG(_("E109: Missing ':' after '?'"));
4210 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213 }
4214
4215 /*
4216 * Get the third variable.
4217 */
4218 *arg = skipwhite(*arg + 1);
4219 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4220 {
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228
4229 return OK;
4230}
4231
4232/*
4233 * Handle first level expression:
4234 * expr2 || expr2 || expr2 logical OR
4235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
4239 * Return OK or FAIL.
4240 */
4241 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int evaluate;
4246{
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 long result;
4249 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 /*
4259 * Repeat until there is no following "||".
4260 */
4261 first = TRUE;
4262 result = FALSE;
4263 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4264 {
4265 if (evaluate && first)
4266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (error)
4271 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first = FALSE;
4273 }
4274
4275 /*
4276 * Get the second variable.
4277 */
4278 *arg = skipwhite(*arg + 2);
4279 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4280 return FAIL;
4281
4282 /*
4283 * Compute the result.
4284 */
4285 if (evaluate && !result)
4286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (error)
4291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 if (evaluate)
4294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 rettv->v_type = VAR_NUMBER;
4296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle second level expression:
4305 * expr3 && expr3 && expr3 logical AND
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 int evaluate;
4317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "&&".
4331 */
4332 first = TRUE;
4333 result = TRUE;
4334 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval4(arg, &var2, evaluate && result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle third level expression:
4376 * var1 == var2
4377 * var1 =~ var2
4378 * var1 != var2
4379 * var1 !~ var2
4380 * var1 > var2
4381 * var1 >= var2
4382 * var1 < var2
4383 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 * var1 is var2
4385 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
4388 * "arg" is advanced to the next non-white after the recognized expression.
4389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int evaluate;
4397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 char_u *p;
4400 int i;
4401 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int len = 2;
4404 long n1, n2;
4405 char_u *s1, *s2;
4406 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4407 regmatch_T regmatch;
4408 int ic;
4409 char_u *save_cpo;
4410
4411 /*
4412 * Get the first variable.
4413 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416
4417 p = *arg;
4418 switch (p[0])
4419 {
4420 case '=': if (p[1] == '=')
4421 type = TYPE_EQUAL;
4422 else if (p[1] == '~')
4423 type = TYPE_MATCH;
4424 break;
4425 case '!': if (p[1] == '=')
4426 type = TYPE_NEQUAL;
4427 else if (p[1] == '~')
4428 type = TYPE_NOMATCH;
4429 break;
4430 case '>': if (p[1] != '=')
4431 {
4432 type = TYPE_GREATER;
4433 len = 1;
4434 }
4435 else
4436 type = TYPE_GEQUAL;
4437 break;
4438 case '<': if (p[1] != '=')
4439 {
4440 type = TYPE_SMALLER;
4441 len = 1;
4442 }
4443 else
4444 type = TYPE_SEQUAL;
4445 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004446 case 'i': if (p[1] == 's')
4447 {
4448 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4449 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004450 i = p[len];
4451 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 {
4453 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4454 type_is = TRUE;
4455 }
4456 }
4457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
4463 if (type != TYPE_UNKNOWN)
4464 {
4465 /* extra question mark appended: ignore case */
4466 if (p[len] == '?')
4467 {
4468 ic = TRUE;
4469 ++len;
4470 }
4471 /* extra '#' appended: match case */
4472 else if (p[len] == '#')
4473 {
4474 ic = FALSE;
4475 ++len;
4476 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004477 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else
4479 ic = p_ic;
4480
4481 /*
4482 * Get the second variable.
4483 */
4484 *arg = skipwhite(p + len);
4485 if (eval5(arg, &var2, evaluate) == FAIL)
4486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return FAIL;
4489 }
4490
4491 if (evaluate)
4492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004493 if (type_is && rettv->v_type != var2.v_type)
4494 {
4495 /* For "is" a different type always means FALSE, for "notis"
4496 * it means TRUE. */
4497 n1 = (type == TYPE_NEQUAL);
4498 }
4499 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4500 {
4501 if (type_is)
4502 {
4503 n1 = (rettv->v_type == var2.v_type
4504 && rettv->vval.v_list == var2.vval.v_list);
4505 if (type == TYPE_NEQUAL)
4506 n1 = !n1;
4507 }
4508 else if (rettv->v_type != var2.v_type
4509 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4510 {
4511 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004512 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004514 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 clear_tv(rettv);
4516 clear_tv(&var2);
4517 return FAIL;
4518 }
4519 else
4520 {
4521 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004522 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4523 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 }
4528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004529 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4530 {
4531 if (type_is)
4532 {
4533 n1 = (rettv->v_type == var2.v_type
4534 && rettv->vval.v_dict == var2.vval.v_dict);
4535 if (type == TYPE_NEQUAL)
4536 n1 = !n1;
4537 }
4538 else if (rettv->v_type != var2.v_type
4539 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4540 {
4541 if (rettv->v_type != var2.v_type)
4542 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4543 else
4544 EMSG(_("E736: Invalid operation for Dictionary"));
4545 clear_tv(rettv);
4546 clear_tv(&var2);
4547 return FAIL;
4548 }
4549 else
4550 {
4551 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004552 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4553 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004554 if (type == TYPE_NEQUAL)
4555 n1 = !n1;
4556 }
4557 }
4558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4560 {
4561 if (rettv->v_type != var2.v_type
4562 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4563 {
4564 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004565 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004567 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 clear_tv(rettv);
4569 clear_tv(&var2);
4570 return FAIL;
4571 }
4572 else
4573 {
4574 /* Compare two Funcrefs for being equal or unequal. */
4575 if (rettv->vval.v_string == NULL
4576 || var2.vval.v_string == NULL)
4577 n1 = FALSE;
4578 else
4579 n1 = STRCMP(rettv->vval.v_string,
4580 var2.vval.v_string) == 0;
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 }
4585
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586#ifdef FEAT_FLOAT
4587 /*
4588 * If one of the two variables is a float, compare as a float.
4589 * When using "=~" or "!~", always compare as string.
4590 */
4591 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4592 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4593 {
4594 float_T f1, f2;
4595
4596 if (rettv->v_type == VAR_FLOAT)
4597 f1 = rettv->vval.v_float;
4598 else
4599 f1 = get_tv_number(rettv);
4600 if (var2.v_type == VAR_FLOAT)
4601 f2 = var2.vval.v_float;
4602 else
4603 f2 = get_tv_number(&var2);
4604 n1 = FALSE;
4605 switch (type)
4606 {
4607 case TYPE_EQUAL: n1 = (f1 == f2); break;
4608 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4609 case TYPE_GREATER: n1 = (f1 > f2); break;
4610 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4611 case TYPE_SMALLER: n1 = (f1 < f2); break;
4612 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4613 case TYPE_UNKNOWN:
4614 case TYPE_MATCH:
4615 case TYPE_NOMATCH: break; /* avoid gcc warning */
4616 }
4617 }
4618#endif
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 /*
4621 * If one of the two variables is a number, compare as a number.
4622 * When using "=~" or "!~", always compare as string.
4623 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4626 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004627 n1 = get_tv_number(rettv);
4628 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 switch (type)
4630 {
4631 case TYPE_EQUAL: n1 = (n1 == n2); break;
4632 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4633 case TYPE_GREATER: n1 = (n1 > n2); break;
4634 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4635 case TYPE_SMALLER: n1 = (n1 < n2); break;
4636 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4637 case TYPE_UNKNOWN:
4638 case TYPE_MATCH:
4639 case TYPE_NOMATCH: break; /* avoid gcc warning */
4640 }
4641 }
4642 else
4643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 s1 = get_tv_string_buf(rettv, buf1);
4645 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4647 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4648 else
4649 i = 0;
4650 n1 = FALSE;
4651 switch (type)
4652 {
4653 case TYPE_EQUAL: n1 = (i == 0); break;
4654 case TYPE_NEQUAL: n1 = (i != 0); break;
4655 case TYPE_GREATER: n1 = (i > 0); break;
4656 case TYPE_GEQUAL: n1 = (i >= 0); break;
4657 case TYPE_SMALLER: n1 = (i < 0); break;
4658 case TYPE_SEQUAL: n1 = (i <= 0); break;
4659
4660 case TYPE_MATCH:
4661 case TYPE_NOMATCH:
4662 /* avoid 'l' flag in 'cpoptions' */
4663 save_cpo = p_cpo;
4664 p_cpo = (char_u *)"";
4665 regmatch.regprog = vim_regcomp(s2,
4666 RE_MAGIC + RE_STRING);
4667 regmatch.rm_ic = ic;
4668 if (regmatch.regprog != NULL)
4669 {
4670 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004671 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (type == TYPE_NOMATCH)
4673 n1 = !n1;
4674 }
4675 p_cpo = save_cpo;
4676 break;
4677
4678 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4679 }
4680 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 clear_tv(rettv);
4682 clear_tv(&var2);
4683 rettv->v_type = VAR_NUMBER;
4684 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 }
4687
4688 return OK;
4689}
4690
4691/*
4692 * Handle fourth level expression:
4693 * + number addition
4694 * - number subtraction
4695 * . string concatenation
4696 *
4697 * "arg" must point to the first non-white of the expression.
4698 * "arg" is advanced to the next non-white after the recognized expression.
4699 *
4700 * Return OK or FAIL.
4701 */
4702 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004703eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int evaluate;
4707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
4709 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 float_T f1 = 0, f2 = 0;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 char_u *s1, *s2;
4716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4717 char_u *p;
4718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '+', '-' or '.' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '+' && op != '-' && op != '.')
4732 break;
4733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if ((op != '+' || rettv->v_type != VAR_LIST)
4735#ifdef FEAT_FLOAT
4736 && (op == '.' || rettv->v_type != VAR_FLOAT)
4737#endif
4738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
4740 /* For "list + ...", an illegal use of the first operand as
4741 * a number cannot be determined before evaluating the 2nd
4742 * operand: if this is also a list, all is ok.
4743 * For "something . ...", "something - ..." or "non-list + ...",
4744 * we know that the first operand needs to be a string or number
4745 * without evaluating the 2nd operand. So check before to avoid
4746 * side effects after an error. */
4747 if (evaluate && get_tv_string_chk(rettv) == NULL)
4748 {
4749 clear_tv(rettv);
4750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /*
4755 * Get the second variable.
4756 */
4757 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762 }
4763
4764 if (evaluate)
4765 {
4766 /*
4767 * Compute the result.
4768 */
4769 if (op == '.')
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4772 s2 = get_tv_string_buf_chk(&var2, buf2);
4773 if (s2 == NULL) /* type error ? */
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004784 else if (op == '+' && rettv->v_type == VAR_LIST
4785 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 {
4787 /* concatenate Lists */
4788 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4789 &var3) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 clear_tv(rettv);
4796 *rettv = var3;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
4799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f1 = rettv->vval.v_float;
4806 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 else
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 n1 = get_tv_number_chk(rettv, &error);
4812 if (error)
4813 {
4814 /* This can only happen for "list + non-list". For
4815 * "non-list + ..." or "something - ...", we returned
4816 * before evaluating the 2nd operand. */
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 f1 = n1;
4823#endif
4824 }
4825#ifdef FEAT_FLOAT
4826 if (var2.v_type == VAR_FLOAT)
4827 {
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 if (error)
4836 {
4837 clear_tv(rettv);
4838 clear_tv(&var2);
4839 return FAIL;
4840 }
4841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 f2 = n2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847
4848#ifdef FEAT_FLOAT
4849 /* If there is a float on either side the result is a float. */
4850 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4851 {
4852 if (op == '+')
4853 f1 = f1 + f2;
4854 else
4855 f1 = f1 - f2;
4856 rettv->v_type = VAR_FLOAT;
4857 rettv->vval.v_float = f1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#endif
4861 {
4862 if (op == '+')
4863 n1 = n1 + n2;
4864 else
4865 n1 = n1 - n2;
4866 rettv->v_type = VAR_NUMBER;
4867 rettv->vval.v_number = n1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 return OK;
4874}
4875
4876/*
4877 * Handle fifth level expression:
4878 * * number multiplication
4879 * / number division
4880 * % number modulo
4881 *
4882 * "arg" must point to the first non-white of the expression.
4883 * "arg" is advanced to the next non-white after the recognized expression.
4884 *
4885 * Return OK or FAIL.
4886 */
4887 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004892 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int op;
4896 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 int use_float = FALSE;
4899 float_T f1 = 0, f2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 /*
4904 * Get the first variable.
4905 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 /*
4910 * Repeat computing, until no '*', '/' or '%' is following.
4911 */
4912 for (;;)
4913 {
4914 op = **arg;
4915 if (op != '*' && op != '/' && op != '%')
4916 break;
4917
4918 if (evaluate)
4919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#ifdef FEAT_FLOAT
4921 if (rettv->v_type == VAR_FLOAT)
4922 {
4923 f1 = rettv->vval.v_float;
4924 use_float = TRUE;
4925 n1 = 0;
4926 }
4927 else
4928#endif
4929 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 if (error)
4932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
4935 n1 = 0;
4936
4937 /*
4938 * Get the second variable.
4939 */
4940 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 if (evaluate)
4945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (var2.v_type == VAR_FLOAT)
4948 {
4949 if (!use_float)
4950 {
4951 f1 = n1;
4952 use_float = TRUE;
4953 }
4954 f2 = var2.vval.v_float;
4955 n2 = 0;
4956 }
4957 else
4958#endif
4959 {
4960 n2 = get_tv_number_chk(&var2, &error);
4961 clear_tv(&var2);
4962 if (error)
4963 return FAIL;
4964#ifdef FEAT_FLOAT
4965 if (use_float)
4966 f2 = n2;
4967#endif
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 if (op == '*')
4978 f1 = f1 * f2;
4979 else if (op == '/')
4980 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# ifdef VMS
4982 /* VMS crashes on divide by zero, work around it */
4983 if (f2 == 0.0)
4984 {
4985 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004990 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991 }
4992 else
4993 f1 = f1 / f2;
4994# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 /* We rely on the floating point library to handle divide
4996 * by zero to result in "inf" and not a crash. */
4997 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005002 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 return FAIL;
5004 }
5005 rettv->v_type = VAR_FLOAT;
5006 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 n1 = n1 * n2;
5013 else if (op == '/')
5014 {
5015 if (n2 == 0) /* give an error message? */
5016 {
5017 if (n1 == 0)
5018 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5019 else if (n1 < 0)
5020 n1 = -0x7fffffffL;
5021 else
5022 n1 = 0x7fffffffL;
5023 }
5024 else
5025 n1 = n1 / n2;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029 if (n2 == 0) /* give an error message? */
5030 n1 = 0;
5031 else
5032 n1 = n1 % n2;
5033 }
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 return OK;
5041}
5042
5043/*
5044 * Handle sixth level expression:
5045 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005046 * "string" string constant
5047 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * &option-name option value
5049 * @r register contents
5050 * identifier variable value
5051 * function() function call
5052 * $VAR environment variable
5053 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005054 * [expr, expr] List
5055 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * Also handle:
5058 * ! in front logical NOT
5059 * - in front unary minus
5060 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * trailing [] subscript in String or List
5062 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * "arg" must point to the first non-white of the expression.
5065 * "arg" is advanced to the next non-white after the recognized expression.
5066 *
5067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005070eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005074 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 long n;
5077 int len;
5078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *start_leader, *end_leader;
5080 int ret = OK;
5081 char_u *alias;
5082
5083 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * Skip '!' and '-' characters. They are handled later.
5091 */
5092 start_leader = *arg;
5093 while (**arg == '!' || **arg == '-' || **arg == '+')
5094 *arg = skipwhite(*arg + 1);
5095 end_leader = *arg;
5096
5097 switch (**arg)
5098 {
5099 /*
5100 * Number constant.
5101 */
5102 case '0':
5103 case '1':
5104 case '2':
5105 case '3':
5106 case '4':
5107 case '5':
5108 case '6':
5109 case '7':
5110 case '8':
5111 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 {
5113#ifdef FEAT_FLOAT
5114 char_u *p = skipdigits(*arg + 1);
5115 int get_float = FALSE;
5116
5117 /* We accept a float when the format matches
5118 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005119 * strict to avoid backwards compatibility problems.
5120 * Don't look for a float after the "." operator, so that
5121 * ":let vers = 1.2.3" doesn't fail. */
5122 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 get_float = TRUE;
5125 p = skipdigits(p + 2);
5126 if (*p == 'e' || *p == 'E')
5127 {
5128 ++p;
5129 if (*p == '-' || *p == '+')
5130 ++p;
5131 if (!vim_isdigit(*p))
5132 get_float = FALSE;
5133 else
5134 p = skipdigits(p + 1);
5135 }
5136 if (ASCII_ISALPHA(*p) || *p == '.')
5137 get_float = FALSE;
5138 }
5139 if (get_float)
5140 {
5141 float_T f;
5142
5143 *arg += string2float(*arg, &f);
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_FLOAT;
5147 rettv->vval.v_float = f;
5148 }
5149 }
5150 else
5151#endif
5152 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005153 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *arg += len;
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = n;
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * String constant: "string".
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 break;
5175
5176 /*
5177 * List: [expr, expr]
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Dictionary: {key: val, key: val}
5184 */
5185 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5186 break;
5187
5188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Environment variable: $VAR.
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Register contents: @r.
5202 */
5203 case '@': ++*arg;
5204 if (evaluate)
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005207 rettv->vval.v_string = get_reg_contents(**arg,
5208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (**arg != NUL)
5211 ++*arg;
5212 break;
5213
5214 /*
5215 * nested expression: (expression).
5216 */
5217 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (**arg == ')')
5220 ++*arg;
5221 else if (ret == OK)
5222 {
5223 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ret = FAIL;
5226 }
5227 break;
5228
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 if (ret == NOTDONE)
5234 {
5235 /*
5236 * Must be a variable or function name.
5237 * Can also be a curly-braces kind of name: {expr}.
5238 */
5239 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (alias != NULL)
5242 s = alias;
5243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 ret = FAIL;
5246 else
5247 {
5248 if (**arg == '(') /* recursive! */
5249 {
5250 /* If "s" is the name of a variable of type VAR_FUNC
5251 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005252 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253
5254 /* Invoke the function. */
5255 ret = get_func_tv(s, len, rettv, arg,
5256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005258
5259 /* If evaluate is FALSE rettv->v_type was not set in
5260 * get_func_tv, but it's needed in handle_subscript() to parse
5261 * what follows. So set it here. */
5262 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5263 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005264 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005265 rettv->v_type = VAR_FUNC;
5266 }
5267
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 /* Stop the expression evaluation when immediately
5269 * aborting on error, or when an interrupt occurred or
5270 * an exception was thrown but not caught. */
5271 if (aborting())
5272 {
5273 if (ret == OK)
5274 clear_tv(rettv);
5275 ret = FAIL;
5276 }
5277 }
5278 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005279 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 else
5281 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005282 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005283 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg = skipwhite(*arg);
5287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5289 * expr(expr). */
5290 if (ret == OK)
5291 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 /*
5294 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5295 */
5296 if (ret == OK && evaluate && end_leader > start_leader)
5297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 int val = 0;
5300#ifdef FEAT_FLOAT
5301 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 if (rettv->v_type == VAR_FLOAT)
5304 f = rettv->vval.v_float;
5305 else
5306#endif
5307 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 clear_tv(rettv);
5311 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else
5314 {
5315 while (end_leader > start_leader)
5316 {
5317 --end_leader;
5318 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 {
5320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 f = !f;
5323 else
5324#endif
5325 val = !val;
5326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005328 {
5329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 f = -f;
5332 else
5333#endif
5334 val = -val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 {
5340 clear_tv(rettv);
5341 rettv->vval.v_float = f;
5342 }
5343 else
5344#endif
5345 {
5346 clear_tv(rettv);
5347 rettv->v_type = VAR_NUMBER;
5348 rettv->vval.v_number = val;
5349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
5353 return ret;
5354}
5355
5356/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5358 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5360 */
5361 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
5368 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371 long len = -1;
5372 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005376 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 if (verbose)
5379 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 return FAIL;
5381 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005382#ifdef FEAT_FLOAT
5383 else if (rettv->v_type == VAR_FLOAT)
5384 {
5385 if (verbose)
5386 EMSG(_(e_float_as_string));
5387 return FAIL;
5388 }
5389#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
5483 case VAR_NUMBER:
5484 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 len = (long)STRLEN(s);
5487 if (range)
5488 {
5489 /* The resulting variable is a substring. If the indexes
5490 * are out of range the result is empty. */
5491 if (n1 < 0)
5492 {
5493 n1 = len + n1;
5494 if (n1 < 0)
5495 n1 = 0;
5496 }
5497 if (n2 < 0)
5498 n2 = len + n2;
5499 else if (n2 >= len)
5500 n2 = len;
5501 if (n1 >= len || n2 < 0 || n1 > n2)
5502 s = NULL;
5503 else
5504 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5505 }
5506 else
5507 {
5508 /* The resulting variable is a string of a single
5509 * character. If the index is too big or negative the
5510 * result is empty. */
5511 if (n1 >= len || n1 < 0)
5512 s = NULL;
5513 else
5514 s = vim_strnsave(s + n1, 1);
5515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516 clear_tv(rettv);
5517 rettv->v_type = VAR_STRING;
5518 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 break;
5520
5521 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 if (n1 < 0)
5524 n1 = len + n1;
5525 if (!empty1 && (n1 < 0 || n1 >= len))
5526 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005527 /* For a range we allow invalid values and return an empty
5528 * list. A list index out of range is an error. */
5529 if (!range)
5530 {
5531 if (verbose)
5532 EMSGN(_(e_listidx), n1);
5533 return FAIL;
5534 }
5535 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537 if (range)
5538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 list_T *l;
5540 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541
5542 if (n2 < 0)
5543 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005544 else if (n2 >= len)
5545 n2 = len - 1;
5546 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 l = list_alloc();
5549 if (l == NULL)
5550 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 n1 <= n2; ++n1)
5553 {
5554 if (list_append_tv(l, &item->li_tv) == FAIL)
5555 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005556 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 return FAIL;
5558 }
5559 item = item->li_next;
5560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(rettv);
5562 rettv->v_type = VAR_LIST;
5563 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 else
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 }
5572 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573
5574 case VAR_DICT:
5575 if (range)
5576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005577 if (verbose)
5578 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 if (len == -1)
5580 clear_tv(&var1);
5581 return FAIL;
5582 }
5583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005585
5586 if (len == -1)
5587 {
5588 key = get_tv_string(&var1);
5589 if (*key == NUL)
5590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (verbose)
5592 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 }
5597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005598 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005601 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 if (item == NULL)
5605 return FAIL;
5606
5607 copy_tv(&item->di_tv, &var1);
5608 clear_tv(rettv);
5609 *rettv = var1;
5610 }
5611 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 }
5614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005615 return OK;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Get an option value.
5620 * "arg" points to the '&' or '+' before the option name.
5621 * "arg" is advanced to character after the option name.
5622 * Return OK or FAIL.
5623 */
5624 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005627 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int evaluate;
5629{
5630 char_u *option_end;
5631 long numval;
5632 char_u *stringval;
5633 int opt_type;
5634 int c;
5635 int working = (**arg == '+'); /* has("+option") */
5636 int ret = OK;
5637 int opt_flags;
5638
5639 /*
5640 * Isolate the option name and find its value.
5641 */
5642 option_end = find_option_end(arg, &opt_flags);
5643 if (option_end == NULL)
5644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 EMSG2(_("E112: Option name missing: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 if (!evaluate)
5651 {
5652 *arg = option_end;
5653 return OK;
5654 }
5655
5656 c = *option_end;
5657 *option_end = NUL;
5658 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (opt_type == -3) /* invalid name */
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 EMSG2(_("E113: Unknown option: %s"), *arg);
5665 ret = FAIL;
5666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (opt_type == -2) /* hidden string option */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 rettv->v_type = VAR_STRING;
5672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else if (opt_type == -1) /* hidden number option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_NUMBER;
5677 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == 1) /* number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else /* string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 }
5690 else if (working && (opt_type == -2 || opt_type == -1))
5691 ret = FAIL;
5692
5693 *option_end = c; /* put back for error messages */
5694 *arg = option_end;
5695
5696 return ret;
5697}
5698
5699/*
5700 * Allocate a variable for a string constant.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 int evaluate;
5708{
5709 char_u *p;
5710 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int extra = 0;
5712
5713 /*
5714 * Find the end of the string, skipping backslashed characters.
5715 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 if (*p == '\\' && p[1] != NUL)
5719 {
5720 ++p;
5721 /* A "\<x>" form occupies at least 4 characters, and produces up
5722 * to 6 characters: reserve space for 2 extra */
5723 if (*p == '<')
5724 extra += 2;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 if (*p != '"')
5729 {
5730 EMSG2(_("E114: Missing quote: %s"), *arg);
5731 return FAIL;
5732 }
5733
5734 /* If only parsing, set *arg and return here */
5735 if (!evaluate)
5736 {
5737 *arg = p + 1;
5738 return OK;
5739 }
5740
5741 /*
5742 * Copy the string into allocated memory, handling backslashed
5743 * characters.
5744 */
5745 name = alloc((unsigned)(p - *arg + extra));
5746 if (name == NULL)
5747 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 rettv->v_type = VAR_STRING;
5749 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (*p == '\\')
5754 {
5755 switch (*++p)
5756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 case 'b': *name++ = BS; ++p; break;
5758 case 'e': *name++ = ESC; ++p; break;
5759 case 'f': *name++ = FF; ++p; break;
5760 case 'n': *name++ = NL; ++p; break;
5761 case 'r': *name++ = CAR; ++p; break;
5762 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 case 'X': /* hex: "\x1", "\x12" */
5765 case 'x':
5766 case 'u': /* Unicode: "\u0023" */
5767 case 'U':
5768 if (vim_isxdigit(p[1]))
5769 {
5770 int n, nr;
5771 int c = toupper(*p);
5772
5773 if (c == 'X')
5774 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005775 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else
5778 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 nr = 0;
5780 while (--n >= 0 && vim_isxdigit(p[1]))
5781 {
5782 ++p;
5783 nr = (nr << 4) + hex2nr(*p);
5784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_MBYTE
5787 /* For "\u" store the number according to
5788 * 'encoding'. */
5789 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else
5792#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 break;
5796
5797 /* octal: "\1", "\12", "\123" */
5798 case '0':
5799 case '1':
5800 case '2':
5801 case '3':
5802 case '4':
5803 case '5':
5804 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 case '7': *name = *p++ - '0';
5806 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 *name = (*name << 3) + *p++ - '0';
5809 if (*p >= '0' && *p <= '7')
5810 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814
5815 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (extra != 0)
5818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 /* FALLTHROUGH */
5823
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 }
5828 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 *arg = p + 1;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return OK;
5836}
5837
5838/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 * Return OK or FAIL.
5841 */
5842 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 int evaluate;
5847{
5848 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 int reduce = 0;
5851
5852 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 */
5855 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 break;
5861 ++reduce;
5862 ++p;
5863 }
5864 }
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 return FAIL;
5870 }
5871
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 if (!evaluate)
5874 {
5875 *arg = p + 1;
5876 return OK;
5877 }
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 str = alloc((unsigned)((p - *arg) - reduce));
5883 if (str == NULL)
5884 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 rettv->v_type = VAR_STRING;
5886 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++p;
5895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 *arg = p + 1;
5900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return OK;
5902}
5903
5904/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905 * Allocate a variable for a List and fill it from "*arg".
5906 * Return OK or FAIL.
5907 */
5908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005909get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 int evaluate;
5913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l = NULL;
5915 typval_T tv;
5916 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (evaluate)
5919 {
5920 l = list_alloc();
5921 if (l == NULL)
5922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 while (**arg != ']' && **arg != NUL)
5927 {
5928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5929 goto failret;
5930 if (evaluate)
5931 {
5932 item = listitem_alloc();
5933 if (item != NULL)
5934 {
5935 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005936 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 list_append(l, item);
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 else
5940 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 }
5942
5943 if (**arg == ']')
5944 break;
5945 if (**arg != ',')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 goto failret;
5949 }
5950 *arg = skipwhite(*arg + 1);
5951 }
5952
5953 if (**arg != ']')
5954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956failret:
5957 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005958 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 return FAIL;
5960 }
5961
5962 *arg = skipwhite(*arg + 1);
5963 if (evaluate)
5964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 rettv->v_type = VAR_LIST;
5966 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 ++l->lv_refcount;
5968 }
5969
5970 return OK;
5971}
5972
5973/*
5974 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005975 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005977 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978list_alloc()
5979{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 list_T *l;
5981
5982 l = (list_T *)alloc_clear(sizeof(list_T));
5983 if (l != NULL)
5984 {
5985 /* Prepend the list to the list of lists for garbage collection. */
5986 if (first_list != NULL)
5987 first_list->lv_used_prev = l;
5988 l->lv_used_prev = NULL;
5989 l->lv_used_next = first_list;
5990 first_list = l;
5991 }
5992 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996 * Allocate an empty list for a return value.
5997 * Returns OK or FAIL.
5998 */
5999 static int
6000rettv_list_alloc(rettv)
6001 typval_T *rettv;
6002{
6003 list_T *l = list_alloc();
6004
6005 if (l == NULL)
6006 return FAIL;
6007
6008 rettv->vval.v_list = l;
6009 rettv->v_type = VAR_LIST;
6010 ++l->lv_refcount;
6011 return OK;
6012}
6013
6014/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015 * Unreference a list: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006018 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031list_free(l, recurse)
6032 list_T *l;
6033 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062listitem_alloc()
6063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006074 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 vim_free(item);
6076}
6077
6078/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079 * Remove a list item from a List and free it. Also clears the value.
6080 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006081 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 list_T *l;
6084 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006085{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006086 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006087 listitem_free(item);
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Get the number of items in a list.
6092 */
6093 static long
6094list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 if (l == NULL)
6098 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006099 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100}
6101
6102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE when two lists have exactly the same values.
6104 */
6105 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 list_T *l1;
6108 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111{
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006114 if (l1 == NULL || l2 == NULL)
6115 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 if (l1 == l2)
6117 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 if (list_len(l1) != list_len(l2))
6119 return FALSE;
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 for (item1 = l1->lv_first, item2 = l2->lv_first;
6122 item1 != NULL && item2 != NULL;
6123 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return FALSE;
6126 return item1 == NULL && item2 == NULL;
6127}
6128
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006129#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6130 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006131/*
6132 * Return the dictitem that an entry in a hashtable points to.
6133 */
6134 dictitem_T *
6135dict_lookup(hi)
6136 hashitem_T *hi;
6137{
6138 return HI2DI(hi);
6139}
6140#endif
6141
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 * Return TRUE when two dictionaries have exactly the same key/values.
6144 */
6145 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 dict_T *d1;
6148 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 hashitem_T *hi;
6153 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006156 if (d1 == NULL || d2 == NULL)
6157 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006158 if (d1 == d2)
6159 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 if (dict_len(d1) != dict_len(d2))
6161 return FALSE;
6162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006163 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 if (!HASHITEM_EMPTY(hi))
6167 {
6168 item2 = dict_find(d2, hi->hi_key, -1);
6169 if (item2 == NULL)
6170 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 return FALSE;
6173 --todo;
6174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175 }
6176 return TRUE;
6177}
6178
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179static int tv_equal_recurse_limit;
6180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006183 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006184 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 */
6186 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006188 typval_T *tv1;
6189 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 int ic; /* ignore case */
6191 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
6193 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006194 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006196 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 * recursiveness to a limit. We guess they are equal then.
6203 * A fixed limit has the problem of still taking an awful long time.
6204 * Reduce the limit every time running into it. That should work fine for
6205 * deeply linked structures that are not recursively linked and catch
6206 * recursiveness quickly. */
6207 if (!recursive)
6208 tv_equal_recurse_limit = 1000;
6209 if (recursive_cnt >= tv_equal_recurse_limit)
6210 {
6211 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224 ++recursive_cnt;
6225 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6226 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006227 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 case VAR_FUNC:
6230 return (tv1->vval.v_string != NULL
6231 && tv2->vval.v_string != NULL
6232 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6233
6234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006237#ifdef FEAT_FLOAT
6238 case VAR_FLOAT:
6239 return tv1->vval.v_float == tv2->vval.v_float;
6240#endif
6241
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 case VAR_STRING:
6243 s1 = get_tv_string_buf(tv1, buf1);
6244 s2 = get_tv_string_buf(tv2, buf2);
6245 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return TRUE;
6250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long n;
6261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
6338list_find_nr(l, idx, errorp)
6339 list_T *l;
6340 long idx;
6341 int *errorp; /* set to TRUE when something wrong */
6342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
6359list_find_str(l, idx)
6360 list_T *l;
6361 long idx;
6362{
6363 listitem_T *li;
6364
6365 li = list_find(l, idx - 1);
6366 if (li == NULL)
6367 {
6368 EMSGN(_(e_listidx), idx);
6369 return NULL;
6370 }
6371 return get_tv_string(&li->li_tv);
6372}
6373
6374/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375 * Locate "item" list "l" and return its index.
6376 * Returns -1 when "item" is not in the list.
6377 */
6378 static long
6379list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 list_T *l;
6381 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382{
6383 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006384 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385
6386 if (l == NULL)
6387 return -1;
6388 idx = 0;
6389 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6390 ++idx;
6391 if (li == NULL)
6392 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006393 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006394}
6395
6396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 * Append item "item" to the end of list "l".
6398 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006399 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 list_T *l;
6402 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 copy_tv(tv, &li->li_tv);
6435 list_append(l, li);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006440 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 * Return FAIL when out of memory.
6442 */
6443 int
6444list_append_dict(list, dict)
6445 list_T *list;
6446 dict_T *dict;
6447{
6448 listitem_T *li = listitem_alloc();
6449
6450 if (li == NULL)
6451 return FAIL;
6452 li->li_tv.v_type = VAR_DICT;
6453 li->li_tv.v_lock = 0;
6454 li->li_tv.vval.v_dict = dict;
6455 list_append(list, li);
6456 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457 return OK;
6458}
6459
6460/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006462 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 * Returns FAIL when out of memory.
6464 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006465 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 list_T *l;
6468 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 list_append(l, li);
6476 li->li_tv.v_type = VAR_STRING;
6477 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478 if (str == NULL)
6479 li->li_tv.vval.v_string = NULL;
6480 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006481 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006482 return FAIL;
6483 return OK;
6484}
6485
6486/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487 * Append "n" to list "l".
6488 * Returns FAIL when out of memory.
6489 */
6490 static int
6491list_append_number(l, n)
6492 list_T *l;
6493 varnumber_T n;
6494{
6495 listitem_T *li;
6496
6497 li = listitem_alloc();
6498 if (li == NULL)
6499 return FAIL;
6500 li->li_tv.v_type = VAR_NUMBER;
6501 li->li_tv.v_lock = 0;
6502 li->li_tv.vval.v_number = n;
6503 list_append(l, li);
6504 return OK;
6505}
6506
6507/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 * If "item" is NULL append at the end.
6510 * Return FAIL when out of memory.
6511 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006512 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l;
6515 typval_T *tv;
6516 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
6520 if (ni == NULL)
6521 return FAIL;
6522 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523 list_insert(l, ni, item);
6524 return OK;
6525}
6526
6527 void
6528list_insert(l, ni, item)
6529 list_T *l;
6530 listitem_T *ni;
6531 listitem_T *item;
6532{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 if (item == NULL)
6534 /* Append new item at end of list. */
6535 list_append(l, ni);
6536 else
6537 {
6538 /* Insert new item before existing item. */
6539 ni->li_prev = item->li_prev;
6540 ni->li_next = item;
6541 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006544 ++l->lv_idx;
6545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006549 l->lv_idx_item = NULL;
6550 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554}
6555
6556/*
6557 * Extend "l1" with "l2".
6558 * If "bef" is NULL append at the end, otherwise insert before this item.
6559 * Returns FAIL when out of memory.
6560 */
6561 static int
6562list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *l1;
6564 list_T *l2;
6565 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006570 /* We also quit the loop when we have inserted the original item count of
6571 * the list, avoid a hang when we extend a list with itself. */
6572 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6574 return FAIL;
6575 return OK;
6576}
6577
6578/*
6579 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6580 * Return FAIL when out of memory.
6581 */
6582 static int
6583list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *l1;
6585 list_T *l2;
6586 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587{
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006590 if (l1 == NULL || l2 == NULL)
6591 return FAIL;
6592
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 if (l == NULL)
6596 return FAIL;
6597 tv->v_type = VAR_LIST;
6598 tv->vval.v_list = l;
6599
6600 /* append all items from the second list */
6601 return list_extend(l, l2, NULL);
6602}
6603
6604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 * Returns NULL when out of memory.
6609 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006669vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006670 list_T *l;
6671 listitem_T *item;
6672 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 /* notify watchers */
6677 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006679 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 list_fix_watch(l, ip);
6681 if (ip == item2)
6682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684
6685 if (item2->li_next == NULL)
6686 l->lv_last = item->li_prev;
6687 else
6688 item2->li_next->li_prev = item->li_prev;
6689 if (item->li_prev == NULL)
6690 l->lv_first = item2->li_next;
6691 else
6692 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694}
6695
6696/*
6697 * Return an allocated string with the string representation of a list.
6698 * May return NULL.
6699 */
6700 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006703 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704{
6705 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706
6707 if (tv->vval.v_list == NULL)
6708 return NULL;
6709 ga_init2(&ga, (int)sizeof(char), 80);
6710 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 vim_free(ga.ga_data);
6714 return NULL;
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 ga_append(&ga, ']');
6717 ga_append(&ga, NUL);
6718 return (char_u *)ga.ga_data;
6719}
6720
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721typedef struct join_S {
6722 char_u *s;
6723 char_u *tofree;
6724} join_T;
6725
6726 static int
6727list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6728 garray_T *gap; /* to store the result in */
6729 list_T *l;
6730 char_u *sep;
6731 int echo_style;
6732 int copyID;
6733 garray_T *join_gap; /* to keep each list item string */
6734{
6735 int i;
6736 join_T *p;
6737 int len;
6738 int sumlen = 0;
6739 int first = TRUE;
6740 char_u *tofree;
6741 char_u numbuf[NUMBUFLEN];
6742 listitem_T *item;
6743 char_u *s;
6744
6745 /* Stringify each item in the list. */
6746 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6747 {
6748 if (echo_style)
6749 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6750 else
6751 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6752 if (s == NULL)
6753 return FAIL;
6754
6755 len = (int)STRLEN(s);
6756 sumlen += len;
6757
Bram Moolenaarcde88542015-08-11 19:14:00 +02006758 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006759 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6760 if (tofree != NULL || s != numbuf)
6761 {
6762 p->s = s;
6763 p->tofree = tofree;
6764 }
6765 else
6766 {
6767 p->s = vim_strnsave(s, len);
6768 p->tofree = p->s;
6769 }
6770
6771 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006772 if (did_echo_string_emsg) /* recursion error, bail out */
6773 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 }
6775
6776 /* Allocate result buffer with its total size, avoid re-allocation and
6777 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6778 if (join_gap->ga_len >= 2)
6779 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6780 if (ga_grow(gap, sumlen + 2) == FAIL)
6781 return FAIL;
6782
6783 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6784 {
6785 if (first)
6786 first = FALSE;
6787 else
6788 ga_concat(gap, sep);
6789 p = ((join_T *)join_gap->ga_data) + i;
6790
6791 if (p->s != NULL)
6792 ga_concat(gap, p->s);
6793 line_breakcheck();
6794 }
6795
6796 return OK;
6797}
6798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006801 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006804 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006805list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006807 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006809 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006810 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812 garray_T join_ga;
6813 int retval;
6814 join_T *p;
6815 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816
Bram Moolenaard39a7512015-04-16 22:51:22 +02006817 if (l->lv_len < 1)
6818 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6820 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6821
6822 /* Dispose each item in join_ga. */
6823 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = (join_T *)join_ga.ga_data;
6826 for (i = 0; i < join_ga.ga_len; ++i)
6827 {
6828 vim_free(p->tofree);
6829 ++p;
6830 }
6831 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006833
6834 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835}
6836
6837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 * Garbage collection for lists and dictionaries.
6839 *
6840 * We use reference counts to be able to free most items right away when they
6841 * are no longer used. But for composite items it's possible that it becomes
6842 * unused while the reference count is > 0: When there is a recursive
6843 * reference. Example:
6844 * :let l = [1, 2, 3]
6845 * :let d = {9: l}
6846 * :let l[1] = d
6847 *
6848 * Since this is quite unusual we handle this with garbage collection: every
6849 * once in a while find out which lists and dicts are not referenced from any
6850 * variable.
6851 *
6852 * Here is a good reference text about garbage collection (refers to Python
6853 * but it applies to all reference-counting mechanisms):
6854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856
6857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 * Do garbage collection for lists and dicts.
6859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 int
6862garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 buf_T *buf;
6867 win_T *wp;
6868 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006869 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006870 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006872#ifdef FEAT_WINDOWS
6873 tabpage_T *tp;
6874#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876 /* Only do this once. */
6877 want_garbage_collect = FALSE;
6878 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006879 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 /* We advance by two because we add one for items referenced through
6882 * previous_funccal. */
6883 current_copyID += COPYID_INC;
6884 copyID = current_copyID;
6885
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 /*
6887 * 1. Go through all accessible variables and mark all lists and dicts
6888 * with copyID.
6889 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
6891 /* Don't free variables in the previous_funccal list unless they are only
6892 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006893 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6895 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6897 NULL);
6898 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6899 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 }
6901
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 /* script-local variables */
6903 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* buffer-local variables */
6907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006908 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6909 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006915#ifdef FEAT_AUTOCMD
6916 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006919#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006921#ifdef FEAT_WINDOWS
6922 /* tabpage-local variables */
6923 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006924 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6925 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#endif
6927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930
6931 /* function-local variables */
6932 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6935 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 }
6937
Bram Moolenaard812df62008-11-09 12:46:09 +00006938 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006940
Bram Moolenaar1dced572012-04-05 16:54:08 +02006941#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006943#endif
6944
Bram Moolenaardb913952012-06-29 12:54:53 +02006945#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006947#endif
6948
6949#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006951#endif
6952
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 /*
6956 * 2. Free lists and dictionaries that are not referenced.
6957 */
6958 did_free = free_unref_items(copyID);
6959
6960 /*
6961 * 3. Check if any funccal can be freed now.
6962 */
6963 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (can_free_funccal(*pfc, copyID))
6966 {
6967 fc = *pfc;
6968 *pfc = fc->caller;
6969 free_funccal(fc, TRUE);
6970 did_free = TRUE;
6971 did_free_funccal = TRUE;
6972 }
6973 else
6974 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 if (did_free_funccal)
6977 /* When a funccal was freed some more items might be garbage
6978 * collected, so run again. */
6979 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006981 else if (p_verbose > 0)
6982 {
6983 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6984 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985
6986 return did_free;
6987}
6988
6989/*
6990 * Free lists and dictionaries that are no longer referenced.
6991 */
6992 static int
6993free_unref_items(copyID)
6994 int copyID;
6995{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 dict_T *dd, *dd_next;
6997 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 int did_free = FALSE;
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 */
7003 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 {
7005 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007008 /* Free the Dictionary and ordinary items it contains, but don't
7009 * recurse into Lists and Dictionaries, they will be in the list
7010 * of dicts or list of lists. */
7011 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dd = dd_next;
7015 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 * Go through the list of lists and free items without the copyID.
7019 * But don't free a list that has a watcher (used in a for loop), these
7020 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
7022 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 {
7024 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7026 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the List and ordinary items it contains, but don't recurse
7029 * into Lists and Dictionaries, they will be in the list of dicts
7030 * or list of lists. */
7031 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 ll = ll_next;
7035 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 return did_free;
7037}
7038
7039/*
7040 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 * "list_stack" is used to add lists to be marked. Can be NULL.
7042 *
7043 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int
7046set_ref_in_ht(ht, copyID, list_stack)
7047 hashtab_T *ht;
7048 int copyID;
7049 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050{
7051 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 hashtab_T *cur_ht;
7055 ht_stack_T *ht_stack = NULL;
7056 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_ht = ht;
7059 for (;;)
7060 {
7061 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 /* Mark each item in the hashtab. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 todo = (int)cur_ht->ht_used;
7067 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7068 if (!HASHITEM_EMPTY(hi))
7069 {
7070 --todo;
7071 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7072 &ht_stack, list_stack);
7073 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075
7076 if (ht_stack == NULL)
7077 break;
7078
7079 /* take an item from the stack */
7080 cur_ht = ht_stack->ht;
7081 tempitem = ht_stack;
7082 ht_stack = ht_stack->prev;
7083 free(tempitem);
7084 }
7085
7086 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087}
7088
7089/*
7090 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7092 *
7093 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int
7096set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097 list_T *l;
7098 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007099 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 listitem_T *li;
7102 int abort = FALSE;
7103 list_T *cur_l;
7104 list_stack_T *list_stack = NULL;
7105 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 cur_l = l;
7108 for (;;)
7109 {
7110 if (!abort)
7111 /* Mark each item in the list. If the item contains a hashtab
7112 * it is added to ht_stack, if it contains a list it is added to
7113 * list_stack. */
7114 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7115 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7116 ht_stack, &list_stack);
7117 if (list_stack == NULL)
7118 break;
7119
7120 /* take an item from the stack */
7121 cur_l = list_stack->list;
7122 tempitem = list_stack;
7123 list_stack = list_stack->prev;
7124 free(tempitem);
7125 }
7126
7127 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128}
7129
7130/*
7131 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 * "list_stack" is used to add lists to be marked. Can be NULL.
7133 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7134 *
7135 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007136 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 int
7138set_ref_in_item(tv, copyID, ht_stack, list_stack)
7139 typval_T *tv;
7140 int copyID;
7141 ht_stack_T **ht_stack;
7142 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143{
7144 dict_T *dd;
7145 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007147
7148 switch (tv->v_type)
7149 {
7150 case VAR_DICT:
7151 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007152 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007154 /* Didn't see this dict yet. */
7155 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 if (ht_stack == NULL)
7157 {
7158 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7159 }
7160 else
7161 {
7162 ht_stack_T *newitem = (ht_stack_T*)malloc(
7163 sizeof(ht_stack_T));
7164 if (newitem == NULL)
7165 abort = TRUE;
7166 else
7167 {
7168 newitem->ht = &dd->dv_hashtab;
7169 newitem->prev = *ht_stack;
7170 *ht_stack = newitem;
7171 }
7172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007174 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175
7176 case VAR_LIST:
7177 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007178 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007180 /* Didn't see this list yet. */
7181 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 if (list_stack == NULL)
7183 {
7184 abort = set_ref_in_list(ll, copyID, ht_stack);
7185 }
7186 else
7187 {
7188 list_stack_T *newitem = (list_stack_T*)malloc(
7189 sizeof(list_stack_T));
7190 if (newitem == NULL)
7191 abort = TRUE;
7192 else
7193 {
7194 newitem->list = ll;
7195 newitem->prev = *list_stack;
7196 *list_stack = newitem;
7197 }
7198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209dict_alloc()
7210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
7236 static int
7237rettv_dict_alloc(rettv)
7238 typval_T *rettv;
7239{
7240 dict_T *d = dict_alloc();
7241
7242 if (d == NULL)
7243 return FAIL;
7244
7245 rettv->vval.v_dict = d;
7246 rettv->v_type = VAR_DICT;
7247 ++d->dv_refcount;
7248 return OK;
7249}
7250
7251
7252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 * Unreference a Dictionary: decrement the reference count and free it when it
7254 * becomes zero.
7255 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007256 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 if (d != NULL && --d->dv_refcount <= 0)
7261 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262}
7263
7264/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007265 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007266 * Ignores the reference count.
7267 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007268 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007269dict_free(d, recurse)
7270 dict_T *d;
7271 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007275 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277 /* Remove the dict from the list of dicts for garbage collection. */
7278 if (d->dv_used_prev == NULL)
7279 first_dict = d->dv_used_next;
7280 else
7281 d->dv_used_prev->dv_used_next = d->dv_used_next;
7282 if (d->dv_used_next != NULL)
7283 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7284
7285 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 if (!HASHITEM_EMPTY(hi))
7291 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 /* Remove the item before deleting it, just in case there is
7293 * something recursive causing trouble. */
7294 di = HI2DI(hi);
7295 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007296 if (recurse || (di->di_tv.v_type != VAR_LIST
7297 && di->di_tv.v_type != VAR_DICT))
7298 clear_tv(&di->di_tv);
7299 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 --todo;
7301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007303 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 vim_free(d);
7305}
7306
7307/*
7308 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 * The "key" is copied to the new item.
7310 * Note that the value of the item "di_tv" still needs to be initialized!
7311 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007313 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314dictitem_alloc(key)
7315 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007323 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326}
7327
7328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 * Make a copy of a Dictionary item.
7330 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007337 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7338 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 if (di != NULL)
7340 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007342 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 copy_tv(&org->di_tv, &di->di_tv);
7344 }
7345 return di;
7346}
7347
7348/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 * Remove item "item" from Dictionary "dict" and free it.
7350 */
7351 static void
7352dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 dict_T *dict;
7354 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 if (HASHITEM_EMPTY(hi))
7360 EMSG2(_(e_intern2), "dictitem_remove()");
7361 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 dictitem_free(item);
7364}
7365
7366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 * Free a dict item. Also clears the value.
7368 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007369 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007374 if (item->di_flags & DI_FLAGS_ALLOC)
7375 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376}
7377
7378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7380 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007381 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 * Returns NULL when out of memory.
7383 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389{
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 dict_T *copy;
7391 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394
7395 if (orig == NULL)
7396 return NULL;
7397
7398 copy = dict_alloc();
7399 if (copy != NULL)
7400 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 if (copyID != 0)
7402 {
7403 orig->dv_copyID = copyID;
7404 orig->dv_copydict = copy;
7405 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 di = dictitem_alloc(hi->hi_key);
7414 if (di == NULL)
7415 break;
7416 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 {
7418 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7419 copyID) == FAIL)
7420 {
7421 vim_free(di);
7422 break;
7423 }
7424 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 else
7426 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7427 if (dict_add(copy, di) == FAIL)
7428 {
7429 dictitem_free(di);
7430 break;
7431 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007433 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 if (todo > 0)
7437 {
7438 dict_unref(copy);
7439 copy = NULL;
7440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441 }
7442
7443 return copy;
7444}
7445
7446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007450 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dict_T *d;
7453 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454{
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456}
7457
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007459 * Add a number or string entry to dictionary "d".
7460 * When "str" is NULL use number "nr", otherwise use "str".
7461 * Returns FAIL when out of memory and when key already exists.
7462 */
7463 int
7464dict_add_nr_str(d, key, nr, str)
7465 dict_T *d;
7466 char *key;
7467 long nr;
7468 char_u *str;
7469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 if (str == NULL)
7477 {
7478 item->di_tv.v_type = VAR_NUMBER;
7479 item->di_tv.vval.v_number = nr;
7480 }
7481 else
7482 {
7483 item->di_tv.v_type = VAR_STRING;
7484 item->di_tv.vval.v_string = vim_strsave(str);
7485 }
7486 if (dict_add(d, item) == FAIL)
7487 {
7488 dictitem_free(item);
7489 return FAIL;
7490 }
7491 return OK;
7492}
7493
7494/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007495 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007496 * Returns FAIL when out of memory and when key already exists.
7497 */
7498 int
7499dict_add_list(d, key, list)
7500 dict_T *d;
7501 char *key;
7502 list_T *list;
7503{
7504 dictitem_T *item;
7505
7506 item = dictitem_alloc((char_u *)key);
7507 if (item == NULL)
7508 return FAIL;
7509 item->di_tv.v_lock = 0;
7510 item->di_tv.v_type = VAR_LIST;
7511 item->di_tv.vval.v_list = list;
7512 if (dict_add(d, item) == FAIL)
7513 {
7514 dictitem_free(item);
7515 return FAIL;
7516 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007517 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007518 return OK;
7519}
7520
7521/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007522 * Get the number of items in a Dictionary.
7523 */
7524 static long
7525dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007527{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007528 if (d == NULL)
7529 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531}
7532
7533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 * Find item "key[len]" in Dictionary "d".
7535 * If "len" is negative use strlen(key).
7536 * Returns NULL when not found.
7537 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007538 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 char_u *key;
7542 int len;
7543{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544#define AKEYLEN 200
7545 char_u buf[AKEYLEN];
7546 char_u *akey;
7547 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 if (len < 0)
7551 akey = key;
7552 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 tofree = akey = vim_strnsave(key, len);
7555 if (akey == NULL)
7556 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007557 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 else
7559 {
7560 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007561 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 akey = buf;
7563 }
7564
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 vim_free(tofree);
7567 if (HASHITEM_EMPTY(hi))
7568 return NULL;
7569 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570}
7571
7572/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007573 * Get a string item from a dictionary.
7574 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007575 * Returns NULL if the entry doesn't exist or out of memory.
7576 */
7577 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007578get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579 dict_T *d;
7580 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007581 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582{
7583 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007584 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007585
7586 di = dict_find(d, key, -1);
7587 if (di == NULL)
7588 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007589 s = get_tv_string(&di->di_tv);
7590 if (save && s != NULL)
7591 s = vim_strsave(s);
7592 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007593}
7594
7595/*
7596 * Get a number item from a dictionary.
7597 * Returns 0 if the entry doesn't exist or out of memory.
7598 */
7599 long
7600get_dict_number(d, key)
7601 dict_T *d;
7602 char_u *key;
7603{
7604 dictitem_T *di;
7605
7606 di = dict_find(d, key, -1);
7607 if (di == NULL)
7608 return 0;
7609 return get_tv_number(&di->di_tv);
7610}
7611
7612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 * Return an allocated string with the string representation of a Dictionary.
7614 * May return NULL.
7615 */
7616 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007617dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
7680get_dict_tv(arg, rettv, evaluate)
7681 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 int evaluate;
7684{
Bram Moolenaar33570922005-01-25 22:26:29 +00007685 dict_T *d = NULL;
7686 typval_T tvkey;
7687 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007688 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 /*
7694 * First check if it's not a curly-braces thing: {expr}.
7695 * Must do this without evaluating, otherwise a function may be called
7696 * twice. Unfortunately this means we need to call eval1() twice for the
7697 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700 if (*start != '}')
7701 {
7702 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7703 return FAIL;
7704 if (*start == '}')
7705 return NOTDONE;
7706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707
7708 if (evaluate)
7709 {
7710 d = dict_alloc();
7711 if (d == NULL)
7712 return FAIL;
7713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 tvkey.v_type = VAR_UNKNOWN;
7715 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 *arg = skipwhite(*arg + 1);
7718 while (**arg != '}' && **arg != NUL)
7719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 if (**arg != ':')
7723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007724 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 goto failret;
7727 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007728 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007730 key = get_tv_string_buf_chk(&tvkey, buf);
7731 if (key == NULL || *key == NUL)
7732 {
7733 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7734 if (key != NULL)
7735 EMSG(_(e_emptykey));
7736 clear_tv(&tvkey);
7737 goto failret;
7738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7743 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007744 if (evaluate)
7745 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 goto failret;
7747 }
7748 if (evaluate)
7749 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007750 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 if (item != NULL)
7752 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007753 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007754 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 clear_tv(&tv);
7756 goto failret;
7757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 item = dictitem_alloc(key);
7759 clear_tv(&tvkey);
7760 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007763 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 if (dict_add(d, item) == FAIL)
7765 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 }
7767 }
7768
7769 if (**arg == '}')
7770 break;
7771 if (**arg != ',')
7772 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007773 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774 goto failret;
7775 }
7776 *arg = skipwhite(*arg + 1);
7777 }
7778
7779 if (**arg != '}')
7780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007781 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782failret:
7783 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007784 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 return FAIL;
7786 }
7787
7788 *arg = skipwhite(*arg + 1);
7789 if (evaluate)
7790 {
7791 rettv->v_type = VAR_DICT;
7792 rettv->vval.v_dict = d;
7793 ++d->dv_refcount;
7794 }
7795
7796 return OK;
7797}
7798
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007800 * Return a string with the string representation of a variable.
7801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007802 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007803 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007804 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007805 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 */
7807 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 static int recurse = 0;
7815 char_u *r = NULL;
7816
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007819 if (!did_echo_string_emsg)
7820 {
7821 /* Only give this message once for a recursive call to avoid
7822 * flooding the user with errors. And stop iterating over lists
7823 * and dicts. */
7824 did_echo_string_emsg = TRUE;
7825 EMSG(_("E724: variable nested too deep for displaying"));
7826 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007828 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 }
7830 ++recurse;
7831
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007832 switch (tv->v_type)
7833 {
7834 case VAR_FUNC:
7835 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007836 r = tv->vval.v_string;
7837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_list == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"[...]";
7849 }
7850 else
7851 {
7852 tv->vval.v_list->lv_copyID = copyID;
7853 *tofree = list2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaar8c711452005-01-14 21:53:12 +00007858 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859 if (tv->vval.v_dict == NULL)
7860 {
7861 *tofree = NULL;
7862 r = NULL;
7863 }
7864 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7865 {
7866 *tofree = NULL;
7867 r = (char_u *)"{...}";
7868 }
7869 else
7870 {
7871 tv->vval.v_dict->dv_copyID = copyID;
7872 *tofree = dict2string(tv, copyID);
7873 r = *tofree;
7874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007876
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 case VAR_STRING:
7878 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 *tofree = NULL;
7880 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#ifdef FEAT_FLOAT
7884 case VAR_FLOAT:
7885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7887 r = numbuf;
7888 break;
7889#endif
7890
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895
Bram Moolenaar8502c702014-06-17 12:51:16 +02007896 if (--recurse == 0)
7897 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899}
7900
7901/*
7902 * Return a string with the string representation of a variable.
7903 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7904 * "numbuf" is used for a number.
7905 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007906 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 */
7908 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 char_u **tofree;
7912 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007913 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914{
7915 switch (tv->v_type)
7916 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007917 case VAR_FUNC:
7918 *tofree = string_quote(tv->vval.v_string, TRUE);
7919 return *tofree;
7920 case VAR_STRING:
7921 *tofree = string_quote(tv->vval.v_string, FALSE);
7922 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007923#ifdef FEAT_FLOAT
7924 case VAR_FLOAT:
7925 *tofree = NULL;
7926 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7927 return numbuf;
7928#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007933 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007937}
7938
7939/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 * Return string "str" in ' quotes, doubling ' characters.
7941 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 */
7944 static char_u *
7945string_quote(str, function)
7946 char_u *str;
7947 int function;
7948{
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950 char_u *p, *r, *s;
7951
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 len = (function ? 13 : 3);
7953 if (str != NULL)
7954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007955 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 for (p = str; *p != NUL; mb_ptr_adv(p))
7957 if (*p == '\'')
7958 ++len;
7959 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960 s = r = alloc(len);
7961 if (r != NULL)
7962 {
7963 if (function)
7964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 r += 10;
7967 }
7968 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007970 if (str != NULL)
7971 for (p = str; *p != NUL; )
7972 {
7973 if (*p == '\'')
7974 *r++ = '\'';
7975 MB_COPY_CHAR(p, r);
7976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 if (function)
7979 *r++ = ')';
7980 *r++ = NUL;
7981 }
7982 return s;
7983}
7984
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
7986/*
7987 * Convert the string "text" to a floating point number.
7988 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7989 * this always uses a decimal point.
7990 * Returns the length of the text that was consumed.
7991 */
7992 static int
7993string2float(text, value)
7994 char_u *text;
7995 float_T *value; /* result stored here */
7996{
7997 char *s = (char *)text;
7998 float_T f;
7999
8000 f = strtod(s, &s);
8001 *value = f;
8002 return (int)((char_u *)s - text);
8003}
8004#endif
8005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * Get the value of an environment variable.
8008 * "arg" is pointing to the '$'. It is advanced to after the name.
8009 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008010 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
8012 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int evaluate;
8017{
8018 char_u *string = NULL;
8019 int len;
8020 int cc;
8021 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008022 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 ++*arg;
8025 name = *arg;
8026 len = get_env_len(arg);
8027 if (evaluate)
8028 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008029 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008030 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008032 cc = name[len];
8033 name[len] = NUL;
8034 /* first try vim_getenv(), fast for normal environment vars */
8035 string = vim_getenv(name, &mustfree);
8036 if (string != NULL && *string != NUL)
8037 {
8038 if (!mustfree)
8039 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 else
8042 {
8043 if (mustfree)
8044 vim_free(string);
8045
8046 /* next try expanding things like $VIM and ${HOME} */
8047 string = expand_env_save(name - 1);
8048 if (string != NULL && *string == '$')
8049 {
8050 vim_free(string);
8051 string = NULL;
8052 }
8053 }
8054 name[len] = cc;
8055
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008056 rettv->v_type = VAR_STRING;
8057 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059
8060 return OK;
8061}
8062
8063/*
8064 * Array with names and number of arguments of all internal functions
8065 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8066 */
8067static struct fst
8068{
8069 char *f_name; /* function name */
8070 char f_min_argc; /* minimal number of arguments */
8071 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008073 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074} functions[] =
8075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008080 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008081 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008082 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"append", 2, 2, f_append},
8084 {"argc", 0, 0, f_argc},
8085 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008086 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008087 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008088#ifdef FEAT_FLOAT
8089 {"asin", 1, 1, f_asin}, /* WJMc */
8090#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008091 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008092 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008093 {"assert_false", 1, 2, f_assert_false},
8094 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#ifdef FEAT_FLOAT
8096 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008097 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"bufexists", 1, 1, f_bufexists},
8102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8105 {"buflisted", 1, 1, f_buflisted},
8106 {"bufloaded", 1, 1, f_bufloaded},
8107 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008108 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufwinnr", 1, 1, f_bufwinnr},
8110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008112 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"ceil", 1, 1, f_ceil},
8116#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008117 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008118 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008120 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008122#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008123 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008124 {"complete_add", 1, 1, f_complete_add},
8125 {"complete_check", 0, 0, f_complete_check},
8126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008131 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008133 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008135 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008136 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"delete", 1, 1, f_delete},
8138 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008139 {"diff_filler", 1, 1, f_diff_filler},
8140 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008141 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008143 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"eventhandler", 0, 0, f_eventhandler},
8145 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008146 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008148#ifdef FEAT_FLOAT
8149 {"exp", 1, 1, f_exp},
8150#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008151 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008152 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008153 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8155 {"filereadable", 1, 1, f_filereadable},
8156 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008157 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008158 {"finddir", 1, 3, f_finddir},
8159 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#ifdef FEAT_FLOAT
8161 {"float2nr", 1, 1, f_float2nr},
8162 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008163 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008165 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"fnamemodify", 2, 2, f_fnamemodify},
8167 {"foldclosed", 1, 1, f_foldclosed},
8168 {"foldclosedend", 1, 1, f_foldclosedend},
8169 {"foldlevel", 1, 1, f_foldlevel},
8170 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008171 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008174 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008175 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008176 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008177 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"getchar", 0, 1, f_getchar},
8179 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008180 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"getcmdline", 0, 0, f_getcmdline},
8182 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008183 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008184 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008185 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008187 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008188 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"getfsize", 1, 1, f_getfsize},
8190 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008191 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008193 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008194 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008195 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008196 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008197 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008198 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008200 {"gettabvar", 2, 3, f_gettabvar},
8201 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"getwinposx", 0, 0, f_getwinposx},
8203 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008204 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008205 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008206 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008207 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008209 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008210 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008211 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8213 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8214 {"histadd", 2, 2, f_histadd},
8215 {"histdel", 1, 2, f_histdel},
8216 {"histget", 1, 2, f_histget},
8217 {"histnr", 1, 1, f_histnr},
8218 {"hlID", 1, 1, f_hlID},
8219 {"hlexists", 1, 1, f_hlexists},
8220 {"hostname", 0, 0, f_hostname},
8221 {"iconv", 3, 3, f_iconv},
8222 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008224 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008226 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"inputrestore", 0, 0, f_inputrestore},
8228 {"inputsave", 0, 0, f_inputsave},
8229 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008231 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008233 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008236 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"libcall", 3, 3, f_libcall},
8240 {"libcallnr", 3, 3, f_libcallnr},
8241 {"line", 1, 1, f_line},
8242 {"line2byte", 1, 1, f_line2byte},
8243 {"lispindent", 1, 1, f_lispindent},
8244 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008245#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008246 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247 {"log10", 1, 1, f_log10},
8248#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008249#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008250 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008251#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008253 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008254 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008255 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008256 {"matchadd", 2, 5, f_matchadd},
8257 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008258 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008259 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008260 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008261 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008262 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008263 {"max", 1, 1, f_max},
8264 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008265#ifdef vim_mkdir
8266 {"mkdir", 1, 3, f_mkdir},
8267#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008269#ifdef FEAT_MZSCHEME
8270 {"mzeval", 1, 1, f_mzeval},
8271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008273 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008274 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008275 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008276#ifdef FEAT_FLOAT
8277 {"pow", 2, 2, f_pow},
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008280 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008281 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008282#ifdef FEAT_PYTHON3
8283 {"py3eval", 1, 1, f_py3eval},
8284#endif
8285#ifdef FEAT_PYTHON
8286 {"pyeval", 1, 1, f_pyeval},
8287#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008288 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008289 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008290 {"reltime", 0, 2, f_reltime},
8291 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"remote_expr", 2, 3, f_remote_expr},
8293 {"remote_foreground", 1, 1, f_remote_foreground},
8294 {"remote_peek", 1, 2, f_remote_peek},
8295 {"remote_read", 1, 1, f_remote_read},
8296 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008297 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008299 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302#ifdef FEAT_FLOAT
8303 {"round", 1, 1, f_round},
8304#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008305 {"screenattr", 2, 2, f_screenattr},
8306 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008307 {"screencol", 0, 0, f_screencol},
8308 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008309 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008310 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008311 {"searchpair", 3, 7, f_searchpair},
8312 {"searchpairpos", 3, 7, f_searchpairpos},
8313 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"server2client", 2, 2, f_server2client},
8315 {"serverlist", 0, 0, f_serverlist},
8316 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008317 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {"setcmdpos", 1, 1, f_setcmdpos},
8319 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008320 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008321 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008322 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008323 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008325 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008326 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008328#ifdef FEAT_CRYPT
8329 {"sha256", 1, 1, f_sha256},
8330#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008331 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008332 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
8335 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008336 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008337#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008338 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008339 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008340 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008341 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008342 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008343#ifdef FEAT_FLOAT
8344 {"sqrt", 1, 1, f_sqrt},
8345 {"str2float", 1, 1, f_str2float},
8346#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008347 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008348 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008349 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350#ifdef HAVE_STRFTIME
8351 {"strftime", 1, 2, f_strftime},
8352#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"strlen", 1, 1, f_strlen},
8356 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008357 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008359 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008360 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"substitute", 4, 4, f_substitute},
8362 {"synID", 3, 3, f_synID},
8363 {"synIDattr", 2, 3, f_synIDattr},
8364 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008365 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008366 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008367 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008368 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008369 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008370 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008371 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008372 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008373 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374#ifdef FEAT_FLOAT
8375 {"tan", 1, 1, f_tan},
8376 {"tanh", 1, 1, f_tanh},
8377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008379 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"tolower", 1, 1, f_tolower},
8381 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008382 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#ifdef FEAT_FLOAT
8384 {"trunc", 1, 1, f_trunc},
8385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008387 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008388 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008389 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008390 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"virtcol", 1, 1, f_virtcol},
8392 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008393 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"winbufnr", 1, 1, f_winbufnr},
8395 {"wincol", 0, 0, f_wincol},
8396 {"winheight", 1, 1, f_winheight},
8397 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008398 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008400 {"winrestview", 1, 1, f_winrestview},
8401 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008403 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008404 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008405 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406};
8407
8408#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8409
8410/*
8411 * Function given to ExpandGeneric() to obtain the list of internal
8412 * or user defined function names.
8413 */
8414 char_u *
8415get_function_name(xp, idx)
8416 expand_T *xp;
8417 int idx;
8418{
8419 static int intidx = -1;
8420 char_u *name;
8421
8422 if (idx == 0)
8423 intidx = -1;
8424 if (intidx < 0)
8425 {
8426 name = get_user_func_name(xp, idx);
8427 if (name != NULL)
8428 return name;
8429 }
8430 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8431 {
8432 STRCPY(IObuff, functions[intidx].f_name);
8433 STRCAT(IObuff, "(");
8434 if (functions[intidx].f_max_argc == 0)
8435 STRCAT(IObuff, ")");
8436 return IObuff;
8437 }
8438
8439 return NULL;
8440}
8441
8442/*
8443 * Function given to ExpandGeneric() to obtain the list of internal or
8444 * user defined variable or function names.
8445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 char_u *
8447get_expr_name(xp, idx)
8448 expand_T *xp;
8449 int idx;
8450{
8451 static int intidx = -1;
8452 char_u *name;
8453
8454 if (idx == 0)
8455 intidx = -1;
8456 if (intidx < 0)
8457 {
8458 name = get_function_name(xp, idx);
8459 if (name != NULL)
8460 return name;
8461 }
8462 return get_user_var_name(xp, ++intidx);
8463}
8464
8465#endif /* FEAT_CMDL_COMPL */
8466
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008467#if defined(EBCDIC) || defined(PROTO)
8468/*
8469 * Compare struct fst by function name.
8470 */
8471 static int
8472compare_func_name(s1, s2)
8473 const void *s1;
8474 const void *s2;
8475{
8476 struct fst *p1 = (struct fst *)s1;
8477 struct fst *p2 = (struct fst *)s2;
8478
8479 return STRCMP(p1->f_name, p2->f_name);
8480}
8481
8482/*
8483 * Sort the function table by function name.
8484 * The sorting of the table above is ASCII dependant.
8485 * On machines using EBCDIC we have to sort it.
8486 */
8487 static void
8488sortFunctions()
8489{
8490 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8491
8492 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8493}
8494#endif
8495
8496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497/*
8498 * Find internal function in table above.
8499 * Return index, or -1 if not found
8500 */
8501 static int
8502find_internal_func(name)
8503 char_u *name; /* name of the function */
8504{
8505 int first = 0;
8506 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8507 int cmp;
8508 int x;
8509
8510 /*
8511 * Find the function name in the table. Binary search.
8512 */
8513 while (first <= last)
8514 {
8515 x = first + ((unsigned)(last - first) >> 1);
8516 cmp = STRCMP(name, functions[x].f_name);
8517 if (cmp < 0)
8518 last = x - 1;
8519 else if (cmp > 0)
8520 first = x + 1;
8521 else
8522 return x;
8523 }
8524 return -1;
8525}
8526
8527/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8529 * name it contains, otherwise return "name".
8530 */
8531 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008532deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 char_u *name;
8534 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008535 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536{
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008538 int cc;
8539
8540 cc = name[*lenp];
8541 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008542 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 {
8548 *lenp = 0;
8549 return (char_u *)""; /* just in case */
8550 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008551 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008552 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 }
8554
8555 return name;
8556}
8557
8558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 * Allocate a variable for the result of a function.
8560 * Return OK or FAIL.
8561 */
8562 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8564 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 char_u *name; /* name of the function */
8566 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 char_u **arg; /* argument, pointing to the '(' */
8569 linenr_T firstline; /* first line of range */
8570 linenr_T lastline; /* last line of range */
8571 int *doesrange; /* return: function handled range */
8572 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 char_u *argp;
8576 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008577 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 int argcount = 0; /* number of arguments found */
8579
8580 /*
8581 * Get the arguments.
8582 */
8583 argp = *arg;
8584 while (argcount < MAX_FUNC_ARGS)
8585 {
8586 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8587 if (*argp == ')' || *argp == ',' || *argp == NUL)
8588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8590 {
8591 ret = FAIL;
8592 break;
8593 }
8594 ++argcount;
8595 if (*argp != ',')
8596 break;
8597 }
8598 if (*argp == ')')
8599 ++argp;
8600 else
8601 ret = FAIL;
8602
8603 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008607 {
8608 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008609 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 *arg = skipwhite(argp);
8618 return ret;
8619}
8620
8621
8622/*
8623 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008624 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008625 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 */
8627 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008628call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008629 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008630 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008632 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008634 typval_T *argvars; /* vars for arguments, must have "argcount"
8635 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 linenr_T firstline; /* first line of range */
8637 linenr_T lastline; /* last line of range */
8638 int *doesrange; /* return: function handled range */
8639 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#define ERROR_UNKNOWN 0
8644#define ERROR_TOOMANY 1
8645#define ERROR_TOOFEW 2
8646#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008647#define ERROR_DICT 4
8648#define ERROR_NONE 5
8649#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 int error = ERROR_NONE;
8651 int i;
8652 int llen;
8653 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#define FLEN_FIXED 40
8655 char_u fname_buf[FLEN_FIXED + 1];
8656 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008657 char_u *name;
8658
8659 /* Make a copy of the name, if it comes from a funcref variable it could
8660 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008661 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008662 if (name == NULL)
8663 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 /*
8666 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8667 * Change <SNR>123_name() to K_SNR 123_name().
8668 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 llen = eval_fname_script(name);
8671 if (llen > 0)
8672 {
8673 fname_buf[0] = K_SPECIAL;
8674 fname_buf[1] = KS_EXTRA;
8675 fname_buf[2] = (int)KE_SNR;
8676 i = 3;
8677 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8678 {
8679 if (current_SID <= 0)
8680 error = ERROR_SCRIPT;
8681 else
8682 {
8683 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8684 i = (int)STRLEN(fname_buf);
8685 }
8686 }
8687 if (i + STRLEN(name + llen) < FLEN_FIXED)
8688 {
8689 STRCPY(fname_buf + i, name + llen);
8690 fname = fname_buf;
8691 }
8692 else
8693 {
8694 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8695 if (fname == NULL)
8696 error = ERROR_OTHER;
8697 else
8698 {
8699 mch_memmove(fname, fname_buf, (size_t)i);
8700 STRCPY(fname + i, name + llen);
8701 }
8702 }
8703 }
8704 else
8705 fname = name;
8706
8707 *doesrange = FALSE;
8708
8709
8710 /* execute the function if no errors detected and executing */
8711 if (evaluate && error == ERROR_NONE)
8712 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 char_u *rfname = fname;
8714
8715 /* Ignore "g:" before a function name. */
8716 if (fname[0] == 'g' && fname[1] == ':')
8717 rfname = fname + 2;
8718
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008719 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8720 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 error = ERROR_UNKNOWN;
8722
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008723 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {
8725 /*
8726 * User defined function.
8727 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008729
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731 /* Trigger FuncUndefined event, may load the function. */
8732 if (fp == NULL
8733 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008734 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008735 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008737 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008738 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 }
8740#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008741 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008742 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008743 {
8744 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008745 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008746 }
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (fp != NULL)
8749 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008750 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008752 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008754 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008756 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 else
8759 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008760 int did_save_redo = FALSE;
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 /*
8763 * Call the user function.
8764 * Save and restore search patterns, script variables and
8765 * redo buffer.
8766 */
8767 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008768#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008769 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008770#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008771 {
8772 saveRedobuff();
8773 did_save_redo = TRUE;
8774 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008775 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008777 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008778 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8779 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8780 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008781 /* Function was unreferenced while being used, free it
8782 * now. */
8783 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008784 if (did_save_redo)
8785 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 restore_search_patterns();
8787 error = ERROR_NONE;
8788 }
8789 }
8790 }
8791 else
8792 {
8793 /*
8794 * Find the function name in the table, call its implementation.
8795 */
8796 i = find_internal_func(fname);
8797 if (i >= 0)
8798 {
8799 if (argcount < functions[i].f_min_argc)
8800 error = ERROR_TOOFEW;
8801 else if (argcount > functions[i].f_max_argc)
8802 error = ERROR_TOOMANY;
8803 else
8804 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008805 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 error = ERROR_NONE;
8808 }
8809 }
8810 }
8811 /*
8812 * The function call (or "FuncUndefined" autocommand sequence) might
8813 * have been aborted by an error, an interrupt, or an explicitly thrown
8814 * exception that has not been caught so far. This situation can be
8815 * tested for by calling aborting(). For an error in an internal
8816 * function or for the "E132" error in call_user_func(), however, the
8817 * throw point at which the "force_abort" flag (temporarily reset by
8818 * emsg()) is normally updated has not been reached yet. We need to
8819 * update that flag first to make aborting() reliable.
8820 */
8821 update_force_abort();
8822 }
8823 if (error == ERROR_NONE)
8824 ret = OK;
8825
8826 /*
8827 * Report an error unless the argument evaluation or function call has been
8828 * cancelled due to an aborting error, an interrupt, or an exception.
8829 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 if (!aborting())
8831 {
8832 switch (error)
8833 {
8834 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 break;
8837 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008838 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008839 break;
8840 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008841 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008842 name);
8843 break;
8844 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008845 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008846 name);
8847 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008848 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008849 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 name);
8851 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008852 }
8853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (fname != name && fname != fname_buf)
8856 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008857 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859 return ret;
8860}
8861
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008862/*
8863 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008864 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 */
8866 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008867emsg_funcname(ermsg, name)
8868 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008869 char_u *name;
8870{
8871 char_u *p;
8872
8873 if (*name == K_SPECIAL)
8874 p = concat_str((char_u *)"<SNR>", name + 3);
8875 else
8876 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008877 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008878 if (p != name)
8879 vim_free(p);
8880}
8881
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008882/*
8883 * Return TRUE for a non-zero Number and a non-empty String.
8884 */
8885 static int
8886non_zero_arg(argvars)
8887 typval_T *argvars;
8888{
8889 return ((argvars[0].v_type == VAR_NUMBER
8890 && argvars[0].vval.v_number != 0)
8891 || (argvars[0].v_type == VAR_STRING
8892 && argvars[0].vval.v_string != NULL
8893 && *argvars[0].vval.v_string != NUL));
8894}
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896/*********************************************
8897 * Implementation of the built-in functions
8898 */
8899
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008900#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008901static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8902
8903/*
8904 * Get the float value of "argvars[0]" into "f".
8905 * Returns FAIL when the argument is not a Number or Float.
8906 */
8907 static int
8908get_float_arg(argvars, f)
8909 typval_T *argvars;
8910 float_T *f;
8911{
8912 if (argvars[0].v_type == VAR_FLOAT)
8913 {
8914 *f = argvars[0].vval.v_float;
8915 return OK;
8916 }
8917 if (argvars[0].v_type == VAR_NUMBER)
8918 {
8919 *f = (float_T)argvars[0].vval.v_number;
8920 return OK;
8921 }
8922 EMSG(_("E808: Number or Float required"));
8923 return FAIL;
8924}
8925
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008926/*
8927 * "abs(expr)" function
8928 */
8929 static void
8930f_abs(argvars, rettv)
8931 typval_T *argvars;
8932 typval_T *rettv;
8933{
8934 if (argvars[0].v_type == VAR_FLOAT)
8935 {
8936 rettv->v_type = VAR_FLOAT;
8937 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8938 }
8939 else
8940 {
8941 varnumber_T n;
8942 int error = FALSE;
8943
8944 n = get_tv_number_chk(&argvars[0], &error);
8945 if (error)
8946 rettv->vval.v_number = -1;
8947 else if (n > 0)
8948 rettv->vval.v_number = n;
8949 else
8950 rettv->vval.v_number = -n;
8951 }
8952}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008953
8954/*
8955 * "acos()" function
8956 */
8957 static void
8958f_acos(argvars, rettv)
8959 typval_T *argvars;
8960 typval_T *rettv;
8961{
8962 float_T f;
8963
8964 rettv->v_type = VAR_FLOAT;
8965 if (get_float_arg(argvars, &f) == OK)
8966 rettv->vval.v_float = acos(f);
8967 else
8968 rettv->vval.v_float = 0.0;
8969}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008970#endif
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008973 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 */
8975 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008976f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 typval_T *argvars;
8978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008985 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008986 && !tv_check_lock(l->lv_lock,
8987 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008988 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 }
8991 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 EMSG(_(e_listreq));
8993}
8994
8995/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008996 * "alloc_fail(id, countdown, repeat)" function
8997 */
8998 static void
8999f_alloc_fail(argvars, rettv)
9000 typval_T *argvars;
9001 typval_T *rettv UNUSED;
9002{
9003 if (argvars[0].v_type != VAR_NUMBER
9004 || argvars[0].vval.v_number <= 0
9005 || argvars[1].v_type != VAR_NUMBER
9006 || argvars[1].vval.v_number < 0
9007 || argvars[2].v_type != VAR_NUMBER)
9008 EMSG(_(e_invarg));
9009 else
9010 {
9011 alloc_fail_id = argvars[0].vval.v_number;
9012 alloc_fail_countdown = argvars[1].vval.v_number;
9013 alloc_fail_repeat = argvars[2].vval.v_number;
9014 }
9015}
9016
9017/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009018 * "and(expr, expr)" function
9019 */
9020 static void
9021f_and(argvars, rettv)
9022 typval_T *argvars;
9023 typval_T *rettv;
9024{
9025 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9026 & get_tv_number_chk(&argvars[1], NULL);
9027}
9028
9029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030 * "append(lnum, string/list)" function
9031 */
9032 static void
9033f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009034 typval_T *argvars;
9035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036{
9037 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009038 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 list_T *l = NULL;
9040 listitem_T *li = NULL;
9041 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 long added = 0;
9043
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009044 /* When coming here from Insert mode, sync undo, so that this can be
9045 * undone separately from what was previously inserted. */
9046 if (u_sync_once == 2)
9047 {
9048 u_sync_once = 1; /* notify that u_sync() was called */
9049 u_sync(TRUE);
9050 }
9051
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052 lnum = get_tv_lnum(argvars);
9053 if (lnum >= 0
9054 && lnum <= curbuf->b_ml.ml_line_count
9055 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009058 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 l = argvars[1].vval.v_list;
9060 if (l == NULL)
9061 return;
9062 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009063 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009064 for (;;)
9065 {
9066 if (l == NULL)
9067 tv = &argvars[1]; /* append a string */
9068 else if (li == NULL)
9069 break; /* end of list */
9070 else
9071 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 line = get_tv_string_chk(tv);
9073 if (line == NULL) /* type error */
9074 {
9075 rettv->vval.v_number = 1; /* Failed */
9076 break;
9077 }
9078 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009079 ++added;
9080 if (l == NULL)
9081 break;
9082 li = li->li_next;
9083 }
9084
9085 appended_lines_mark(lnum, added);
9086 if (curwin->w_cursor.lnum > lnum)
9087 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 else
9090 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093/*
9094 * "argc()" function
9095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102}
9103
9104/*
9105 * "argidx()" function
9106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009116 * "arglistid()" function
9117 */
9118 static void
9119f_arglistid(argvars, rettv)
9120 typval_T *argvars UNUSED;
9121 typval_T *rettv;
9122{
9123 win_T *wp;
9124 tabpage_T *tp = NULL;
9125 long n;
9126
9127 rettv->vval.v_number = -1;
9128 if (argvars[0].v_type != VAR_UNKNOWN)
9129 {
9130 if (argvars[1].v_type != VAR_UNKNOWN)
9131 {
9132 n = get_tv_number(&argvars[1]);
9133 if (n >= 0)
9134 tp = find_tabpage(n);
9135 }
9136 else
9137 tp = curtab;
9138
9139 if (tp != NULL)
9140 {
9141 wp = find_win_by_nr(&argvars[0], tp);
9142 if (wp != NULL)
9143 rettv->vval.v_number = wp->w_alist->id;
9144 }
9145 }
9146 else
9147 rettv->vval.v_number = curwin->w_alist->id;
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * "argv(nr)" function
9152 */
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 int idx;
9159
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009160 if (argvars[0].v_type != VAR_UNKNOWN)
9161 {
9162 idx = get_tv_number_chk(&argvars[0], NULL);
9163 if (idx >= 0 && idx < ARGCOUNT)
9164 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9165 else
9166 rettv->vval.v_string = NULL;
9167 rettv->v_type = VAR_STRING;
9168 }
9169 else if (rettv_list_alloc(rettv) == OK)
9170 for (idx = 0; idx < ARGCOUNT; ++idx)
9171 list_append_string(rettv->vval.v_list,
9172 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009175static void prepare_assert_error __ARGS((garray_T*gap));
9176static void fill_assert_error __ARGS((garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv));
9177static void assert_error __ARGS((garray_T *gap));
9178static void assert_bool __ARGS((typval_T *argvars, int isTrue));
9179
9180/*
9181 * Prepare "gap" for an assert error and add the sourcing position.
9182 */
9183 static void
9184prepare_assert_error(gap)
9185 garray_T *gap;
9186{
9187 char buf[NUMBUFLEN];
9188
9189 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009190 if (sourcing_name != NULL)
9191 {
9192 ga_concat(gap, sourcing_name);
9193 if (sourcing_lnum > 0)
9194 ga_concat(gap, (char_u *)" ");
9195 }
9196 if (sourcing_lnum > 0)
9197 {
9198 sprintf(buf, "line %ld", (long)sourcing_lnum);
9199 ga_concat(gap, (char_u *)buf);
9200 }
9201 if (sourcing_name != NULL || sourcing_lnum > 0)
9202 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009203}
9204
9205/*
9206 * Fill "gap" with information about an assert error.
9207 */
9208 static void
9209fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9210 garray_T *gap;
9211 typval_T *opt_msg_tv;
9212 char_u *exp_str;
9213 typval_T *exp_tv;
9214 typval_T *got_tv;
9215{
9216 char_u numbuf[NUMBUFLEN];
9217 char_u *tofree;
9218
9219 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9220 {
9221 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9222 vim_free(tofree);
9223 }
9224 else
9225 {
9226 ga_concat(gap, (char_u *)"Expected ");
9227 if (exp_str == NULL)
9228 {
9229 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9230 vim_free(tofree);
9231 }
9232 else
9233 ga_concat(gap, exp_str);
9234 ga_concat(gap, (char_u *)" but got ");
9235 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9236 vim_free(tofree);
9237 }
9238}
Bram Moolenaar43345542015-11-29 17:35:35 +01009239
9240/*
9241 * Add an assert error to v:errors.
9242 */
9243 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009245 garray_T *gap;
9246{
9247 struct vimvar *vp = &vimvars[VV_ERRORS];
9248
9249 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9250 /* Make sure v:errors is a list. */
9251 set_vim_var_list(VV_ERRORS, list_alloc());
9252 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9253}
9254
Bram Moolenaar43345542015-11-29 17:35:35 +01009255/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009257 */
9258 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009260 typval_T *argvars;
9261 typval_T *rettv UNUSED;
9262{
9263 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009264
9265 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9266 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009267 prepare_assert_error(&ga);
9268 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9269 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009270 ga_clear(&ga);
9271 }
9272}
9273
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009274/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009275 * "assert_exception(string[, msg])" function
9276 */
9277 static void
9278f_assert_exception(argvars, rettv)
9279 typval_T *argvars;
9280 typval_T *rettv UNUSED;
9281{
9282 garray_T ga;
9283 char *error;
9284
9285 error = (char *)get_tv_string_chk(&argvars[0]);
9286 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9287 {
9288 prepare_assert_error(&ga);
9289 ga_concat(&ga, (char_u *)"v:exception is not set");
9290 assert_error(&ga);
9291 ga_clear(&ga);
9292 }
9293 else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9294 {
9295 prepare_assert_error(&ga);
9296 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9297 &vimvars[VV_EXCEPTION].vv_tv);
9298 assert_error(&ga);
9299 ga_clear(&ga);
9300 }
9301}
9302
9303/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304 * Common for assert_true() and assert_false().
9305 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009306 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009307assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009308 typval_T *argvars;
9309 int isTrue;
9310{
9311 int error = FALSE;
9312 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009313
9314 if (argvars[0].v_type != VAR_NUMBER
9315 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9316 || error)
9317 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009318 prepare_assert_error(&ga);
9319 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009320 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321 NULL, &argvars[0]);
9322 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009323 ga_clear(&ga);
9324 }
9325}
9326
9327/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009328 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009329 */
9330 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009332 typval_T *argvars;
9333 typval_T *rettv UNUSED;
9334{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009335 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009336}
9337
9338/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009339 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009340 */
9341 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009342f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009343 typval_T *argvars;
9344 typval_T *rettv UNUSED;
9345{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009347}
9348
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009349#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009350/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009351 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009352 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353 static void
9354f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009355 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009356 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009357{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009358 float_T f;
9359
9360 rettv->v_type = VAR_FLOAT;
9361 if (get_float_arg(argvars, &f) == OK)
9362 rettv->vval.v_float = asin(f);
9363 else
9364 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365}
9366
9367/*
9368 * "atan()" function
9369 */
9370 static void
9371f_atan(argvars, rettv)
9372 typval_T *argvars;
9373 typval_T *rettv;
9374{
9375 float_T f;
9376
9377 rettv->v_type = VAR_FLOAT;
9378 if (get_float_arg(argvars, &f) == OK)
9379 rettv->vval.v_float = atan(f);
9380 else
9381 rettv->vval.v_float = 0.0;
9382}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009383
9384/*
9385 * "atan2()" function
9386 */
9387 static void
9388f_atan2(argvars, rettv)
9389 typval_T *argvars;
9390 typval_T *rettv;
9391{
9392 float_T fx, fy;
9393
9394 rettv->v_type = VAR_FLOAT;
9395 if (get_float_arg(argvars, &fx) == OK
9396 && get_float_arg(&argvars[1], &fy) == OK)
9397 rettv->vval.v_float = atan2(fx, fy);
9398 else
9399 rettv->vval.v_float = 0.0;
9400}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009401#endif
9402
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403/*
9404 * "browse(save, title, initdir, default)" function
9405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifdef FEAT_BROWSE
9412 int save;
9413 char_u *title;
9414 char_u *initdir;
9415 char_u *defname;
9416 char_u buf[NUMBUFLEN];
9417 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009418 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 save = get_tv_number_chk(&argvars[0], &error);
9421 title = get_tv_string_chk(&argvars[1]);
9422 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9423 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 if (error || title == NULL || initdir == NULL || defname == NULL)
9426 rettv->vval.v_string = NULL;
9427 else
9428 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009429 do_browse(save ? BROWSE_SAVE : 0,
9430 title, defname, NULL, initdir, NULL, curbuf);
9431#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009433#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009434 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009435}
9436
9437/*
9438 * "browsedir(title, initdir)" function
9439 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009441f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009442 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009444{
9445#ifdef FEAT_BROWSE
9446 char_u *title;
9447 char_u *initdir;
9448 char_u buf[NUMBUFLEN];
9449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009450 title = get_tv_string_chk(&argvars[0]);
9451 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 if (title == NULL || initdir == NULL)
9454 rettv->vval.v_string = NULL;
9455 else
9456 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009457 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
Bram Moolenaar33570922005-01-25 22:26:29 +00009464static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466/*
9467 * Find a buffer by number or exact name.
9468 */
9469 static buf_T *
9470find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009471 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009475 if (avar->v_type == VAR_NUMBER)
9476 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009477 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009479 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009480 if (buf == NULL)
9481 {
9482 /* No full path name match, try a match with a URL or a "nofile"
9483 * buffer, these don't use the full path. */
9484 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9485 if (buf->b_fname != NULL
9486 && (path_with_url(buf->b_fname)
9487#ifdef FEAT_QUICKFIX
9488 || bt_nofile(buf)
9489#endif
9490 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009491 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009492 break;
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 }
9495 return buf;
9496}
9497
9498/*
9499 * "bufexists(expr)" function
9500 */
9501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009503 typval_T *argvars;
9504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * "buflisted(expr)" function
9511 */
9512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 buf_T *buf;
9518
9519 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
9524 * "bufloaded(expr)" function
9525 */
9526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 buf_T *buf;
9532
9533 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009537static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539/*
9540 * Get buffer by number or pattern.
9541 */
9542 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009543get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009545 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 int save_magic;
9549 char_u *save_cpo;
9550 buf_T *buf;
9551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 if (tv->v_type == VAR_NUMBER)
9553 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009554 if (tv->v_type != VAR_STRING)
9555 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (name == NULL || *name == NUL)
9557 return curbuf;
9558 if (name[0] == '$' && name[1] == NUL)
9559 return lastbuf;
9560
9561 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9562 save_magic = p_magic;
9563 p_magic = TRUE;
9564 save_cpo = p_cpo;
9565 p_cpo = (char_u *)"";
9566
9567 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009568 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
9570 p_magic = save_magic;
9571 p_cpo = save_cpo;
9572
9573 /* If not found, try expanding the name, like done for bufexists(). */
9574 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
9577 return buf;
9578}
9579
9580/*
9581 * "bufname(expr)" function
9582 */
9583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009585 typval_T *argvars;
9586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 buf_T *buf;
9589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009592 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 --emsg_off;
9599}
9600
9601/*
9602 * "bufnr(expr)" function
9603 */
9604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009606 typval_T *argvars;
9607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009610 int error = FALSE;
9611 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009615 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009616 --emsg_off;
9617
9618 /* If the buffer isn't found and the second argument is not zero create a
9619 * new buffer. */
9620 if (buf == NULL
9621 && argvars[1].v_type != VAR_UNKNOWN
9622 && get_tv_number_chk(&argvars[1], &error) != 0
9623 && !error
9624 && (name = get_tv_string_chk(&argvars[0])) != NULL
9625 && !error)
9626 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
9635 * "bufwinnr(nr)" function
9636 */
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642#ifdef FEAT_WINDOWS
9643 win_T *wp;
9644 int winnr = 0;
9645#endif
9646 buf_T *buf;
9647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009650 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_WINDOWS
9652 for (wp = firstwin; wp; wp = wp->w_next)
9653 {
9654 ++winnr;
9655 if (wp->w_buffer == buf)
9656 break;
9657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661#endif
9662 --emsg_off;
9663}
9664
9665/*
9666 * "byte2line(byte)" function
9667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675#else
9676 long boff = 0;
9677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009678 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009680 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 (linenr_T)0, &boff);
9684#endif
9685}
9686
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009687 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009688byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 typval_T *argvars;
9690 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009691 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009692{
9693#ifdef FEAT_MBYTE
9694 char_u *t;
9695#endif
9696 char_u *str;
9697 long idx;
9698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 str = get_tv_string_chk(&argvars[0]);
9700 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009703 return;
9704
9705#ifdef FEAT_MBYTE
9706 t = str;
9707 for ( ; idx > 0; idx--)
9708 {
9709 if (*t == NUL) /* EOL reached */
9710 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009711 if (enc_utf8 && comp)
9712 t += utf_ptr2len(t);
9713 else
9714 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009716 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009717#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009718 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009720#endif
9721}
9722
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009723/*
9724 * "byteidx()" function
9725 */
9726 static void
9727f_byteidx(argvars, rettv)
9728 typval_T *argvars;
9729 typval_T *rettv;
9730{
9731 byteidx(argvars, rettv, FALSE);
9732}
9733
9734/*
9735 * "byteidxcomp()" function
9736 */
9737 static void
9738f_byteidxcomp(argvars, rettv)
9739 typval_T *argvars;
9740 typval_T *rettv;
9741{
9742 byteidx(argvars, rettv, TRUE);
9743}
9744
Bram Moolenaardb913952012-06-29 12:54:53 +02009745 int
9746func_call(name, args, selfdict, rettv)
9747 char_u *name;
9748 typval_T *args;
9749 dict_T *selfdict;
9750 typval_T *rettv;
9751{
9752 listitem_T *item;
9753 typval_T argv[MAX_FUNC_ARGS + 1];
9754 int argc = 0;
9755 int dummy;
9756 int r = 0;
9757
9758 for (item = args->vval.v_list->lv_first; item != NULL;
9759 item = item->li_next)
9760 {
9761 if (argc == MAX_FUNC_ARGS)
9762 {
9763 EMSG(_("E699: Too many arguments"));
9764 break;
9765 }
9766 /* Make a copy of each argument. This is needed to be able to set
9767 * v_lock to VAR_FIXED in the copy without changing the original list.
9768 */
9769 copy_tv(&item->li_tv, &argv[argc++]);
9770 }
9771
9772 if (item == NULL)
9773 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9774 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9775 &dummy, TRUE, selfdict);
9776
9777 /* Free the arguments. */
9778 while (argc > 0)
9779 clear_tv(&argv[--argc]);
9780
9781 return r;
9782}
9783
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009784/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009785 * "call(func, arglist)" function
9786 */
9787 static void
9788f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009791{
9792 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009794
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009795 if (argvars[1].v_type != VAR_LIST)
9796 {
9797 EMSG(_(e_listreq));
9798 return;
9799 }
9800 if (argvars[1].vval.v_list == NULL)
9801 return;
9802
9803 if (argvars[0].v_type == VAR_FUNC)
9804 func = argvars[0].vval.v_string;
9805 else
9806 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009807 if (*func == NUL)
9808 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009809
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810 if (argvars[2].v_type != VAR_UNKNOWN)
9811 {
9812 if (argvars[2].v_type != VAR_DICT)
9813 {
9814 EMSG(_(e_dictreq));
9815 return;
9816 }
9817 selfdict = argvars[2].vval.v_dict;
9818 }
9819
Bram Moolenaardb913952012-06-29 12:54:53 +02009820 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009821}
9822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009823#ifdef FEAT_FLOAT
9824/*
9825 * "ceil({float})" function
9826 */
9827 static void
9828f_ceil(argvars, rettv)
9829 typval_T *argvars;
9830 typval_T *rettv;
9831{
9832 float_T f;
9833
9834 rettv->v_type = VAR_FLOAT;
9835 if (get_float_arg(argvars, &f) == OK)
9836 rettv->vval.v_float = ceil(f);
9837 else
9838 rettv->vval.v_float = 0.0;
9839}
9840#endif
9841
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009842/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009843 * "changenr()" function
9844 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009845 static void
9846f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009847 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009848 typval_T *rettv;
9849{
9850 rettv->vval.v_number = curbuf->b_u_seq_cur;
9851}
9852
9853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 * "char2nr(string)" function
9855 */
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861#ifdef FEAT_MBYTE
9862 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009863 {
9864 int utf8 = 0;
9865
9866 if (argvars[1].v_type != VAR_UNKNOWN)
9867 utf8 = get_tv_number_chk(&argvars[1], NULL);
9868
9869 if (utf8)
9870 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9871 else
9872 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 else
9875#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877}
9878
9879/*
9880 * "cindent(lnum)" function
9881 */
9882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009883f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009884 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887#ifdef FEAT_CINDENT
9888 pos_T pos;
9889 linenr_T lnum;
9890
9891 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9894 {
9895 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 curwin->w_cursor = pos;
9898 }
9899 else
9900#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009905 * "clearmatches()" function
9906 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009907 static void
9908f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009909 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009910 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009911{
9912#ifdef FEAT_SEARCH_EXTRA
9913 clear_matches(curwin);
9914#endif
9915}
9916
9917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 * "col(string)" function
9919 */
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 colnr_T col = 0;
9926 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009927 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009929 fp = var2fpos(&argvars[0], FALSE, &fnum);
9930 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 {
9932 if (fp->col == MAXCOL)
9933 {
9934 /* '> can be MAXCOL, get the length of the line then */
9935 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009936 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 col = MAXCOL;
9939 }
9940 else
9941 {
9942 col = fp->col + 1;
9943#ifdef FEAT_VIRTUALEDIT
9944 /* col(".") when the cursor is on the NUL at the end of the line
9945 * because of "coladd" can be seen as an extra column. */
9946 if (virtual_active() && fp == &curwin->w_cursor)
9947 {
9948 char_u *p = ml_get_cursor();
9949
9950 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9951 curwin->w_virtcol - curwin->w_cursor.coladd))
9952 {
9953# ifdef FEAT_MBYTE
9954 int l;
9955
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009956 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 col += l;
9958# else
9959 if (*p != NUL && p[1] == NUL)
9960 ++col;
9961# endif
9962 }
9963 }
9964#endif
9965 }
9966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
Bram Moolenaar572cb562005-08-05 21:35:02 +00009970#if defined(FEAT_INS_EXPAND)
9971/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009972 * "complete()" function
9973 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009974 static void
9975f_complete(argvars, rettv)
9976 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009977 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009978{
9979 int startcol;
9980
9981 if ((State & INSERT) == 0)
9982 {
9983 EMSG(_("E785: complete() can only be used in Insert mode"));
9984 return;
9985 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009986
9987 /* Check for undo allowed here, because if something was already inserted
9988 * the line was already saved for undo and this check isn't done. */
9989 if (!undo_allowed())
9990 return;
9991
Bram Moolenaarade00832006-03-10 21:46:58 +00009992 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9993 {
9994 EMSG(_(e_invarg));
9995 return;
9996 }
9997
9998 startcol = get_tv_number_chk(&argvars[0], NULL);
9999 if (startcol <= 0)
10000 return;
10001
10002 set_completion(startcol - 1, argvars[1].vval.v_list);
10003}
10004
10005/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010006 * "complete_add()" function
10007 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010008 static void
10009f_complete_add(argvars, rettv)
10010 typval_T *argvars;
10011 typval_T *rettv;
10012{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010013 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010014}
10015
10016/*
10017 * "complete_check()" function
10018 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010019 static void
10020f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010021 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010022 typval_T *rettv;
10023{
10024 int saved = RedrawingDisabled;
10025
10026 RedrawingDisabled = 0;
10027 ins_compl_check_keys(0);
10028 rettv->vval.v_number = compl_interrupted;
10029 RedrawingDisabled = saved;
10030}
10031#endif
10032
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033/*
10034 * "confirm(message, buttons[, default [, type]])" function
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010038 typval_T *argvars UNUSED;
10039 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10042 char_u *message;
10043 char_u *buttons = NULL;
10044 char_u buf[NUMBUFLEN];
10045 char_u buf2[NUMBUFLEN];
10046 int def = 1;
10047 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048 char_u *typestr;
10049 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 message = get_tv_string_chk(&argvars[0]);
10052 if (message == NULL)
10053 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10057 if (buttons == NULL)
10058 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010059 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010061 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010062 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010064 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10065 if (typestr == NULL)
10066 error = TRUE;
10067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 switch (TOUPPER_ASC(*typestr))
10070 {
10071 case 'E': type = VIM_ERROR; break;
10072 case 'Q': type = VIM_QUESTION; break;
10073 case 'I': type = VIM_INFO; break;
10074 case 'W': type = VIM_WARNING; break;
10075 case 'G': type = VIM_GENERIC; break;
10076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 }
10079 }
10080 }
10081
10082 if (buttons == NULL || *buttons == NUL)
10083 buttons = (char_u *)_("&Ok");
10084
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010085 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010087 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088#endif
10089}
10090
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010091/*
10092 * "copy()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010099 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010100}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010102#ifdef FEAT_FLOAT
10103/*
10104 * "cos()" function
10105 */
10106 static void
10107f_cos(argvars, rettv)
10108 typval_T *argvars;
10109 typval_T *rettv;
10110{
10111 float_T f;
10112
10113 rettv->v_type = VAR_FLOAT;
10114 if (get_float_arg(argvars, &f) == OK)
10115 rettv->vval.v_float = cos(f);
10116 else
10117 rettv->vval.v_float = 0.0;
10118}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010119
10120/*
10121 * "cosh()" function
10122 */
10123 static void
10124f_cosh(argvars, rettv)
10125 typval_T *argvars;
10126 typval_T *rettv;
10127{
10128 float_T f;
10129
10130 rettv->v_type = VAR_FLOAT;
10131 if (get_float_arg(argvars, &f) == OK)
10132 rettv->vval.v_float = cosh(f);
10133 else
10134 rettv->vval.v_float = 0.0;
10135}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010136#endif
10137
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010139 * "count()" function
10140 */
10141 static void
10142f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010143 typval_T *argvars;
10144 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010145{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010146 long n = 0;
10147 int ic = FALSE;
10148
Bram Moolenaare9a41262005-01-15 22:18:47 +000010149 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 listitem_T *li;
10152 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010154
Bram Moolenaare9a41262005-01-15 22:18:47 +000010155 if ((l = argvars[0].vval.v_list) != NULL)
10156 {
10157 li = l->lv_first;
10158 if (argvars[2].v_type != VAR_UNKNOWN)
10159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 int error = FALSE;
10161
10162 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163 if (argvars[3].v_type != VAR_UNKNOWN)
10164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 idx = get_tv_number_chk(&argvars[3], &error);
10166 if (!error)
10167 {
10168 li = list_find(l, idx);
10169 if (li == NULL)
10170 EMSGN(_(e_listidx), idx);
10171 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 if (error)
10174 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 }
10176
10177 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010178 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 ++n;
10180 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 else if (argvars[0].v_type == VAR_DICT)
10183 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010184 int todo;
10185 dict_T *d;
10186 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010188 if ((d = argvars[0].vval.v_dict) != NULL)
10189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 int error = FALSE;
10191
Bram Moolenaare9a41262005-01-15 22:18:47 +000010192 if (argvars[2].v_type != VAR_UNKNOWN)
10193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 if (argvars[3].v_type != VAR_UNKNOWN)
10196 EMSG(_(e_invarg));
10197 }
10198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010199 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010200 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010201 {
10202 if (!HASHITEM_EMPTY(hi))
10203 {
10204 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010205 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010206 ++n;
10207 }
10208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 }
10210 }
10211 else
10212 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010213 rettv->vval.v_number = n;
10214}
10215
10216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10218 *
10219 * Checks the existence of a cscope connection.
10220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010222f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010223 typval_T *argvars UNUSED;
10224 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225{
10226#ifdef FEAT_CSCOPE
10227 int num = 0;
10228 char_u *dbpath = NULL;
10229 char_u *prepend = NULL;
10230 char_u buf[NUMBUFLEN];
10231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010232 if (argvars[0].v_type != VAR_UNKNOWN
10233 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 num = (int)get_tv_number(&argvars[0]);
10236 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010237 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243}
10244
10245/*
10246 * "cursor(lnum, col)" function
10247 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010248 * Moves the cursor to the specified line and column.
10249 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010252f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010253 typval_T *argvars;
10254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255{
10256 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010257#ifdef FEAT_VIRTUALEDIT
10258 long coladd = 0;
10259#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010260 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010262 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010263 if (argvars[1].v_type == VAR_UNKNOWN)
10264 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010265 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010266 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010267
Bram Moolenaar493c1782014-05-28 14:34:46 +020010268 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +000010269 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010270 line = pos.lnum;
10271 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010272#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010273 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010274#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010275 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010276 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010277 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010278 set_curswant = FALSE;
10279 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010280 }
10281 else
10282 {
10283 line = get_tv_lnum(argvars);
10284 col = get_tv_number_chk(&argvars[1], NULL);
10285#ifdef FEAT_VIRTUALEDIT
10286 if (argvars[2].v_type != VAR_UNKNOWN)
10287 coladd = get_tv_number_chk(&argvars[2], NULL);
10288#endif
10289 }
10290 if (line < 0 || col < 0
10291#ifdef FEAT_VIRTUALEDIT
10292 || coladd < 0
10293#endif
10294 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 if (line > 0)
10297 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 if (col > 0)
10299 curwin->w_cursor.col = col - 1;
10300#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010301 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302#endif
10303
10304 /* Make sure the cursor is in a valid position. */
10305 check_cursor();
10306#ifdef FEAT_MBYTE
10307 /* Correct cursor for multi-byte character. */
10308 if (has_mbyte)
10309 mb_adjust_cursor();
10310#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010311
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010312 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010313 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314}
10315
10316/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010317 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 */
10319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010324 int noref = 0;
10325
10326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010328 if (noref < 0 || noref > 1)
10329 EMSG(_(e_invarg));
10330 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010331 {
10332 current_copyID += COPYID_INC;
10333 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335}
10336
10337/*
10338 * "delete()" function
10339 */
10340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010342 typval_T *argvars;
10343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344{
10345 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349}
10350
10351/*
10352 * "did_filetype()" function
10353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010356 typval_T *argvars UNUSED;
10357 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361#endif
10362}
10363
10364/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010365 * "diff_filler()" function
10366 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010369 typval_T *argvars UNUSED;
10370 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010371{
10372#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010374#endif
10375}
10376
10377/*
10378 * "diff_hlID()" function
10379 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010381f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010382 typval_T *argvars UNUSED;
10383 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010384{
10385#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010387 static linenr_T prev_lnum = 0;
10388 static int changedtick = 0;
10389 static int fnum = 0;
10390 static int change_start = 0;
10391 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010392 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010393 int filler_lines;
10394 int col;
10395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010396 if (lnum < 0) /* ignore type error in {lnum} arg */
10397 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010398 if (lnum != prev_lnum
10399 || changedtick != curbuf->b_changedtick
10400 || fnum != curbuf->b_fnum)
10401 {
10402 /* New line, buffer, change: need to get the values. */
10403 filler_lines = diff_check(curwin, lnum);
10404 if (filler_lines < 0)
10405 {
10406 if (filler_lines == -1)
10407 {
10408 change_start = MAXCOL;
10409 change_end = -1;
10410 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10411 hlID = HLF_ADD; /* added line */
10412 else
10413 hlID = HLF_CHD; /* changed line */
10414 }
10415 else
10416 hlID = HLF_ADD; /* added line */
10417 }
10418 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010419 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010420 prev_lnum = lnum;
10421 changedtick = curbuf->b_changedtick;
10422 fnum = curbuf->b_fnum;
10423 }
10424
10425 if (hlID == HLF_CHD || hlID == HLF_TXD)
10426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010428 if (col >= change_start && col <= change_end)
10429 hlID = HLF_TXD; /* changed text */
10430 else
10431 hlID = HLF_CHD; /* changed line */
10432 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010433 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010434#endif
10435}
10436
10437/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010438 * "empty({expr})" function
10439 */
10440 static void
10441f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 typval_T *argvars;
10443 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010444{
10445 int n;
10446
10447 switch (argvars[0].v_type)
10448 {
10449 case VAR_STRING:
10450 case VAR_FUNC:
10451 n = argvars[0].vval.v_string == NULL
10452 || *argvars[0].vval.v_string == NUL;
10453 break;
10454 case VAR_NUMBER:
10455 n = argvars[0].vval.v_number == 0;
10456 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457#ifdef FEAT_FLOAT
10458 case VAR_FLOAT:
10459 n = argvars[0].vval.v_float == 0.0;
10460 break;
10461#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010462 case VAR_LIST:
10463 n = argvars[0].vval.v_list == NULL
10464 || argvars[0].vval.v_list->lv_first == NULL;
10465 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010466 case VAR_DICT:
10467 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010469 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010470 default:
10471 EMSG2(_(e_intern2), "f_empty()");
10472 n = 0;
10473 }
10474
10475 rettv->vval.v_number = n;
10476}
10477
10478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 * "escape({string}, {chars})" function
10480 */
10481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010482f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010483 typval_T *argvars;
10484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485{
10486 char_u buf[NUMBUFLEN];
10487
Bram Moolenaar758711c2005-02-02 23:11:38 +000010488 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10489 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491}
10492
10493/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010494 * "eval()" function
10495 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010496 static void
10497f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010500{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010501 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 s = get_tv_string_chk(&argvars[0]);
10504 if (s != NULL)
10505 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010506
Bram Moolenaar615b9972015-01-14 17:15:05 +010010507 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10509 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010510 if (p != NULL && !aborting())
10511 EMSG2(_(e_invexpr2), p);
10512 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010514 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010516 else if (*s != NUL)
10517 EMSG(_(e_trailing));
10518}
10519
10520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 * "eventhandler()" function
10522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010525 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
10531/*
10532 * "executable()" function
10533 */
10534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *argvars;
10537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010539 char_u *name = get_tv_string(&argvars[0]);
10540
10541 /* Check in $PATH and also check directly if there is a directory name. */
10542 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10543 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010544}
10545
10546/*
10547 * "exepath()" function
10548 */
10549 static void
10550f_exepath(argvars, rettv)
10551 typval_T *argvars;
10552 typval_T *rettv;
10553{
10554 char_u *p = NULL;
10555
Bram Moolenaarb5971142015-03-21 17:32:19 +010010556 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010557 rettv->v_type = VAR_STRING;
10558 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
10562 * "exists()" function
10563 */
10564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 char_u *p;
10570 char_u *name;
10571 int n = FALSE;
10572 int len = 0;
10573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 if (*p == '$') /* environment variable */
10576 {
10577 /* first try "normal" environment variables (fast) */
10578 if (mch_getenv(p + 1) != NULL)
10579 n = TRUE;
10580 else
10581 {
10582 /* try expanding things like $VIM and ${HOME} */
10583 p = expand_env_save(p);
10584 if (p != NULL && *p != '$')
10585 n = TRUE;
10586 vim_free(p);
10587 }
10588 }
10589 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010592 if (*skipwhite(p) != NUL)
10593 n = FALSE; /* trailing garbage */
10594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 else if (*p == '*') /* internal or user defined function */
10596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010597 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 }
10599 else if (*p == ':')
10600 {
10601 n = cmd_exists(p + 1);
10602 }
10603 else if (*p == '#')
10604 {
10605#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010606 if (p[1] == '#')
10607 n = autocmd_supported(p + 2);
10608 else
10609 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610#endif
10611 }
10612 else /* internal variable */
10613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010614 char_u *tofree;
10615 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010617 /* get_name_len() takes care of expanding curly braces */
10618 name = p;
10619 len = get_name_len(&p, &tofree, TRUE, FALSE);
10620 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010622 if (tofree != NULL)
10623 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010624 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010625 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010627 /* handle d.key, l[idx], f(expr) */
10628 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10629 if (n)
10630 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 }
10632 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010633 if (*p != NUL)
10634 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010636 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 }
10638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640}
10641
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010642#ifdef FEAT_FLOAT
10643/*
10644 * "exp()" function
10645 */
10646 static void
10647f_exp(argvars, rettv)
10648 typval_T *argvars;
10649 typval_T *rettv;
10650{
10651 float_T f;
10652
10653 rettv->v_type = VAR_FLOAT;
10654 if (get_float_arg(argvars, &f) == OK)
10655 rettv->vval.v_float = exp(f);
10656 else
10657 rettv->vval.v_float = 0.0;
10658}
10659#endif
10660
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661/*
10662 * "expand()" function
10663 */
10664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010666 typval_T *argvars;
10667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
10669 char_u *s;
10670 int len;
10671 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010672 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010675 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010678 if (argvars[1].v_type != VAR_UNKNOWN
10679 && argvars[2].v_type != VAR_UNKNOWN
10680 && get_tv_number_chk(&argvars[2], &error)
10681 && !error)
10682 {
10683 rettv->v_type = VAR_LIST;
10684 rettv->vval.v_list = NULL;
10685 }
10686
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 if (*s == '%' || *s == '#' || *s == '<')
10689 {
10690 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010691 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010693 if (rettv->v_type == VAR_LIST)
10694 {
10695 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10696 list_append_string(rettv->vval.v_list, result, -1);
10697 else
10698 vim_free(result);
10699 }
10700 else
10701 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 }
10703 else
10704 {
10705 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010706 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 if (argvars[1].v_type != VAR_UNKNOWN
10708 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010709 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 if (!error)
10711 {
10712 ExpandInit(&xpc);
10713 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010714 if (p_wic)
10715 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010716 if (rettv->v_type == VAR_STRING)
10717 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10718 options, WILD_ALL);
10719 else if (rettv_list_alloc(rettv) != FAIL)
10720 {
10721 int i;
10722
10723 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10724 for (i = 0; i < xpc.xp_numfiles; i++)
10725 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10726 ExpandCleanup(&xpc);
10727 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 }
10729 else
10730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 }
10732}
10733
10734/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010735 * Go over all entries in "d2" and add them to "d1".
10736 * When "action" is "error" then a duplicate key is an error.
10737 * When "action" is "force" then a duplicate key is overwritten.
10738 * Otherwise duplicate keys are ignored ("action" is "keep").
10739 */
10740 void
10741dict_extend(d1, d2, action)
10742 dict_T *d1;
10743 dict_T *d2;
10744 char_u *action;
10745{
10746 dictitem_T *di1;
10747 hashitem_T *hi2;
10748 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010749 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010750
10751 todo = (int)d2->dv_hashtab.ht_used;
10752 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10753 {
10754 if (!HASHITEM_EMPTY(hi2))
10755 {
10756 --todo;
10757 di1 = dict_find(d1, hi2->hi_key, -1);
10758 if (d1->dv_scope != 0)
10759 {
10760 /* Disallow replacing a builtin function in l: and g:.
10761 * Check the key to be valid when adding to any
10762 * scope. */
10763 if (d1->dv_scope == VAR_DEF_SCOPE
10764 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10765 && var_check_func_name(hi2->hi_key,
10766 di1 == NULL))
10767 break;
10768 if (!valid_varname(hi2->hi_key))
10769 break;
10770 }
10771 if (di1 == NULL)
10772 {
10773 di1 = dictitem_copy(HI2DI(hi2));
10774 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10775 dictitem_free(di1);
10776 }
10777 else if (*action == 'e')
10778 {
10779 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10780 break;
10781 }
10782 else if (*action == 'f' && HI2DI(hi2) != di1)
10783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010784 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10785 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010786 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010787 clear_tv(&di1->di_tv);
10788 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10789 }
10790 }
10791 }
10792}
10793
10794/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010795 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010797 */
10798 static void
10799f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010800 typval_T *argvars;
10801 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010802{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010803 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010804
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010806 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 list_T *l1, *l2;
10808 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010811
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 l1 = argvars[0].vval.v_list;
10813 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010814 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010815 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 {
10817 if (argvars[2].v_type != VAR_UNKNOWN)
10818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010819 before = get_tv_number_chk(&argvars[2], &error);
10820 if (error)
10821 return; /* type error; errmsg already given */
10822
Bram Moolenaar758711c2005-02-02 23:11:38 +000010823 if (before == l1->lv_len)
10824 item = NULL;
10825 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010827 item = list_find(l1, before);
10828 if (item == NULL)
10829 {
10830 EMSGN(_(e_listidx), before);
10831 return;
10832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010833 }
10834 }
10835 else
10836 item = NULL;
10837 list_extend(l1, l2, item);
10838
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 copy_tv(&argvars[0], rettv);
10840 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10843 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010844 dict_T *d1, *d2;
10845 char_u *action;
10846 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010847
10848 d1 = argvars[0].vval.v_dict;
10849 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010850 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010851 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 {
10853 /* Check the third argument. */
10854 if (argvars[2].v_type != VAR_UNKNOWN)
10855 {
10856 static char *(av[]) = {"keep", "force", "error"};
10857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858 action = get_tv_string_chk(&argvars[2]);
10859 if (action == NULL)
10860 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 for (i = 0; i < 3; ++i)
10862 if (STRCMP(action, av[i]) == 0)
10863 break;
10864 if (i == 3)
10865 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010866 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010867 return;
10868 }
10869 }
10870 else
10871 action = (char_u *)"force";
10872
Bram Moolenaara9922d62013-05-30 13:01:18 +020010873 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874
Bram Moolenaare9a41262005-01-15 22:18:47 +000010875 copy_tv(&argvars[0], rettv);
10876 }
10877 }
10878 else
10879 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010880}
10881
10882/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010883 * "feedkeys()" function
10884 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010885 static void
10886f_feedkeys(argvars, rettv)
10887 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010888 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010889{
10890 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010891 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010892 char_u *keys, *flags;
10893 char_u nbuf[NUMBUFLEN];
10894 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010895 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010896
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010897 /* This is not allowed in the sandbox. If the commands would still be
10898 * executed in the sandbox it would be OK, but it probably happens later,
10899 * when "sandbox" is no longer set. */
10900 if (check_secure())
10901 return;
10902
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010903 keys = get_tv_string(&argvars[0]);
10904 if (*keys != NUL)
10905 {
10906 if (argvars[1].v_type != VAR_UNKNOWN)
10907 {
10908 flags = get_tv_string_buf(&argvars[1], nbuf);
10909 for ( ; *flags != NUL; ++flags)
10910 {
10911 switch (*flags)
10912 {
10913 case 'n': remap = FALSE; break;
10914 case 'm': remap = TRUE; break;
10915 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010916 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010917 }
10918 }
10919 }
10920
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010921 /* Need to escape K_SPECIAL and CSI before putting the string in the
10922 * typeahead buffer. */
10923 keys_esc = vim_strsave_escape_csi(keys);
10924 if (keys_esc != NULL)
10925 {
10926 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010927 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010928 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010929 if (vgetc_busy)
10930 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010931 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010932 }
10933}
10934
10935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 * "filereadable()" function
10937 */
10938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010940 typval_T *argvars;
10941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010943 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 char_u *p;
10945 int n;
10946
Bram Moolenaarc236c162008-07-13 17:41:49 +000010947#ifndef O_NONBLOCK
10948# define O_NONBLOCK 0
10949#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010951 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10952 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 {
10954 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010955 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 }
10957 else
10958 n = FALSE;
10959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
10963/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010964 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 * rights to write into.
10966 */
10967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010969 typval_T *argvars;
10970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010972 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973}
10974
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010975static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010976
10977 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010978findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010980 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010981 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010982{
10983#ifdef FEAT_SEARCHPATH
10984 char_u *fname;
10985 char_u *fresult = NULL;
10986 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10987 char_u *p;
10988 char_u pathbuf[NUMBUFLEN];
10989 int count = 1;
10990 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010991 int error = FALSE;
10992#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010993
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010994 rettv->vval.v_string = NULL;
10995 rettv->v_type = VAR_STRING;
10996
10997#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010999
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11003 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011004 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 else
11006 {
11007 if (*p != NUL)
11008 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011011 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011013 }
11014
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011015 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11016 error = TRUE;
11017
11018 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 do
11021 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011022 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011023 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 fresult = find_file_in_path_option(first ? fname : NULL,
11025 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011026 0, first, path,
11027 find_what,
11028 curbuf->b_ffname,
11029 find_what == FINDFILE_DIR
11030 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011032
11033 if (fresult != NULL && rettv->v_type == VAR_LIST)
11034 list_append_string(rettv->vval.v_list, fresult, -1);
11035
11036 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011038
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011039 if (rettv->v_type == VAR_STRING)
11040 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011041#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011042}
11043
Bram Moolenaar33570922005-01-25 22:26:29 +000011044static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
11045static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011046
11047/*
11048 * Implementation of map() and filter().
11049 */
11050 static void
11051filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011052 typval_T *argvars;
11053 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011054 int map;
11055{
11056 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011057 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011058 listitem_T *li, *nli;
11059 list_T *l = NULL;
11060 dictitem_T *di;
11061 hashtab_T *ht;
11062 hashitem_T *hi;
11063 dict_T *d = NULL;
11064 typval_T save_val;
11065 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011067 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011068 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011069 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011070 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011071 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011072 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011073
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011075 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011076 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011077 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 return;
11079 }
11080 else if (argvars[0].v_type == VAR_DICT)
11081 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011082 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011083 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011084 return;
11085 }
11086 else
11087 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011088 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011089 return;
11090 }
11091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 expr = get_tv_string_buf_chk(&argvars[1], buf);
11093 /* On type errors, the preceding call has already displayed an error
11094 * message. Avoid a misleading error message for an empty string that
11095 * was not passed as argument. */
11096 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011098 prepare_vimvar(VV_VAL, &save_val);
11099 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011101 /* We reset "did_emsg" to be able to detect whether an error
11102 * occurred during evaluation of the expression. */
11103 save_did_emsg = did_emsg;
11104 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011105
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011106 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 vimvars[VV_KEY].vv_type = VAR_STRING;
11110
11111 ht = &d->dv_hashtab;
11112 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011113 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 if (!HASHITEM_EMPTY(hi))
11117 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011118 int r;
11119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 --todo;
11121 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011122 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011123 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11124 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011125 break;
11126 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011127 r = filter_map_one(&di->di_tv, expr, map, &rem);
11128 clear_tv(&vimvars[VV_KEY].vv_tv);
11129 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 break;
11131 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011132 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011133 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11134 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011135 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 }
11139 }
11140 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 }
11142 else
11143 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011144 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 for (li = l->lv_first; li != NULL; li = nli)
11147 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011148 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011149 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011151 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011152 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011153 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011154 break;
11155 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011157 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011158 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011159 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011160
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011161 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011163
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011164 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011166
11167 copy_tv(&argvars[0], rettv);
11168}
11169
11170 static int
11171filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011172 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 char_u *expr;
11174 int map;
11175 int *remp;
11176{
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011179 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011180
Bram Moolenaar33570922005-01-25 22:26:29 +000011181 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011182 s = expr;
11183 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011184 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 if (*s != NUL) /* check for trailing chars after expr */
11186 {
11187 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011188 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011189 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011190 }
11191 if (map)
11192 {
11193 /* map(): replace the list item value */
11194 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011195 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011196 *tv = rettv;
11197 }
11198 else
11199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 int error = FALSE;
11201
Bram Moolenaare9a41262005-01-15 22:18:47 +000011202 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011204 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 /* On type error, nothing has been removed; return FAIL to stop the
11206 * loop. The error message was given by get_tv_number_chk(). */
11207 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011208 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011209 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011210 retval = OK;
11211theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011213 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011214}
11215
11216/*
11217 * "filter()" function
11218 */
11219 static void
11220f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223{
11224 filter_map(argvars, rettv, FALSE);
11225}
11226
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011227/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228 * "finddir({fname}[, {path}[, {count}]])" function
11229 */
11230 static void
11231f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 typval_T *argvars;
11233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011235 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236}
11237
11238/*
11239 * "findfile({fname}[, {path}[, {count}]])" function
11240 */
11241 static void
11242f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011243 typval_T *argvars;
11244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011246 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247}
11248
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011249#ifdef FEAT_FLOAT
11250/*
11251 * "float2nr({float})" function
11252 */
11253 static void
11254f_float2nr(argvars, rettv)
11255 typval_T *argvars;
11256 typval_T *rettv;
11257{
11258 float_T f;
11259
11260 if (get_float_arg(argvars, &f) == OK)
11261 {
11262 if (f < -0x7fffffff)
11263 rettv->vval.v_number = -0x7fffffff;
11264 else if (f > 0x7fffffff)
11265 rettv->vval.v_number = 0x7fffffff;
11266 else
11267 rettv->vval.v_number = (varnumber_T)f;
11268 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011269}
11270
11271/*
11272 * "floor({float})" function
11273 */
11274 static void
11275f_floor(argvars, rettv)
11276 typval_T *argvars;
11277 typval_T *rettv;
11278{
11279 float_T f;
11280
11281 rettv->v_type = VAR_FLOAT;
11282 if (get_float_arg(argvars, &f) == OK)
11283 rettv->vval.v_float = floor(f);
11284 else
11285 rettv->vval.v_float = 0.0;
11286}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011287
11288/*
11289 * "fmod()" function
11290 */
11291 static void
11292f_fmod(argvars, rettv)
11293 typval_T *argvars;
11294 typval_T *rettv;
11295{
11296 float_T fx, fy;
11297
11298 rettv->v_type = VAR_FLOAT;
11299 if (get_float_arg(argvars, &fx) == OK
11300 && get_float_arg(&argvars[1], &fy) == OK)
11301 rettv->vval.v_float = fmod(fx, fy);
11302 else
11303 rettv->vval.v_float = 0.0;
11304}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011305#endif
11306
Bram Moolenaar0d660222005-01-07 21:51:51 +000011307/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011308 * "fnameescape({string})" function
11309 */
11310 static void
11311f_fnameescape(argvars, rettv)
11312 typval_T *argvars;
11313 typval_T *rettv;
11314{
11315 rettv->vval.v_string = vim_strsave_fnameescape(
11316 get_tv_string(&argvars[0]), FALSE);
11317 rettv->v_type = VAR_STRING;
11318}
11319
11320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 * "fnamemodify({fname}, {mods})" function
11322 */
11323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011325 typval_T *argvars;
11326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
11328 char_u *fname;
11329 char_u *mods;
11330 int usedlen = 0;
11331 int len;
11332 char_u *fbuf = NULL;
11333 char_u buf[NUMBUFLEN];
11334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 fname = get_tv_string_chk(&argvars[0]);
11336 mods = get_tv_string_buf_chk(&argvars[1], buf);
11337 if (fname == NULL || mods == NULL)
11338 fname = NULL;
11339 else
11340 {
11341 len = (int)STRLEN(fname);
11342 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 vim_free(fbuf);
11351}
11352
Bram Moolenaar33570922005-01-25 22:26:29 +000011353static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354
11355/*
11356 * "foldclosed()" function
11357 */
11358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011362 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
11364#ifdef FEAT_FOLDING
11365 linenr_T lnum;
11366 linenr_T first, last;
11367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11370 {
11371 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11372 {
11373 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 return;
11378 }
11379 }
11380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
11384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011385 * "foldclosed()" function
11386 */
11387 static void
11388f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011391{
11392 foldclosed_both(argvars, rettv, FALSE);
11393}
11394
11395/*
11396 * "foldclosedend()" function
11397 */
11398 static void
11399f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *argvars;
11401 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011402{
11403 foldclosed_both(argvars, rettv, TRUE);
11404}
11405
11406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 * "foldlevel()" function
11408 */
11409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011411 typval_T *argvars UNUSED;
11412 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
11414#ifdef FEAT_FOLDING
11415 linenr_T lnum;
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421}
11422
11423/*
11424 * "foldtext()" function
11425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430{
11431#ifdef FEAT_FOLDING
11432 linenr_T lnum;
11433 char_u *s;
11434 char_u *r;
11435 int len;
11436 char *txt;
11437#endif
11438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->v_type = VAR_STRING;
11440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11443 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11444 <= curbuf->b_ml.ml_line_count
11445 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 {
11447 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011448 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11449 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 {
11451 if (!linewhite(lnum))
11452 break;
11453 ++lnum;
11454 }
11455
11456 /* Find interesting text in this line. */
11457 s = skipwhite(ml_get(lnum));
11458 /* skip C comment-start */
11459 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011462 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011464 {
11465 s = skipwhite(ml_get(lnum + 1));
11466 if (*s == '*')
11467 s = skipwhite(s + 1);
11468 }
11469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 txt = _("+-%s%3ld lines: ");
11471 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 + 20 /* for %3ld */
11474 + STRLEN(s))); /* concatenated */
11475 if (r != NULL)
11476 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11478 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11479 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 len = (int)STRLEN(r);
11481 STRCAT(r, s);
11482 /* remove 'foldmarker' and 'commentstring' */
11483 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 }
11486 }
11487#endif
11488}
11489
11490/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011491 * "foldtextresult(lnum)" function
11492 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011497{
11498#ifdef FEAT_FOLDING
11499 linenr_T lnum;
11500 char_u *text;
11501 char_u buf[51];
11502 foldinfo_T foldinfo;
11503 int fold_count;
11504#endif
11505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->v_type = VAR_STRING;
11507 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011508#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011510 /* treat illegal types and illegal string values for {lnum} the same */
11511 if (lnum < 0)
11512 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011513 fold_count = foldedCount(curwin, lnum, &foldinfo);
11514 if (fold_count > 0)
11515 {
11516 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11517 &foldinfo, buf);
11518 if (text == buf)
11519 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011521 }
11522#endif
11523}
11524
11525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 * "foreground()" function
11527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011530 typval_T *argvars UNUSED;
11531 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533#ifdef FEAT_GUI
11534 if (gui.in_use)
11535 gui_mch_set_foreground();
11536#else
11537# ifdef WIN32
11538 win32_set_foreground();
11539# endif
11540#endif
11541}
11542
11543/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011544 * "function()" function
11545 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011548 typval_T *argvars;
11549 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011550{
11551 char_u *s;
11552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011556 /* Don't check an autoload name for existence here. */
11557 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011558 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559 else
11560 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011561 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011562 {
11563 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011564 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011565
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011566 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11567 * also be called from another script. Using trans_function_name()
11568 * would also work, but some plugins depend on the name being
11569 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011570 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011571 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011572 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011573 if (rettv->vval.v_string != NULL)
11574 {
11575 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011576 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011577 }
11578 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011579 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011580 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011582 }
11583}
11584
11585/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011586 * "garbagecollect()" function
11587 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011588 static void
11589f_garbagecollect(argvars, rettv)
11590 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011591 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011592{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011593 /* This is postponed until we are back at the toplevel, because we may be
11594 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11595 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011596
11597 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11598 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011599}
11600
11601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602 * "get()" function
11603 */
11604 static void
11605f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011606 typval_T *argvars;
11607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011608{
Bram Moolenaar33570922005-01-25 22:26:29 +000011609 listitem_T *li;
11610 list_T *l;
11611 dictitem_T *di;
11612 dict_T *d;
11613 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011616 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011617 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619 int error = FALSE;
11620
11621 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11622 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011623 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011626 else if (argvars[0].v_type == VAR_DICT)
11627 {
11628 if ((d = argvars[0].vval.v_dict) != NULL)
11629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011630 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 if (di != NULL)
11632 tv = &di->di_tv;
11633 }
11634 }
11635 else
11636 EMSG2(_(e_listdictarg), "get()");
11637
11638 if (tv == NULL)
11639 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011640 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011641 copy_tv(&argvars[2], rettv);
11642 }
11643 else
11644 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645}
11646
Bram Moolenaar342337a2005-07-21 21:11:17 +000011647static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011648
11649/*
11650 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651 * Return a range (from start to end) of lines in rettv from the specified
11652 * buffer.
11653 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011654 */
11655 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011656get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011657 buf_T *buf;
11658 linenr_T start;
11659 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011660 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011661 typval_T *rettv;
11662{
11663 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011664
Bram Moolenaar959a1432013-12-14 12:17:38 +010011665 rettv->v_type = VAR_STRING;
11666 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011667 if (retlist && rettv_list_alloc(rettv) == FAIL)
11668 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011669
11670 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11671 return;
11672
11673 if (!retlist)
11674 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011675 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11676 p = ml_get_buf(buf, start, FALSE);
11677 else
11678 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011679 rettv->vval.v_string = vim_strsave(p);
11680 }
11681 else
11682 {
11683 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 return;
11685
11686 if (start < 1)
11687 start = 1;
11688 if (end > buf->b_ml.ml_line_count)
11689 end = buf->b_ml.ml_line_count;
11690 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011691 if (list_append_string(rettv->vval.v_list,
11692 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011693 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011694 }
11695}
11696
11697/*
11698 * "getbufline()" function
11699 */
11700 static void
11701f_getbufline(argvars, rettv)
11702 typval_T *argvars;
11703 typval_T *rettv;
11704{
11705 linenr_T lnum;
11706 linenr_T end;
11707 buf_T *buf;
11708
11709 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11710 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011711 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011712 --emsg_off;
11713
Bram Moolenaar661b1822005-07-28 22:36:45 +000011714 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011715 if (argvars[2].v_type == VAR_UNKNOWN)
11716 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011717 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011718 end = get_tv_lnum_buf(&argvars[2], buf);
11719
Bram Moolenaar342337a2005-07-21 21:11:17 +000011720 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011721}
11722
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723/*
11724 * "getbufvar()" function
11725 */
11726 static void
11727f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *argvars;
11729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011730{
11731 buf_T *buf;
11732 buf_T *save_curbuf;
11733 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011735 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11738 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011739 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011740 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011742 rettv->v_type = VAR_STRING;
11743 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011744
11745 if (buf != NULL && varname != NULL)
11746 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011747 /* set curbuf to be our buf, temporarily */
11748 save_curbuf = curbuf;
11749 curbuf = buf;
11750
Bram Moolenaar0d660222005-01-07 21:51:51 +000011751 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011752 {
11753 if (get_option_tv(&varname, rettv, TRUE) == OK)
11754 done = TRUE;
11755 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011756 else if (STRCMP(varname, "changedtick") == 0)
11757 {
11758 rettv->v_type = VAR_NUMBER;
11759 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011760 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011761 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011762 else
11763 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011764 /* Look up the variable. */
11765 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11766 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11767 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011768 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011770 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011771 done = TRUE;
11772 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011773 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011774
11775 /* restore previous notion of curbuf */
11776 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777 }
11778
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011779 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11780 /* use the default value */
11781 copy_tv(&argvars[2], rettv);
11782
Bram Moolenaar0d660222005-01-07 21:51:51 +000011783 --emsg_off;
11784}
11785
11786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 * "getchar()" function
11788 */
11789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793{
11794 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011797 /* Position the cursor. Needed after a message that ends in a space. */
11798 windgoto(msg_row, msg_col);
11799
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 ++no_mapping;
11801 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011802 for (;;)
11803 {
11804 if (argvars[0].v_type == VAR_UNKNOWN)
11805 /* getchar(): blocking wait. */
11806 n = safe_vgetc();
11807 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11808 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011809 n = vpeekc_any();
11810 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011811 /* illegal argument or getchar(0) and no char avail: return zero */
11812 n = 0;
11813 else
11814 /* getchar(0) and char avail: return char */
11815 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011816
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011817 if (n == K_IGNORE)
11818 continue;
11819 break;
11820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 --no_mapping;
11822 --allow_keys;
11823
Bram Moolenaar219b8702006-11-01 14:32:36 +000011824 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11825 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11826 vimvars[VV_MOUSE_COL].vv_nr = 0;
11827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 if (IS_SPECIAL(n) || mod_mask != 0)
11830 {
11831 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11832 int i = 0;
11833
11834 /* Turn a special key into three bytes, plus modifier. */
11835 if (mod_mask != 0)
11836 {
11837 temp[i++] = K_SPECIAL;
11838 temp[i++] = KS_MODIFIER;
11839 temp[i++] = mod_mask;
11840 }
11841 if (IS_SPECIAL(n))
11842 {
11843 temp[i++] = K_SPECIAL;
11844 temp[i++] = K_SECOND(n);
11845 temp[i++] = K_THIRD(n);
11846 }
11847#ifdef FEAT_MBYTE
11848 else if (has_mbyte)
11849 i += (*mb_char2bytes)(n, temp + i);
11850#endif
11851 else
11852 temp[i++] = n;
11853 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->v_type = VAR_STRING;
11855 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011856
11857#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011858 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011859 {
11860 int row = mouse_row;
11861 int col = mouse_col;
11862 win_T *win;
11863 linenr_T lnum;
11864# ifdef FEAT_WINDOWS
11865 win_T *wp;
11866# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011867 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011868
11869 if (row >= 0 && col >= 0)
11870 {
11871 /* Find the window at the mouse coordinates and compute the
11872 * text position. */
11873 win = mouse_find_win(&row, &col);
11874 (void)mouse_comp_pos(win, &row, &col, &lnum);
11875# ifdef FEAT_WINDOWS
11876 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011877 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011878# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011879 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011880 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11881 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11882 }
11883 }
11884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 }
11886}
11887
11888/*
11889 * "getcharmod()" function
11890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011893 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897}
11898
11899/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020011900 * "getcharsearch()" function
11901 */
11902 static void
11903f_getcharsearch(argvars, rettv)
11904 typval_T *argvars UNUSED;
11905 typval_T *rettv;
11906{
11907 if (rettv_dict_alloc(rettv) != FAIL)
11908 {
11909 dict_T *dict = rettv->vval.v_dict;
11910
11911 dict_add_nr_str(dict, "char", 0L, last_csearch());
11912 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
11913 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
11914 }
11915}
11916
11917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 * "getcmdline()" function
11919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925 rettv->v_type = VAR_STRING;
11926 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927}
11928
11929/*
11930 * "getcmdpos()" function
11931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938}
11939
11940/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011941 * "getcmdtype()" function
11942 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011943 static void
11944f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011945 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011946 typval_T *rettv;
11947{
11948 rettv->v_type = VAR_STRING;
11949 rettv->vval.v_string = alloc(2);
11950 if (rettv->vval.v_string != NULL)
11951 {
11952 rettv->vval.v_string[0] = get_cmdline_type();
11953 rettv->vval.v_string[1] = NUL;
11954 }
11955}
11956
11957/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011958 * "getcmdwintype()" function
11959 */
11960 static void
11961f_getcmdwintype(argvars, rettv)
11962 typval_T *argvars UNUSED;
11963 typval_T *rettv;
11964{
11965 rettv->v_type = VAR_STRING;
11966 rettv->vval.v_string = NULL;
11967#ifdef FEAT_CMDWIN
11968 rettv->vval.v_string = alloc(2);
11969 if (rettv->vval.v_string != NULL)
11970 {
11971 rettv->vval.v_string[0] = cmdwin_type;
11972 rettv->vval.v_string[1] = NUL;
11973 }
11974#endif
11975}
11976
11977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 * "getcwd()" function
11979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011985 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011987 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011988 rettv->vval.v_string = NULL;
11989 cwd = alloc(MAXPATHL);
11990 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011992 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11993 {
11994 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011996 if (rettv->vval.v_string != NULL)
11997 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011999 }
12000 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001 }
12002}
12003
12004/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012005 * "getfontname()" function
12006 */
12007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012011{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 rettv->v_type = VAR_STRING;
12013 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012014#ifdef FEAT_GUI
12015 if (gui.in_use)
12016 {
12017 GuiFont font;
12018 char_u *name = NULL;
12019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012020 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012021 {
12022 /* Get the "Normal" font. Either the name saved by
12023 * hl_set_font_name() or from the font ID. */
12024 font = gui.norm_font;
12025 name = hl_get_font_name();
12026 }
12027 else
12028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012030 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12031 return;
12032 font = gui_mch_get_font(name, FALSE);
12033 if (font == NOFONT)
12034 return; /* Invalid font name, return empty string. */
12035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012037 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012038 gui_mch_free_font(font);
12039 }
12040#endif
12041}
12042
12043/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012044 * "getfperm({fname})" function
12045 */
12046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012047f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012048 typval_T *argvars;
12049 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012050{
12051 char_u *fname;
12052 struct stat st;
12053 char_u *perm = NULL;
12054 char_u flags[] = "rwx";
12055 int i;
12056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012060 if (mch_stat((char *)fname, &st) >= 0)
12061 {
12062 perm = vim_strsave((char_u *)"---------");
12063 if (perm != NULL)
12064 {
12065 for (i = 0; i < 9; i++)
12066 {
12067 if (st.st_mode & (1 << (8 - i)))
12068 perm[i] = flags[i % 3];
12069 }
12070 }
12071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012073}
12074
12075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 * "getfsize({fname})" function
12077 */
12078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012080 typval_T *argvars;
12081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082{
12083 char_u *fname;
12084 struct stat st;
12085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012086 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089
12090 if (mch_stat((char *)fname, &st) >= 0)
12091 {
12092 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012097
12098 /* non-perfect check for overflow */
12099 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12100 rettv->vval.v_number = -2;
12101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 }
12103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105}
12106
12107/*
12108 * "getftime({fname})" function
12109 */
12110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012112 typval_T *argvars;
12113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114{
12115 char_u *fname;
12116 struct stat st;
12117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119
12120 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124}
12125
12126/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012127 * "getftype({fname})" function
12128 */
12129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 typval_T *argvars;
12132 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012133{
12134 char_u *fname;
12135 struct stat st;
12136 char_u *type = NULL;
12137 char *t;
12138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012142 if (mch_lstat((char *)fname, &st) >= 0)
12143 {
12144#ifdef S_ISREG
12145 if (S_ISREG(st.st_mode))
12146 t = "file";
12147 else if (S_ISDIR(st.st_mode))
12148 t = "dir";
12149# ifdef S_ISLNK
12150 else if (S_ISLNK(st.st_mode))
12151 t = "link";
12152# endif
12153# ifdef S_ISBLK
12154 else if (S_ISBLK(st.st_mode))
12155 t = "bdev";
12156# endif
12157# ifdef S_ISCHR
12158 else if (S_ISCHR(st.st_mode))
12159 t = "cdev";
12160# endif
12161# ifdef S_ISFIFO
12162 else if (S_ISFIFO(st.st_mode))
12163 t = "fifo";
12164# endif
12165# ifdef S_ISSOCK
12166 else if (S_ISSOCK(st.st_mode))
12167 t = "fifo";
12168# endif
12169 else
12170 t = "other";
12171#else
12172# ifdef S_IFMT
12173 switch (st.st_mode & S_IFMT)
12174 {
12175 case S_IFREG: t = "file"; break;
12176 case S_IFDIR: t = "dir"; break;
12177# ifdef S_IFLNK
12178 case S_IFLNK: t = "link"; break;
12179# endif
12180# ifdef S_IFBLK
12181 case S_IFBLK: t = "bdev"; break;
12182# endif
12183# ifdef S_IFCHR
12184 case S_IFCHR: t = "cdev"; break;
12185# endif
12186# ifdef S_IFIFO
12187 case S_IFIFO: t = "fifo"; break;
12188# endif
12189# ifdef S_IFSOCK
12190 case S_IFSOCK: t = "socket"; break;
12191# endif
12192 default: t = "other";
12193 }
12194# else
12195 if (mch_isdir(fname))
12196 t = "dir";
12197 else
12198 t = "file";
12199# endif
12200#endif
12201 type = vim_strsave((char_u *)t);
12202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012204}
12205
12206/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012207 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012208 */
12209 static void
12210f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 typval_T *argvars;
12212 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213{
12214 linenr_T lnum;
12215 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012216 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217
12218 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012219 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012220 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012221 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012222 retlist = FALSE;
12223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012225 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012226 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012227 retlist = TRUE;
12228 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012229
Bram Moolenaar342337a2005-07-21 21:11:17 +000012230 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012231}
12232
12233/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012234 * "getmatches()" function
12235 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012236 static void
12237f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012238 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012239 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012240{
12241#ifdef FEAT_SEARCH_EXTRA
12242 dict_T *dict;
12243 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012244 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012245
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012246 if (rettv_list_alloc(rettv) == OK)
12247 {
12248 while (cur != NULL)
12249 {
12250 dict = dict_alloc();
12251 if (dict == NULL)
12252 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012253 if (cur->match.regprog == NULL)
12254 {
12255 /* match added with matchaddpos() */
12256 for (i = 0; i < MAXPOSMATCH; ++i)
12257 {
12258 llpos_T *llpos;
12259 char buf[6];
12260 list_T *l;
12261
12262 llpos = &cur->pos.pos[i];
12263 if (llpos->lnum == 0)
12264 break;
12265 l = list_alloc();
12266 if (l == NULL)
12267 break;
12268 list_append_number(l, (varnumber_T)llpos->lnum);
12269 if (llpos->col > 0)
12270 {
12271 list_append_number(l, (varnumber_T)llpos->col);
12272 list_append_number(l, (varnumber_T)llpos->len);
12273 }
12274 sprintf(buf, "pos%d", i + 1);
12275 dict_add_list(dict, buf, l);
12276 }
12277 }
12278 else
12279 {
12280 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12281 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012282 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012283 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12284 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012285# ifdef FEAT_CONCEAL
12286 if (cur->conceal_char)
12287 {
12288 char_u buf[MB_MAXBYTES + 1];
12289
12290 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12291 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12292 }
12293# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012294 list_append_dict(rettv->vval.v_list, dict);
12295 cur = cur->next;
12296 }
12297 }
12298#endif
12299}
12300
12301/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012302 * "getpid()" function
12303 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012304 static void
12305f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012306 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012307 typval_T *rettv;
12308{
12309 rettv->vval.v_number = mch_get_pid();
12310}
12311
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012312static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
12313
12314/*
12315 * "getcurpos()" function
12316 */
12317 static void
12318f_getcurpos(argvars, rettv)
12319 typval_T *argvars;
12320 typval_T *rettv;
12321{
12322 getpos_both(argvars, rettv, TRUE);
12323}
12324
Bram Moolenaar18081e32008-02-20 19:11:07 +000012325/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012326 * "getpos(string)" function
12327 */
12328 static void
12329f_getpos(argvars, rettv)
12330 typval_T *argvars;
12331 typval_T *rettv;
12332{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012333 getpos_both(argvars, rettv, FALSE);
12334}
12335
12336 static void
12337getpos_both(argvars, rettv, getcurpos)
12338 typval_T *argvars;
12339 typval_T *rettv;
12340 int getcurpos;
12341{
Bram Moolenaara5525202006-03-02 22:52:09 +000012342 pos_T *fp;
12343 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012344 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012345
12346 if (rettv_list_alloc(rettv) == OK)
12347 {
12348 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012349 if (getcurpos)
12350 fp = &curwin->w_cursor;
12351 else
12352 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012353 if (fnum != -1)
12354 list_append_number(l, (varnumber_T)fnum);
12355 else
12356 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012357 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12358 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012359 list_append_number(l, (fp != NULL)
12360 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012361 : (varnumber_T)0);
12362 list_append_number(l,
12363#ifdef FEAT_VIRTUALEDIT
12364 (fp != NULL) ? (varnumber_T)fp->coladd :
12365#endif
12366 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012367 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012368 list_append_number(l, curwin->w_curswant == MAXCOL ?
12369 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012370 }
12371 else
12372 rettv->vval.v_number = FALSE;
12373}
12374
12375/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012376 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012377 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012378 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012379f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012380 typval_T *argvars UNUSED;
12381 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012382{
12383#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012384 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012385#endif
12386
Bram Moolenaar2641f772005-03-25 21:58:17 +000012387#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012388 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012389 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012390 wp = NULL;
12391 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12392 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012393 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012394 if (wp == NULL)
12395 return;
12396 }
12397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012398 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012399 }
12400#endif
12401}
12402
12403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 * "getreg()" function
12405 */
12406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012408 typval_T *argvars;
12409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410{
12411 char_u *strregname;
12412 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012413 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012414 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012417 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 strregname = get_tv_string_chk(&argvars[0]);
12420 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012421 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012423 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012424 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12425 return_list = get_tv_number_chk(&argvars[2], &error);
12426 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012429 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012430
12431 if (error)
12432 return;
12433
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 regname = (strregname == NULL ? '"' : *strregname);
12435 if (regname == 0)
12436 regname = '"';
12437
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012438 if (return_list)
12439 {
12440 rettv->v_type = VAR_LIST;
12441 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12442 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012443 if (rettv->vval.v_list != NULL)
12444 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012445 }
12446 else
12447 {
12448 rettv->v_type = VAR_STRING;
12449 rettv->vval.v_string = get_reg_contents(regname,
12450 arg2 ? GREG_EXPR_SRC : 0);
12451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452}
12453
12454/*
12455 * "getregtype()" function
12456 */
12457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 typval_T *argvars;
12460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461{
12462 char_u *strregname;
12463 int regname;
12464 char_u buf[NUMBUFLEN + 2];
12465 long reglen = 0;
12466
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012467 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 {
12469 strregname = get_tv_string_chk(&argvars[0]);
12470 if (strregname == NULL) /* type error; errmsg already given */
12471 {
12472 rettv->v_type = VAR_STRING;
12473 rettv->vval.v_string = NULL;
12474 return;
12475 }
12476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 else
12478 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012479 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
12481 regname = (strregname == NULL ? '"' : *strregname);
12482 if (regname == 0)
12483 regname = '"';
12484
12485 buf[0] = NUL;
12486 buf[1] = NUL;
12487 switch (get_reg_type(regname, &reglen))
12488 {
12489 case MLINE: buf[0] = 'V'; break;
12490 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 case MBLOCK:
12492 buf[0] = Ctrl_V;
12493 sprintf((char *)buf + 1, "%ld", reglen + 1);
12494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 rettv->v_type = VAR_STRING;
12497 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498}
12499
12500/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012501 * "gettabvar()" function
12502 */
12503 static void
12504f_gettabvar(argvars, rettv)
12505 typval_T *argvars;
12506 typval_T *rettv;
12507{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012508 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012509 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012510 dictitem_T *v;
12511 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012512 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012513
12514 rettv->v_type = VAR_STRING;
12515 rettv->vval.v_string = NULL;
12516
12517 varname = get_tv_string_chk(&argvars[1]);
12518 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12519 if (tp != NULL && varname != NULL)
12520 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012521 /* Set tp to be our tabpage, temporarily. Also set the window to the
12522 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012523 if (switch_win(&oldcurwin, &oldtabpage,
12524 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012525 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012526 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012527 /* look up the variable */
12528 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12529 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12530 if (v != NULL)
12531 {
12532 copy_tv(&v->di_tv, rettv);
12533 done = TRUE;
12534 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012535 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012536
12537 /* restore previous notion of curwin */
12538 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012539 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012540
12541 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012542 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012543}
12544
12545/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012546 * "gettabwinvar()" function
12547 */
12548 static void
12549f_gettabwinvar(argvars, rettv)
12550 typval_T *argvars;
12551 typval_T *rettv;
12552{
12553 getwinvar(argvars, rettv, 1);
12554}
12555
12556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 * "getwinposx()" function
12558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565#ifdef FEAT_GUI
12566 if (gui.in_use)
12567 {
12568 int x, y;
12569
12570 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 }
12573#endif
12574}
12575
12576/*
12577 * "getwinposy()" function
12578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#ifdef FEAT_GUI
12586 if (gui.in_use)
12587 {
12588 int x, y;
12589
12590 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 }
12593#endif
12594}
12595
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012596/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012597 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012598 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012599 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012600find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012601 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012602 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012603{
12604#ifdef FEAT_WINDOWS
12605 win_T *wp;
12606#endif
12607 int nr;
12608
12609 nr = get_tv_number_chk(vp, NULL);
12610
12611#ifdef FEAT_WINDOWS
12612 if (nr < 0)
12613 return NULL;
12614 if (nr == 0)
12615 return curwin;
12616
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012617 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12618 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012619 if (--nr <= 0)
12620 break;
12621 return wp;
12622#else
12623 if (nr == 0 || nr == 1)
12624 return curwin;
12625 return NULL;
12626#endif
12627}
12628
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629/*
12630 * "getwinvar()" function
12631 */
12632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012634 typval_T *argvars;
12635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012637 getwinvar(argvars, rettv, 0);
12638}
12639
12640/*
12641 * getwinvar() and gettabwinvar()
12642 */
12643 static void
12644getwinvar(argvars, rettv, off)
12645 typval_T *argvars;
12646 typval_T *rettv;
12647 int off; /* 1 for gettabwinvar() */
12648{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012649 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012652 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012653 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012654#ifdef FEAT_WINDOWS
12655 win_T *oldcurwin;
12656 tabpage_T *oldtabpage;
12657 int need_switch_win;
12658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012660#ifdef FEAT_WINDOWS
12661 if (off == 1)
12662 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12663 else
12664 tp = curtab;
12665#endif
12666 win = find_win_by_nr(&argvars[off], tp);
12667 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012670 rettv->v_type = VAR_STRING;
12671 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
12673 if (win != NULL && varname != NULL)
12674 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012675#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012676 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012677 * otherwise the window is not valid. Only do this when needed,
12678 * autocommands get blocked. */
12679 need_switch_win = !(tp == curtab && win == curwin);
12680 if (!need_switch_win
12681 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12682#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012683 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012684 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012685 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012686 if (get_option_tv(&varname, rettv, 1) == OK)
12687 done = TRUE;
12688 }
12689 else
12690 {
12691 /* Look up the variable. */
12692 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12693 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12694 varname, FALSE);
12695 if (v != NULL)
12696 {
12697 copy_tv(&v->di_tv, rettv);
12698 done = TRUE;
12699 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012702
Bram Moolenaarba117c22015-09-29 16:53:22 +020012703#ifdef FEAT_WINDOWS
12704 if (need_switch_win)
12705 /* restore previous notion of curwin */
12706 restore_win(oldcurwin, oldtabpage, TRUE);
12707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 }
12709
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012710 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12711 /* use the default return value */
12712 copy_tv(&argvars[off + 2], rettv);
12713
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 --emsg_off;
12715}
12716
12717/*
12718 * "glob()" function
12719 */
12720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012725 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012727 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012729 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012730 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012732 if (argvars[1].v_type != VAR_UNKNOWN)
12733 {
12734 if (get_tv_number_chk(&argvars[1], &error))
12735 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012736 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012737 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012738 if (get_tv_number_chk(&argvars[2], &error))
12739 {
12740 rettv->v_type = VAR_LIST;
12741 rettv->vval.v_list = NULL;
12742 }
12743 if (argvars[3].v_type != VAR_UNKNOWN
12744 && get_tv_number_chk(&argvars[3], &error))
12745 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012746 }
12747 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012748 if (!error)
12749 {
12750 ExpandInit(&xpc);
12751 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012752 if (p_wic)
12753 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012754 if (rettv->v_type == VAR_STRING)
12755 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012756 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012757 else if (rettv_list_alloc(rettv) != FAIL)
12758 {
12759 int i;
12760
12761 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12762 NULL, options, WILD_ALL_KEEP);
12763 for (i = 0; i < xpc.xp_numfiles; i++)
12764 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12765
12766 ExpandCleanup(&xpc);
12767 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012768 }
12769 else
12770 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
12774 * "globpath()" function
12775 */
12776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *argvars;
12779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012781 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012784 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012785 garray_T ga;
12786 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012788 /* When the optional second argument is non-zero, don't remove matches
12789 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012791 if (argvars[2].v_type != VAR_UNKNOWN)
12792 {
12793 if (get_tv_number_chk(&argvars[2], &error))
12794 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012796 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012797 if (get_tv_number_chk(&argvars[3], &error))
12798 {
12799 rettv->v_type = VAR_LIST;
12800 rettv->vval.v_list = NULL;
12801 }
12802 if (argvars[4].v_type != VAR_UNKNOWN
12803 && get_tv_number_chk(&argvars[4], &error))
12804 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012805 }
12806 }
12807 if (file != NULL && !error)
12808 {
12809 ga_init2(&ga, (int)sizeof(char_u *), 10);
12810 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12811 if (rettv->v_type == VAR_STRING)
12812 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12813 else if (rettv_list_alloc(rettv) != FAIL)
12814 for (i = 0; i < ga.ga_len; ++i)
12815 list_append_string(rettv->vval.v_list,
12816 ((char_u **)(ga.ga_data))[i], -1);
12817 ga_clear_strings(&ga);
12818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012819 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012824 * "glob2regpat()" function
12825 */
12826 static void
12827f_glob2regpat(argvars, rettv)
12828 typval_T *argvars;
12829 typval_T *rettv;
12830{
12831 char_u *pat = get_tv_string_chk(&argvars[0]);
12832
12833 rettv->v_type = VAR_STRING;
12834 rettv->vval.v_string = file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
12835}
12836
12837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 * "has()" function
12839 */
12840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 typval_T *argvars;
12843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844{
12845 int i;
12846 char_u *name;
12847 int n = FALSE;
12848 static char *(has_list[]) =
12849 {
12850#ifdef AMIGA
12851 "amiga",
12852# ifdef FEAT_ARP
12853 "arp",
12854# endif
12855#endif
12856#ifdef __BEOS__
12857 "beos",
12858#endif
12859#ifdef MSDOS
12860# ifdef DJGPP
12861 "dos32",
12862# else
12863 "dos16",
12864# endif
12865#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012866#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 "mac",
12868#endif
12869#if defined(MACOS_X_UNIX)
12870 "macunix",
12871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872#ifdef __QNX__
12873 "qnx",
12874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875#ifdef UNIX
12876 "unix",
12877#endif
12878#ifdef VMS
12879 "vms",
12880#endif
12881#ifdef WIN16
12882 "win16",
12883#endif
12884#ifdef WIN32
12885 "win32",
12886#endif
12887#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12888 "win32unix",
12889#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012890#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 "win64",
12892#endif
12893#ifdef EBCDIC
12894 "ebcdic",
12895#endif
12896#ifndef CASE_INSENSITIVE_FILENAME
12897 "fname_case",
12898#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012899#ifdef HAVE_ACL
12900 "acl",
12901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902#ifdef FEAT_ARABIC
12903 "arabic",
12904#endif
12905#ifdef FEAT_AUTOCMD
12906 "autocmd",
12907#endif
12908#ifdef FEAT_BEVAL
12909 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012910# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12911 "balloon_multiline",
12912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913#endif
12914#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12915 "builtin_terms",
12916# ifdef ALL_BUILTIN_TCAPS
12917 "all_builtin_terms",
12918# endif
12919#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012920#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12921 || defined(FEAT_GUI_W32) \
12922 || defined(FEAT_GUI_MOTIF))
12923 "browsefilter",
12924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925#ifdef FEAT_BYTEOFF
12926 "byte_offset",
12927#endif
12928#ifdef FEAT_CINDENT
12929 "cindent",
12930#endif
12931#ifdef FEAT_CLIENTSERVER
12932 "clientserver",
12933#endif
12934#ifdef FEAT_CLIPBOARD
12935 "clipboard",
12936#endif
12937#ifdef FEAT_CMDL_COMPL
12938 "cmdline_compl",
12939#endif
12940#ifdef FEAT_CMDHIST
12941 "cmdline_hist",
12942#endif
12943#ifdef FEAT_COMMENTS
12944 "comments",
12945#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012946#ifdef FEAT_CONCEAL
12947 "conceal",
12948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef FEAT_CRYPT
12950 "cryptv",
12951#endif
12952#ifdef FEAT_CSCOPE
12953 "cscope",
12954#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012955#ifdef FEAT_CURSORBIND
12956 "cursorbind",
12957#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012958#ifdef CURSOR_SHAPE
12959 "cursorshape",
12960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961#ifdef DEBUG
12962 "debug",
12963#endif
12964#ifdef FEAT_CON_DIALOG
12965 "dialog_con",
12966#endif
12967#ifdef FEAT_GUI_DIALOG
12968 "dialog_gui",
12969#endif
12970#ifdef FEAT_DIFF
12971 "diff",
12972#endif
12973#ifdef FEAT_DIGRAPHS
12974 "digraphs",
12975#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012976#ifdef FEAT_DIRECTX
12977 "directx",
12978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979#ifdef FEAT_DND
12980 "dnd",
12981#endif
12982#ifdef FEAT_EMACS_TAGS
12983 "emacs_tags",
12984#endif
12985 "eval", /* always present, of course! */
12986#ifdef FEAT_EX_EXTRA
12987 "ex_extra",
12988#endif
12989#ifdef FEAT_SEARCH_EXTRA
12990 "extra_search",
12991#endif
12992#ifdef FEAT_FKMAP
12993 "farsi",
12994#endif
12995#ifdef FEAT_SEARCHPATH
12996 "file_in_path",
12997#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012998#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012999 "filterpipe",
13000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001#ifdef FEAT_FIND_ID
13002 "find_in_path",
13003#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013004#ifdef FEAT_FLOAT
13005 "float",
13006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007#ifdef FEAT_FOLDING
13008 "folding",
13009#endif
13010#ifdef FEAT_FOOTER
13011 "footer",
13012#endif
13013#if !defined(USE_SYSTEM) && defined(UNIX)
13014 "fork",
13015#endif
13016#ifdef FEAT_GETTEXT
13017 "gettext",
13018#endif
13019#ifdef FEAT_GUI
13020 "gui",
13021#endif
13022#ifdef FEAT_GUI_ATHENA
13023# ifdef FEAT_GUI_NEXTAW
13024 "gui_neXtaw",
13025# else
13026 "gui_athena",
13027# endif
13028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029#ifdef FEAT_GUI_GTK
13030 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013033#ifdef FEAT_GUI_GNOME
13034 "gui_gnome",
13035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#ifdef FEAT_GUI_MAC
13037 "gui_mac",
13038#endif
13039#ifdef FEAT_GUI_MOTIF
13040 "gui_motif",
13041#endif
13042#ifdef FEAT_GUI_PHOTON
13043 "gui_photon",
13044#endif
13045#ifdef FEAT_GUI_W16
13046 "gui_win16",
13047#endif
13048#ifdef FEAT_GUI_W32
13049 "gui_win32",
13050#endif
13051#ifdef FEAT_HANGULIN
13052 "hangul_input",
13053#endif
13054#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13055 "iconv",
13056#endif
13057#ifdef FEAT_INS_EXPAND
13058 "insert_expand",
13059#endif
13060#ifdef FEAT_JUMPLIST
13061 "jumplist",
13062#endif
13063#ifdef FEAT_KEYMAP
13064 "keymap",
13065#endif
13066#ifdef FEAT_LANGMAP
13067 "langmap",
13068#endif
13069#ifdef FEAT_LIBCALL
13070 "libcall",
13071#endif
13072#ifdef FEAT_LINEBREAK
13073 "linebreak",
13074#endif
13075#ifdef FEAT_LISP
13076 "lispindent",
13077#endif
13078#ifdef FEAT_LISTCMDS
13079 "listcmds",
13080#endif
13081#ifdef FEAT_LOCALMAP
13082 "localmap",
13083#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013084#ifdef FEAT_LUA
13085# ifndef DYNAMIC_LUA
13086 "lua",
13087# endif
13088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089#ifdef FEAT_MENU
13090 "menu",
13091#endif
13092#ifdef FEAT_SESSION
13093 "mksession",
13094#endif
13095#ifdef FEAT_MODIFY_FNAME
13096 "modify_fname",
13097#endif
13098#ifdef FEAT_MOUSE
13099 "mouse",
13100#endif
13101#ifdef FEAT_MOUSESHAPE
13102 "mouseshape",
13103#endif
13104#if defined(UNIX) || defined(VMS)
13105# ifdef FEAT_MOUSE_DEC
13106 "mouse_dec",
13107# endif
13108# ifdef FEAT_MOUSE_GPM
13109 "mouse_gpm",
13110# endif
13111# ifdef FEAT_MOUSE_JSB
13112 "mouse_jsbterm",
13113# endif
13114# ifdef FEAT_MOUSE_NET
13115 "mouse_netterm",
13116# endif
13117# ifdef FEAT_MOUSE_PTERM
13118 "mouse_pterm",
13119# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013120# ifdef FEAT_MOUSE_SGR
13121 "mouse_sgr",
13122# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013123# ifdef FEAT_SYSMOUSE
13124 "mouse_sysmouse",
13125# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013126# ifdef FEAT_MOUSE_URXVT
13127 "mouse_urxvt",
13128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129# ifdef FEAT_MOUSE_XTERM
13130 "mouse_xterm",
13131# endif
13132#endif
13133#ifdef FEAT_MBYTE
13134 "multi_byte",
13135#endif
13136#ifdef FEAT_MBYTE_IME
13137 "multi_byte_ime",
13138#endif
13139#ifdef FEAT_MULTI_LANG
13140 "multi_lang",
13141#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013142#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013143#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013144 "mzscheme",
13145#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147#ifdef FEAT_OLE
13148 "ole",
13149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_PATH_EXTRA
13151 "path_extra",
13152#endif
13153#ifdef FEAT_PERL
13154#ifndef DYNAMIC_PERL
13155 "perl",
13156#endif
13157#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013158#ifdef FEAT_PERSISTENT_UNDO
13159 "persistent_undo",
13160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161#ifdef FEAT_PYTHON
13162#ifndef DYNAMIC_PYTHON
13163 "python",
13164#endif
13165#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013166#ifdef FEAT_PYTHON3
13167#ifndef DYNAMIC_PYTHON3
13168 "python3",
13169#endif
13170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171#ifdef FEAT_POSTSCRIPT
13172 "postscript",
13173#endif
13174#ifdef FEAT_PRINTER
13175 "printer",
13176#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013177#ifdef FEAT_PROFILE
13178 "profile",
13179#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013180#ifdef FEAT_RELTIME
13181 "reltime",
13182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183#ifdef FEAT_QUICKFIX
13184 "quickfix",
13185#endif
13186#ifdef FEAT_RIGHTLEFT
13187 "rightleft",
13188#endif
13189#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13190 "ruby",
13191#endif
13192#ifdef FEAT_SCROLLBIND
13193 "scrollbind",
13194#endif
13195#ifdef FEAT_CMDL_INFO
13196 "showcmd",
13197 "cmdline_info",
13198#endif
13199#ifdef FEAT_SIGNS
13200 "signs",
13201#endif
13202#ifdef FEAT_SMARTINDENT
13203 "smartindent",
13204#endif
13205#ifdef FEAT_SNIFF
13206 "sniff",
13207#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013208#ifdef STARTUPTIME
13209 "startuptime",
13210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211#ifdef FEAT_STL_OPT
13212 "statusline",
13213#endif
13214#ifdef FEAT_SUN_WORKSHOP
13215 "sun_workshop",
13216#endif
13217#ifdef FEAT_NETBEANS_INTG
13218 "netbeans_intg",
13219#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013220#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013221 "spell",
13222#endif
13223#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 "syntax",
13225#endif
13226#if defined(USE_SYSTEM) || !defined(UNIX)
13227 "system",
13228#endif
13229#ifdef FEAT_TAG_BINS
13230 "tag_binary",
13231#endif
13232#ifdef FEAT_TAG_OLDSTATIC
13233 "tag_old_static",
13234#endif
13235#ifdef FEAT_TAG_ANYWHITE
13236 "tag_any_white",
13237#endif
13238#ifdef FEAT_TCL
13239# ifndef DYNAMIC_TCL
13240 "tcl",
13241# endif
13242#endif
13243#ifdef TERMINFO
13244 "terminfo",
13245#endif
13246#ifdef FEAT_TERMRESPONSE
13247 "termresponse",
13248#endif
13249#ifdef FEAT_TEXTOBJ
13250 "textobjects",
13251#endif
13252#ifdef HAVE_TGETENT
13253 "tgetent",
13254#endif
13255#ifdef FEAT_TITLE
13256 "title",
13257#endif
13258#ifdef FEAT_TOOLBAR
13259 "toolbar",
13260#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013261#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13262 "unnamedplus",
13263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264#ifdef FEAT_USR_CMDS
13265 "user-commands", /* was accidentally included in 5.4 */
13266 "user_commands",
13267#endif
13268#ifdef FEAT_VIMINFO
13269 "viminfo",
13270#endif
13271#ifdef FEAT_VERTSPLIT
13272 "vertsplit",
13273#endif
13274#ifdef FEAT_VIRTUALEDIT
13275 "virtualedit",
13276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278#ifdef FEAT_VISUALEXTRA
13279 "visualextra",
13280#endif
13281#ifdef FEAT_VREPLACE
13282 "vreplace",
13283#endif
13284#ifdef FEAT_WILDIGN
13285 "wildignore",
13286#endif
13287#ifdef FEAT_WILDMENU
13288 "wildmenu",
13289#endif
13290#ifdef FEAT_WINDOWS
13291 "windows",
13292#endif
13293#ifdef FEAT_WAK
13294 "winaltkeys",
13295#endif
13296#ifdef FEAT_WRITEBACKUP
13297 "writebackup",
13298#endif
13299#ifdef FEAT_XIM
13300 "xim",
13301#endif
13302#ifdef FEAT_XFONTSET
13303 "xfontset",
13304#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013305#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013306 "xpm",
13307 "xpm_w32", /* for backward compatibility */
13308#else
13309# if defined(HAVE_XPM)
13310 "xpm",
13311# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef USE_XSMP
13314 "xsmp",
13315#endif
13316#ifdef USE_XSMP_INTERACT
13317 "xsmp_interact",
13318#endif
13319#ifdef FEAT_XCLIPBOARD
13320 "xterm_clipboard",
13321#endif
13322#ifdef FEAT_XTERM_SAVE
13323 "xterm_save",
13324#endif
13325#if defined(UNIX) && defined(FEAT_X11)
13326 "X11",
13327#endif
13328 NULL
13329 };
13330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 for (i = 0; has_list[i] != NULL; ++i)
13333 if (STRICMP(name, has_list[i]) == 0)
13334 {
13335 n = TRUE;
13336 break;
13337 }
13338
13339 if (n == FALSE)
13340 {
13341 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013342 {
13343 if (name[5] == '-'
13344 && STRLEN(name) > 11
13345 && vim_isdigit(name[6])
13346 && vim_isdigit(name[8])
13347 && vim_isdigit(name[10]))
13348 {
13349 int major = atoi((char *)name + 6);
13350 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013351
13352 /* Expect "patch-9.9.01234". */
13353 n = (major < VIM_VERSION_MAJOR
13354 || (major == VIM_VERSION_MAJOR
13355 && (minor < VIM_VERSION_MINOR
13356 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013357 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013358 }
13359 else
13360 n = has_patch(atoi((char *)name + 5));
13361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 else if (STRICMP(name, "vim_starting") == 0)
13363 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013364#ifdef FEAT_MBYTE
13365 else if (STRICMP(name, "multi_byte_encoding") == 0)
13366 n = has_mbyte;
13367#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013368#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13369 else if (STRICMP(name, "balloon_multiline") == 0)
13370 n = multiline_balloon_available();
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef DYNAMIC_TCL
13373 else if (STRICMP(name, "tcl") == 0)
13374 n = tcl_enabled(FALSE);
13375#endif
13376#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13377 else if (STRICMP(name, "iconv") == 0)
13378 n = iconv_enabled(FALSE);
13379#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013380#ifdef DYNAMIC_LUA
13381 else if (STRICMP(name, "lua") == 0)
13382 n = lua_enabled(FALSE);
13383#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013384#ifdef DYNAMIC_MZSCHEME
13385 else if (STRICMP(name, "mzscheme") == 0)
13386 n = mzscheme_enabled(FALSE);
13387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388#ifdef DYNAMIC_RUBY
13389 else if (STRICMP(name, "ruby") == 0)
13390 n = ruby_enabled(FALSE);
13391#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013392#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef DYNAMIC_PYTHON
13394 else if (STRICMP(name, "python") == 0)
13395 n = python_enabled(FALSE);
13396#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013397#endif
13398#ifdef FEAT_PYTHON3
13399#ifdef DYNAMIC_PYTHON3
13400 else if (STRICMP(name, "python3") == 0)
13401 n = python3_enabled(FALSE);
13402#endif
13403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404#ifdef DYNAMIC_PERL
13405 else if (STRICMP(name, "perl") == 0)
13406 n = perl_enabled(FALSE);
13407#endif
13408#ifdef FEAT_GUI
13409 else if (STRICMP(name, "gui_running") == 0)
13410 n = (gui.in_use || gui.starting);
13411# ifdef FEAT_GUI_W32
13412 else if (STRICMP(name, "gui_win32s") == 0)
13413 n = gui_is_win32s();
13414# endif
13415# ifdef FEAT_BROWSE
13416 else if (STRICMP(name, "browse") == 0)
13417 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13418# endif
13419#endif
13420#ifdef FEAT_SYN_HL
13421 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013422 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#endif
13424#if defined(WIN3264)
13425 else if (STRICMP(name, "win95") == 0)
13426 n = mch_windows95();
13427#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013428#ifdef FEAT_NETBEANS_INTG
13429 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013430 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 }
13433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
13437/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013438 * "has_key()" function
13439 */
13440 static void
13441f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013442 typval_T *argvars;
13443 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013444{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013445 if (argvars[0].v_type != VAR_DICT)
13446 {
13447 EMSG(_(e_dictreq));
13448 return;
13449 }
13450 if (argvars[0].vval.v_dict == NULL)
13451 return;
13452
13453 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013454 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013455}
13456
13457/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013458 * "haslocaldir()" function
13459 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013460 static void
13461f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013462 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013463 typval_T *rettv;
13464{
13465 rettv->vval.v_number = (curwin->w_localdir != NULL);
13466}
13467
13468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 * "hasmapto()" function
13470 */
13471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
13476 char_u *name;
13477 char_u *mode;
13478 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013479 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013481 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 mode = (char_u *)"nvo";
13484 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013487 if (argvars[2].v_type != VAR_UNKNOWN)
13488 abbr = get_tv_number(&argvars[2]);
13489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490
Bram Moolenaar2c932302006-03-18 21:42:09 +000013491 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495}
13496
13497/*
13498 * "histadd()" function
13499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013502 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504{
13505#ifdef FEAT_CMDHIST
13506 int histype;
13507 char_u *str;
13508 char_u buf[NUMBUFLEN];
13509#endif
13510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 if (check_restricted() || check_secure())
13513 return;
13514#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13516 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 if (histype >= 0)
13518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 if (*str != NUL)
13521 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013522 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 return;
13526 }
13527 }
13528#endif
13529}
13530
13531/*
13532 * "histdel()" function
13533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013535f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013536 typval_T *argvars UNUSED;
13537 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
13539#ifdef FEAT_CMDHIST
13540 int n;
13541 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13545 if (str == NULL)
13546 n = 0;
13547 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013550 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 else
13555 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 get_tv_string_buf(&argvars[1], buf));
13558 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#endif
13560}
13561
13562/*
13563 * "histget()" function
13564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013567 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569{
13570#ifdef FEAT_CMDHIST
13571 int type;
13572 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13576 if (str == NULL)
13577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 {
13580 type = get_histtype(str);
13581 if (argvars[1].v_type == VAR_UNKNOWN)
13582 idx = get_history_idx(type);
13583 else
13584 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13585 /* -1 on type error */
13586 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592}
13593
13594/*
13595 * "histnr()" function
13596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013598f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601{
13602 int i;
13603
13604#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605 char_u *history = get_tv_string_chk(&argvars[0]);
13606
13607 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 if (i >= HIST_CMD && i < HIST_COUNT)
13609 i = get_history_idx(i);
13610 else
13611#endif
13612 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614}
13615
13616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617 * "highlightID(name)" function
13618 */
13619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013621 typval_T *argvars;
13622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
13627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013628 * "highlight_exists()" function
13629 */
13630 static void
13631f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634{
13635 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13636}
13637
13638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 * "hostname()" function
13640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
13646 char_u hostname[256];
13647
13648 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->v_type = VAR_STRING;
13650 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
13653/*
13654 * iconv() function
13655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013658 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
13661#ifdef FEAT_MBYTE
13662 char_u buf1[NUMBUFLEN];
13663 char_u buf2[NUMBUFLEN];
13664 char_u *from, *to, *str;
13665 vimconv_T vimconv;
13666#endif
13667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->v_type = VAR_STRING;
13669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
13671#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 str = get_tv_string(&argvars[0]);
13673 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13674 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 vimconv.vc_type = CONV_NONE;
13676 convert_setup(&vimconv, from, to);
13677
13678 /* If the encodings are equal, no conversion needed. */
13679 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683
13684 convert_setup(&vimconv, NULL, NULL);
13685 vim_free(from);
13686 vim_free(to);
13687#endif
13688}
13689
13690/*
13691 * "indent()" function
13692 */
13693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 typval_T *argvars;
13696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697{
13698 linenr_T lnum;
13699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013707/*
13708 * "index()" function
13709 */
13710 static void
13711f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *argvars;
13713 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013714{
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 list_T *l;
13716 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013717 long idx = 0;
13718 int ic = FALSE;
13719
13720 rettv->vval.v_number = -1;
13721 if (argvars[0].v_type != VAR_LIST)
13722 {
13723 EMSG(_(e_listreq));
13724 return;
13725 }
13726 l = argvars[0].vval.v_list;
13727 if (l != NULL)
13728 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013729 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013732 int error = FALSE;
13733
Bram Moolenaar758711c2005-02-02 23:11:38 +000013734 /* Start at specified item. Use the cached index that list_find()
13735 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013736 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013737 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013738 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 ic = get_tv_number_chk(&argvars[3], &error);
13740 if (error)
13741 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013742 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013743
Bram Moolenaar758711c2005-02-02 23:11:38 +000013744 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013745 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013746 {
13747 rettv->vval.v_number = idx;
13748 break;
13749 }
13750 }
13751}
13752
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753static int inputsecret_flag = 0;
13754
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013755static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13756
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013758 * This function is used by f_input() and f_inputdialog() functions. The third
13759 * argument to f_input() specifies the type of completion to use at the
13760 * prompt. The third argument to f_inputdialog() specifies the value to return
13761 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 */
13763 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013764get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 typval_T *argvars;
13766 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013767 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 char_u *p = NULL;
13771 int c;
13772 char_u buf[NUMBUFLEN];
13773 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013775 int xp_type = EXPAND_NOTHING;
13776 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013779 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780
13781#ifdef NO_CONSOLE_INPUT
13782 /* While starting up, there is no place to enter text. */
13783 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785#endif
13786
13787 cmd_silent = FALSE; /* Want to see the prompt. */
13788 if (prompt != NULL)
13789 {
13790 /* Only the part of the message after the last NL is considered as
13791 * prompt for the command line */
13792 p = vim_strrchr(prompt, '\n');
13793 if (p == NULL)
13794 p = prompt;
13795 else
13796 {
13797 ++p;
13798 c = *p;
13799 *p = NUL;
13800 msg_start();
13801 msg_clr_eos();
13802 msg_puts_attr(prompt, echo_attr);
13803 msg_didout = FALSE;
13804 msg_starthere();
13805 *p = c;
13806 }
13807 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013809 if (argvars[1].v_type != VAR_UNKNOWN)
13810 {
13811 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13812 if (defstr != NULL)
13813 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013815 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013816 {
13817 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013818 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013819 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013820
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013821 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013822 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013823
Bram Moolenaar4463f292005-09-25 22:20:24 +000013824 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13825 if (xp_name == NULL)
13826 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013828 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013829
Bram Moolenaar4463f292005-09-25 22:20:24 +000013830 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13831 &xp_arg) == FAIL)
13832 return;
13833 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013834 }
13835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013836 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013837 {
13838# ifdef FEAT_EX_EXTRA
13839 int save_ex_normal_busy = ex_normal_busy;
13840 ex_normal_busy = 0;
13841# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013842 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013843 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13844 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013845# ifdef FEAT_EX_EXTRA
13846 ex_normal_busy = save_ex_normal_busy;
13847# endif
13848 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013849 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013850 && argvars[1].v_type != VAR_UNKNOWN
13851 && argvars[2].v_type != VAR_UNKNOWN)
13852 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13853 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013854
13855 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 /* since the user typed this, no need to wait for return */
13858 need_wait_return = FALSE;
13859 msg_didout = FALSE;
13860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 cmd_silent = cmd_silent_save;
13862}
13863
13864/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013865 * "input()" function
13866 * Also handles inputsecret() when inputsecret is set.
13867 */
13868 static void
13869f_input(argvars, rettv)
13870 typval_T *argvars;
13871 typval_T *rettv;
13872{
13873 get_user_input(argvars, rettv, FALSE);
13874}
13875
13876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 * "inputdialog()" function
13878 */
13879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 typval_T *argvars;
13882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883{
13884#if defined(FEAT_GUI_TEXTDIALOG)
13885 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13886 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13887 {
13888 char_u *message;
13889 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 message = get_tv_string_chk(&argvars[0]);
13893 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013894 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013895 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 else
13897 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 if (message != NULL && defstr != NULL
13899 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013900 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 else
13903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 if (message != NULL && defstr != NULL
13905 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013906 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013907 rettv->vval.v_string = vim_strsave(
13908 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 }
13914 else
13915#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013916 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917}
13918
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013919/*
13920 * "inputlist()" function
13921 */
13922 static void
13923f_inputlist(argvars, rettv)
13924 typval_T *argvars;
13925 typval_T *rettv;
13926{
13927 listitem_T *li;
13928 int selected;
13929 int mouse_used;
13930
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013931#ifdef NO_CONSOLE_INPUT
13932 /* While starting up, there is no place to enter text. */
13933 if (no_console_input())
13934 return;
13935#endif
13936 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13937 {
13938 EMSG2(_(e_listarg), "inputlist()");
13939 return;
13940 }
13941
13942 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013943 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013944 lines_left = Rows; /* avoid more prompt */
13945 msg_scroll = TRUE;
13946 msg_clr_eos();
13947
13948 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13949 {
13950 msg_puts(get_tv_string(&li->li_tv));
13951 msg_putchar('\n');
13952 }
13953
13954 /* Ask for choice. */
13955 selected = prompt_for_number(&mouse_used);
13956 if (mouse_used)
13957 selected -= lines_left;
13958
13959 rettv->vval.v_number = selected;
13960}
13961
13962
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13964
13965/*
13966 * "inputrestore()" function
13967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972{
13973 if (ga_userinput.ga_len > 0)
13974 {
13975 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13977 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013978 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 }
13980 else if (p_verbose > 1)
13981 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013982 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 }
13985}
13986
13987/*
13988 * "inputsave()" function
13989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013992 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013995 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 if (ga_grow(&ga_userinput, 1) == OK)
13997 {
13998 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13999 + ga_userinput.ga_len);
14000 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014001 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 }
14003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005}
14006
14007/*
14008 * "inputsecret()" function
14009 */
14010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014012 typval_T *argvars;
14013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014{
14015 ++cmdline_star;
14016 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 --cmdline_star;
14019 --inputsecret_flag;
14020}
14021
14022/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014023 * "insert()" function
14024 */
14025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014027 typval_T *argvars;
14028 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014029{
14030 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 listitem_T *item;
14032 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014034
14035 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014036 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014037 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014038 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014039 {
14040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 before = get_tv_number_chk(&argvars[2], &error);
14042 if (error)
14043 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014044
Bram Moolenaar758711c2005-02-02 23:11:38 +000014045 if (before == l->lv_len)
14046 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 else
14048 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014049 item = list_find(l, before);
14050 if (item == NULL)
14051 {
14052 EMSGN(_(e_listidx), before);
14053 l = NULL;
14054 }
14055 }
14056 if (l != NULL)
14057 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014058 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014059 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014060 }
14061 }
14062}
14063
14064/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014065 * "invert(expr)" function
14066 */
14067 static void
14068f_invert(argvars, rettv)
14069 typval_T *argvars;
14070 typval_T *rettv;
14071{
14072 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14073}
14074
14075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 * "isdirectory()" function
14077 */
14078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014080 typval_T *argvars;
14081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084}
14085
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014086/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014087 * "islocked()" function
14088 */
14089 static void
14090f_islocked(argvars, rettv)
14091 typval_T *argvars;
14092 typval_T *rettv;
14093{
14094 lval_T lv;
14095 char_u *end;
14096 dictitem_T *di;
14097
14098 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014099 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14100 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014101 if (end != NULL && lv.ll_name != NULL)
14102 {
14103 if (*end != NUL)
14104 EMSG(_(e_trailing));
14105 else
14106 {
14107 if (lv.ll_tv == NULL)
14108 {
14109 if (check_changedtick(lv.ll_name))
14110 rettv->vval.v_number = 1; /* always locked */
14111 else
14112 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014113 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014114 if (di != NULL)
14115 {
14116 /* Consider a variable locked when:
14117 * 1. the variable itself is locked
14118 * 2. the value of the variable is locked.
14119 * 3. the List or Dict value is locked.
14120 */
14121 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14122 || tv_islocked(&di->di_tv));
14123 }
14124 }
14125 }
14126 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014127 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014128 else if (lv.ll_newkey != NULL)
14129 EMSG2(_(e_dictkey), lv.ll_newkey);
14130 else if (lv.ll_list != NULL)
14131 /* List item. */
14132 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14133 else
14134 /* Dictionary item. */
14135 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14136 }
14137 }
14138
14139 clear_lval(&lv);
14140}
14141
Bram Moolenaar33570922005-01-25 22:26:29 +000014142static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143
14144/*
14145 * Turn a dict into a list:
14146 * "what" == 0: list of keys
14147 * "what" == 1: list of values
14148 * "what" == 2: list of items
14149 */
14150 static void
14151dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *argvars;
14153 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014154 int what;
14155{
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 list_T *l2;
14157 dictitem_T *di;
14158 hashitem_T *hi;
14159 listitem_T *li;
14160 listitem_T *li2;
14161 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014162 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014163
Bram Moolenaar8c711452005-01-14 21:53:12 +000014164 if (argvars[0].v_type != VAR_DICT)
14165 {
14166 EMSG(_(e_dictreq));
14167 return;
14168 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014169 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014170 return;
14171
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014172 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014173 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014174
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014175 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014178 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014180 --todo;
14181 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014182
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014183 li = listitem_alloc();
14184 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014185 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014186 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014188 if (what == 0)
14189 {
14190 /* keys() */
14191 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014192 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014193 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14194 }
14195 else if (what == 1)
14196 {
14197 /* values() */
14198 copy_tv(&di->di_tv, &li->li_tv);
14199 }
14200 else
14201 {
14202 /* items() */
14203 l2 = list_alloc();
14204 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014205 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014206 li->li_tv.vval.v_list = l2;
14207 if (l2 == NULL)
14208 break;
14209 ++l2->lv_refcount;
14210
14211 li2 = listitem_alloc();
14212 if (li2 == NULL)
14213 break;
14214 list_append(l2, li2);
14215 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014216 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014217 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14218
14219 li2 = listitem_alloc();
14220 if (li2 == NULL)
14221 break;
14222 list_append(l2, li2);
14223 copy_tv(&di->di_tv, &li2->li_tv);
14224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014225 }
14226 }
14227}
14228
14229/*
14230 * "items(dict)" function
14231 */
14232 static void
14233f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014234 typval_T *argvars;
14235 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014236{
14237 dict_list(argvars, rettv, 2);
14238}
14239
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014241 * "join()" function
14242 */
14243 static void
14244f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014245 typval_T *argvars;
14246 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014247{
14248 garray_T ga;
14249 char_u *sep;
14250
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014251 if (argvars[0].v_type != VAR_LIST)
14252 {
14253 EMSG(_(e_listreq));
14254 return;
14255 }
14256 if (argvars[0].vval.v_list == NULL)
14257 return;
14258 if (argvars[1].v_type == VAR_UNKNOWN)
14259 sep = (char_u *)" ";
14260 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014262
14263 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264
14265 if (sep != NULL)
14266 {
14267 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014268 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 ga_append(&ga, NUL);
14270 rettv->vval.v_string = (char_u *)ga.ga_data;
14271 }
14272 else
14273 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014274}
14275
14276/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014277 * "keys()" function
14278 */
14279 static void
14280f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014281 typval_T *argvars;
14282 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283{
14284 dict_list(argvars, rettv, 0);
14285}
14286
14287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 * "last_buffer_nr()" function.
14289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294{
14295 int n = 0;
14296 buf_T *buf;
14297
14298 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14299 if (n < buf->b_fnum)
14300 n = buf->b_fnum;
14301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014303}
14304
14305/*
14306 * "len()" function
14307 */
14308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 typval_T *argvars;
14311 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014312{
14313 switch (argvars[0].v_type)
14314 {
14315 case VAR_STRING:
14316 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_number = (varnumber_T)STRLEN(
14318 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014319 break;
14320 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014322 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014323 case VAR_DICT:
14324 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14325 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014327 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014328 break;
14329 }
14330}
14331
Bram Moolenaar33570922005-01-25 22:26:29 +000014332static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014333
14334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014335libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014336 typval_T *argvars;
14337 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014338 int type;
14339{
14340#ifdef FEAT_LIBCALL
14341 char_u *string_in;
14342 char_u **string_result;
14343 int nr_result;
14344#endif
14345
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014346 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014347 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014349
14350 if (check_restricted() || check_secure())
14351 return;
14352
14353#ifdef FEAT_LIBCALL
14354 /* The first two args must be strings, otherwise its meaningless */
14355 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14356 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014357 string_in = NULL;
14358 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014359 string_in = argvars[2].vval.v_string;
14360 if (type == VAR_NUMBER)
14361 string_result = NULL;
14362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014364 if (mch_libcall(argvars[0].vval.v_string,
14365 argvars[1].vval.v_string,
14366 string_in,
14367 argvars[2].vval.v_number,
14368 string_result,
14369 &nr_result) == OK
14370 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014372 }
14373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377 * "libcall()" function
14378 */
14379 static void
14380f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014381 typval_T *argvars;
14382 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383{
14384 libcall_common(argvars, rettv, VAR_STRING);
14385}
14386
14387/*
14388 * "libcallnr()" function
14389 */
14390 static void
14391f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 typval_T *argvars;
14393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014394{
14395 libcall_common(argvars, rettv, VAR_NUMBER);
14396}
14397
14398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 * "line(string)" function
14400 */
14401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014402f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014403 typval_T *argvars;
14404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405{
14406 linenr_T lnum = 0;
14407 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014408 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014410 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 if (fp != NULL)
14412 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414}
14415
14416/*
14417 * "line2byte(lnum)" function
14418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423{
14424#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426#else
14427 linenr_T lnum;
14428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14434 if (rettv->vval.v_number >= 0)
14435 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436#endif
14437}
14438
14439/*
14440 * "lispindent(lnum)" function
14441 */
14442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014443f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014444 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
14447#ifdef FEAT_LISP
14448 pos_T pos;
14449 linenr_T lnum;
14450
14451 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14454 {
14455 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 curwin->w_cursor = pos;
14458 }
14459 else
14460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
14464/*
14465 * "localtime()" function
14466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014469 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473}
14474
Bram Moolenaar33570922005-01-25 22:26:29 +000014475static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476
14477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014479 typval_T *argvars;
14480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 int exact;
14482{
14483 char_u *keys;
14484 char_u *which;
14485 char_u buf[NUMBUFLEN];
14486 char_u *keys_buf = NULL;
14487 char_u *rhs;
14488 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014489 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014490 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014491 mapblock_T *mp;
14492 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493
14494 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014495 rettv->v_type = VAR_STRING;
14496 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 if (*keys == NUL)
14500 return;
14501
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014505 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014506 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014507 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014508 if (argvars[3].v_type != VAR_UNKNOWN)
14509 get_dict = get_tv_number(&argvars[3]);
14510 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 else
14513 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 if (which == NULL)
14515 return;
14516
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 mode = get_map_mode(&which, 0);
14518
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014519 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014520 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014522
14523 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014525 /* Return a string. */
14526 if (rhs != NULL)
14527 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528
Bram Moolenaarbd743252010-10-20 21:23:33 +020014529 }
14530 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14531 {
14532 /* Return a dictionary. */
14533 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14534 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14535 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536
Bram Moolenaarbd743252010-10-20 21:23:33 +020014537 dict_add_nr_str(dict, "lhs", 0L, lhs);
14538 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14539 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14540 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14541 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14542 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14543 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014544 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014545 dict_add_nr_str(dict, "mode", 0L, mapmode);
14546
14547 vim_free(lhs);
14548 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549 }
14550}
14551
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014552#ifdef FEAT_FLOAT
14553/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014554 * "log()" function
14555 */
14556 static void
14557f_log(argvars, rettv)
14558 typval_T *argvars;
14559 typval_T *rettv;
14560{
14561 float_T f;
14562
14563 rettv->v_type = VAR_FLOAT;
14564 if (get_float_arg(argvars, &f) == OK)
14565 rettv->vval.v_float = log(f);
14566 else
14567 rettv->vval.v_float = 0.0;
14568}
14569
14570/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014571 * "log10()" function
14572 */
14573 static void
14574f_log10(argvars, rettv)
14575 typval_T *argvars;
14576 typval_T *rettv;
14577{
14578 float_T f;
14579
14580 rettv->v_type = VAR_FLOAT;
14581 if (get_float_arg(argvars, &f) == OK)
14582 rettv->vval.v_float = log10(f);
14583 else
14584 rettv->vval.v_float = 0.0;
14585}
14586#endif
14587
Bram Moolenaar1dced572012-04-05 16:54:08 +020014588#ifdef FEAT_LUA
14589/*
14590 * "luaeval()" function
14591 */
14592 static void
14593f_luaeval(argvars, rettv)
14594 typval_T *argvars;
14595 typval_T *rettv;
14596{
14597 char_u *str;
14598 char_u buf[NUMBUFLEN];
14599
14600 str = get_tv_string_buf(&argvars[0], buf);
14601 do_luaeval(str, argvars + 1, rettv);
14602}
14603#endif
14604
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014606 * "map()" function
14607 */
14608 static void
14609f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014612{
14613 filter_map(argvars, rettv, TRUE);
14614}
14615
14616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014617 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 */
14619 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014620f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 typval_T *argvars;
14622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014624 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625}
14626
14627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 */
14630 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014631f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 typval_T *argvars;
14633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636}
14637
Bram Moolenaar33570922005-01-25 22:26:29 +000014638static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639
14640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 int type;
14645{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014646 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014647 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014648 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 char_u *pat;
14650 regmatch_T regmatch;
14651 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014652 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653 char_u *save_cpo;
14654 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014655 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014656 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014657 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 list_T *l = NULL;
14659 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014660 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014661 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662
14663 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14664 save_cpo = p_cpo;
14665 p_cpo = (char_u *)"";
14666
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014667 rettv->vval.v_number = -1;
14668 if (type == 3)
14669 {
14670 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014671 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014672 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673 }
14674 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->v_type = VAR_STRING;
14677 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014680 if (argvars[0].v_type == VAR_LIST)
14681 {
14682 if ((l = argvars[0].vval.v_list) == NULL)
14683 goto theend;
14684 li = l->lv_first;
14685 }
14686 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014687 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014688 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014689 len = (long)STRLEN(str);
14690 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14693 if (pat == NULL)
14694 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 int error = FALSE;
14699
14700 start = get_tv_number_chk(&argvars[2], &error);
14701 if (error)
14702 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014703 if (l != NULL)
14704 {
14705 li = list_find(l, start);
14706 if (li == NULL)
14707 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014708 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014709 }
14710 else
14711 {
14712 if (start < 0)
14713 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014714 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014715 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014716 /* When "count" argument is there ignore matches before "start",
14717 * otherwise skip part of the string. Differs when pattern is "^"
14718 * or "\<". */
14719 if (argvars[3].v_type != VAR_UNKNOWN)
14720 startcol = start;
14721 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014722 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014723 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014724 len -= start;
14725 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014726 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014728 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014729 nth = get_tv_number_chk(&argvars[3], &error);
14730 if (error)
14731 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 }
14733
14734 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14735 if (regmatch.regprog != NULL)
14736 {
14737 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014738
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014739 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014740 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014741 if (l != NULL)
14742 {
14743 if (li == NULL)
14744 {
14745 match = FALSE;
14746 break;
14747 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014748 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014749 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014750 if (str == NULL)
14751 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014752 }
14753
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014754 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014755
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014756 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014757 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014758 if (l == NULL && !match)
14759 break;
14760
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014761 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014762 if (l != NULL)
14763 {
14764 li = li->li_next;
14765 ++idx;
14766 }
14767 else
14768 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014769#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014770 startcol = (colnr_T)(regmatch.startp[0]
14771 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014772#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014773 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014774#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014775 if (startcol > (colnr_T)len
14776 || str + startcol <= regmatch.startp[0])
14777 {
14778 match = FALSE;
14779 break;
14780 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014781 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014782 }
14783
14784 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014788 int i;
14789
14790 /* return list with matched string and submatches */
14791 for (i = 0; i < NSUBEXP; ++i)
14792 {
14793 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014794 {
14795 if (list_append_string(rettv->vval.v_list,
14796 (char_u *)"", 0) == FAIL)
14797 break;
14798 }
14799 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014800 regmatch.startp[i],
14801 (int)(regmatch.endp[i] - regmatch.startp[i]))
14802 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014803 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014804 }
14805 }
14806 else if (type == 2)
14807 {
14808 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014809 if (l != NULL)
14810 copy_tv(&li->li_tv, rettv);
14811 else
14812 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014814 }
14815 else if (l != NULL)
14816 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 else
14818 {
14819 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014820 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 (varnumber_T)(regmatch.startp[0] - str);
14822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014825 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 }
14827 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014828 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 }
14830
14831theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 p_cpo = save_cpo;
14834}
14835
14836/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014837 * "match()" function
14838 */
14839 static void
14840f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014841 typval_T *argvars;
14842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843{
14844 find_some_match(argvars, rettv, 1);
14845}
14846
14847/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014848 * "matchadd()" function
14849 */
14850 static void
14851f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014852 typval_T *argvars UNUSED;
14853 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014854{
14855#ifdef FEAT_SEARCH_EXTRA
14856 char_u buf[NUMBUFLEN];
14857 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14858 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14859 int prio = 10; /* default priority */
14860 int id = -1;
14861 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014862 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014863
14864 rettv->vval.v_number = -1;
14865
14866 if (grp == NULL || pat == NULL)
14867 return;
14868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014869 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014870 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014871 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014872 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014873 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014874 if (argvars[4].v_type != VAR_UNKNOWN)
14875 {
14876 if (argvars[4].v_type != VAR_DICT)
14877 {
14878 EMSG(_(e_dictreq));
14879 return;
14880 }
14881 if (dict_find(argvars[4].vval.v_dict,
14882 (char_u *)"conceal", -1) != NULL)
14883 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14884 (char_u *)"conceal", FALSE);
14885 }
14886 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014887 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014888 if (error == TRUE)
14889 return;
14890 if (id >= 1 && id <= 3)
14891 {
14892 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14893 return;
14894 }
14895
Bram Moolenaar6561d522015-07-21 15:48:27 +020014896 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
14897 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020014898#endif
14899}
14900
14901/*
14902 * "matchaddpos()" function
14903 */
14904 static void
14905f_matchaddpos(argvars, rettv)
14906 typval_T *argvars UNUSED;
14907 typval_T *rettv UNUSED;
14908{
14909#ifdef FEAT_SEARCH_EXTRA
14910 char_u buf[NUMBUFLEN];
14911 char_u *group;
14912 int prio = 10;
14913 int id = -1;
14914 int error = FALSE;
14915 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020014916 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020014917
14918 rettv->vval.v_number = -1;
14919
14920 group = get_tv_string_buf_chk(&argvars[0], buf);
14921 if (group == NULL)
14922 return;
14923
14924 if (argvars[1].v_type != VAR_LIST)
14925 {
14926 EMSG2(_(e_listarg), "matchaddpos()");
14927 return;
14928 }
14929 l = argvars[1].vval.v_list;
14930 if (l == NULL)
14931 return;
14932
14933 if (argvars[2].v_type != VAR_UNKNOWN)
14934 {
14935 prio = get_tv_number_chk(&argvars[2], &error);
14936 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020014937 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020014938 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020014939 if (argvars[4].v_type != VAR_UNKNOWN)
14940 {
14941 if (argvars[4].v_type != VAR_DICT)
14942 {
14943 EMSG(_(e_dictreq));
14944 return;
14945 }
14946 if (dict_find(argvars[4].vval.v_dict,
14947 (char_u *)"conceal", -1) != NULL)
14948 conceal_char = get_dict_string(argvars[4].vval.v_dict,
14949 (char_u *)"conceal", FALSE);
14950 }
14951 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020014952 }
14953 if (error == TRUE)
14954 return;
14955
14956 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14957 if (id == 1 || id == 2)
14958 {
14959 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14960 return;
14961 }
14962
Bram Moolenaar6561d522015-07-21 15:48:27 +020014963 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
14964 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014965#endif
14966}
14967
14968/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014969 * "matcharg()" function
14970 */
14971 static void
14972f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014973 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014974 typval_T *rettv;
14975{
14976 if (rettv_list_alloc(rettv) == OK)
14977 {
14978#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014979 int id = get_tv_number(&argvars[0]);
14980 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014981
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014982 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014983 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014984 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14985 {
14986 list_append_string(rettv->vval.v_list,
14987 syn_id2name(m->hlg_id), -1);
14988 list_append_string(rettv->vval.v_list, m->pattern, -1);
14989 }
14990 else
14991 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014992 list_append_string(rettv->vval.v_list, NULL, -1);
14993 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014994 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014995 }
14996#endif
14997 }
14998}
14999
15000/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015001 * "matchdelete()" function
15002 */
15003 static void
15004f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015005 typval_T *argvars UNUSED;
15006 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015007{
15008#ifdef FEAT_SEARCH_EXTRA
15009 rettv->vval.v_number = match_delete(curwin,
15010 (int)get_tv_number(&argvars[0]), TRUE);
15011#endif
15012}
15013
15014/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 * "matchend()" function
15016 */
15017 static void
15018f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015019 typval_T *argvars;
15020 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021{
15022 find_some_match(argvars, rettv, 0);
15023}
15024
15025/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015026 * "matchlist()" function
15027 */
15028 static void
15029f_matchlist(argvars, rettv)
15030 typval_T *argvars;
15031 typval_T *rettv;
15032{
15033 find_some_match(argvars, rettv, 3);
15034}
15035
15036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 * "matchstr()" function
15038 */
15039 static void
15040f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 typval_T *argvars;
15042 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043{
15044 find_some_match(argvars, rettv, 2);
15045}
15046
Bram Moolenaar33570922005-01-25 22:26:29 +000015047static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015048
15049 static void
15050max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015053 int domax;
15054{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015055 long n = 0;
15056 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015057 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015058
15059 if (argvars[0].v_type == VAR_LIST)
15060 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015061 list_T *l;
15062 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015063
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015064 l = argvars[0].vval.v_list;
15065 if (l != NULL)
15066 {
15067 li = l->lv_first;
15068 if (li != NULL)
15069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015071 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015072 {
15073 li = li->li_next;
15074 if (li == NULL)
15075 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015077 if (domax ? i > n : i < n)
15078 n = i;
15079 }
15080 }
15081 }
15082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015083 else if (argvars[0].v_type == VAR_DICT)
15084 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015086 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015088 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015089
15090 d = argvars[0].vval.v_dict;
15091 if (d != NULL)
15092 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015093 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015096 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015098 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015100 if (first)
15101 {
15102 n = i;
15103 first = FALSE;
15104 }
15105 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015106 n = i;
15107 }
15108 }
15109 }
15110 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015111 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015112 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015113 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015114}
15115
15116/*
15117 * "max()" function
15118 */
15119 static void
15120f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015121 typval_T *argvars;
15122 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015123{
15124 max_min(argvars, rettv, TRUE);
15125}
15126
15127/*
15128 * "min()" function
15129 */
15130 static void
15131f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *argvars;
15133 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015134{
15135 max_min(argvars, rettv, FALSE);
15136}
15137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015138static int mkdir_recurse __ARGS((char_u *dir, int prot));
15139
15140/*
15141 * Create the directory in which "dir" is located, and higher levels when
15142 * needed.
15143 */
15144 static int
15145mkdir_recurse(dir, prot)
15146 char_u *dir;
15147 int prot;
15148{
15149 char_u *p;
15150 char_u *updir;
15151 int r = FAIL;
15152
15153 /* Get end of directory name in "dir".
15154 * We're done when it's "/" or "c:/". */
15155 p = gettail_sep(dir);
15156 if (p <= get_past_head(dir))
15157 return OK;
15158
15159 /* If the directory exists we're done. Otherwise: create it.*/
15160 updir = vim_strnsave(dir, (int)(p - dir));
15161 if (updir == NULL)
15162 return FAIL;
15163 if (mch_isdir(updir))
15164 r = OK;
15165 else if (mkdir_recurse(updir, prot) == OK)
15166 r = vim_mkdir_emsg(updir, prot);
15167 vim_free(updir);
15168 return r;
15169}
15170
15171#ifdef vim_mkdir
15172/*
15173 * "mkdir()" function
15174 */
15175 static void
15176f_mkdir(argvars, rettv)
15177 typval_T *argvars;
15178 typval_T *rettv;
15179{
15180 char_u *dir;
15181 char_u buf[NUMBUFLEN];
15182 int prot = 0755;
15183
15184 rettv->vval.v_number = FAIL;
15185 if (check_restricted() || check_secure())
15186 return;
15187
15188 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015189 if (*dir == NUL)
15190 rettv->vval.v_number = FAIL;
15191 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015192 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015193 if (*gettail(dir) == NUL)
15194 /* remove trailing slashes */
15195 *gettail_sep(dir) = NUL;
15196
15197 if (argvars[1].v_type != VAR_UNKNOWN)
15198 {
15199 if (argvars[2].v_type != VAR_UNKNOWN)
15200 prot = get_tv_number_chk(&argvars[2], NULL);
15201 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15202 mkdir_recurse(dir, prot);
15203 }
15204 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015205 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015206}
15207#endif
15208
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 * "mode()" function
15211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015213f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 typval_T *argvars;
15215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015217 char_u buf[3];
15218
15219 buf[1] = NUL;
15220 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 if (VIsual_active)
15223 {
15224 if (VIsual_select)
15225 buf[0] = VIsual_mode + 's' - 'v';
15226 else
15227 buf[0] = VIsual_mode;
15228 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015229 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015230 || State == CONFIRM)
15231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015233 if (State == ASKMORE)
15234 buf[1] = 'm';
15235 else if (State == CONFIRM)
15236 buf[1] = '?';
15237 }
15238 else if (State == EXTERNCMD)
15239 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 else if (State & INSERT)
15241 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015242#ifdef FEAT_VREPLACE
15243 if (State & VREPLACE_FLAG)
15244 {
15245 buf[0] = 'R';
15246 buf[1] = 'v';
15247 }
15248 else
15249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 if (State & REPLACE_FLAG)
15251 buf[0] = 'R';
15252 else
15253 buf[0] = 'i';
15254 }
15255 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015258 if (exmode_active)
15259 buf[1] = 'v';
15260 }
15261 else if (exmode_active)
15262 {
15263 buf[0] = 'c';
15264 buf[1] = 'e';
15265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015269 if (finish_op)
15270 buf[1] = 'o';
15271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015273 /* Clear out the minor mode when the argument is not a non-zero number or
15274 * non-empty string. */
15275 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015276 buf[1] = NUL;
15277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 rettv->vval.v_string = vim_strsave(buf);
15279 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280}
15281
Bram Moolenaar429fa852013-04-15 12:27:36 +020015282#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015283/*
15284 * "mzeval()" function
15285 */
15286 static void
15287f_mzeval(argvars, rettv)
15288 typval_T *argvars;
15289 typval_T *rettv;
15290{
15291 char_u *str;
15292 char_u buf[NUMBUFLEN];
15293
15294 str = get_tv_string_buf(&argvars[0], buf);
15295 do_mzeval(str, rettv);
15296}
Bram Moolenaar75676462013-01-30 14:55:42 +010015297
15298 void
15299mzscheme_call_vim(name, args, rettv)
15300 char_u *name;
15301 typval_T *args;
15302 typval_T *rettv;
15303{
15304 typval_T argvars[3];
15305
15306 argvars[0].v_type = VAR_STRING;
15307 argvars[0].vval.v_string = name;
15308 copy_tv(args, &argvars[1]);
15309 argvars[2].v_type = VAR_UNKNOWN;
15310 f_call(argvars, rettv);
15311 clear_tv(&argvars[1]);
15312}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015313#endif
15314
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316 * "nextnonblank()" function
15317 */
15318 static void
15319f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015320 typval_T *argvars;
15321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322{
15323 linenr_T lnum;
15324
15325 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 {
15329 lnum = 0;
15330 break;
15331 }
15332 if (*skipwhite(ml_get(lnum)) != NUL)
15333 break;
15334 }
15335 rettv->vval.v_number = lnum;
15336}
15337
15338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 * "nr2char()" function
15340 */
15341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015343 typval_T *argvars;
15344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345{
15346 char_u buf[NUMBUFLEN];
15347
15348#ifdef FEAT_MBYTE
15349 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015350 {
15351 int utf8 = 0;
15352
15353 if (argvars[1].v_type != VAR_UNKNOWN)
15354 utf8 = get_tv_number_chk(&argvars[1], NULL);
15355 if (utf8)
15356 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15357 else
15358 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 else
15361#endif
15362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 buf[1] = NUL;
15365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->v_type = VAR_STRING;
15367 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015368}
15369
15370/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015371 * "or(expr, expr)" function
15372 */
15373 static void
15374f_or(argvars, rettv)
15375 typval_T *argvars;
15376 typval_T *rettv;
15377{
15378 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15379 | get_tv_number_chk(&argvars[1], NULL);
15380}
15381
15382/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015383 * "pathshorten()" function
15384 */
15385 static void
15386f_pathshorten(argvars, rettv)
15387 typval_T *argvars;
15388 typval_T *rettv;
15389{
15390 char_u *p;
15391
15392 rettv->v_type = VAR_STRING;
15393 p = get_tv_string_chk(&argvars[0]);
15394 if (p == NULL)
15395 rettv->vval.v_string = NULL;
15396 else
15397 {
15398 p = vim_strsave(p);
15399 rettv->vval.v_string = p;
15400 if (p != NULL)
15401 shorten_dir(p);
15402 }
15403}
15404
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015405#ifdef FEAT_FLOAT
15406/*
15407 * "pow()" function
15408 */
15409 static void
15410f_pow(argvars, rettv)
15411 typval_T *argvars;
15412 typval_T *rettv;
15413{
15414 float_T fx, fy;
15415
15416 rettv->v_type = VAR_FLOAT;
15417 if (get_float_arg(argvars, &fx) == OK
15418 && get_float_arg(&argvars[1], &fy) == OK)
15419 rettv->vval.v_float = pow(fx, fy);
15420 else
15421 rettv->vval.v_float = 0.0;
15422}
15423#endif
15424
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015425/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426 * "prevnonblank()" function
15427 */
15428 static void
15429f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015430 typval_T *argvars;
15431 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015432{
15433 linenr_T lnum;
15434
15435 lnum = get_tv_lnum(argvars);
15436 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15437 lnum = 0;
15438 else
15439 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15440 --lnum;
15441 rettv->vval.v_number = lnum;
15442}
15443
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015444#ifdef HAVE_STDARG_H
15445/* This dummy va_list is here because:
15446 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15447 * - locally in the function results in a "used before set" warning
15448 * - using va_start() to initialize it gives "function with fixed args" error */
15449static va_list ap;
15450#endif
15451
Bram Moolenaar8c711452005-01-14 21:53:12 +000015452/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015453 * "printf()" function
15454 */
15455 static void
15456f_printf(argvars, rettv)
15457 typval_T *argvars;
15458 typval_T *rettv;
15459{
15460 rettv->v_type = VAR_STRING;
15461 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015462#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015463 {
15464 char_u buf[NUMBUFLEN];
15465 int len;
15466 char_u *s;
15467 int saved_did_emsg = did_emsg;
15468 char *fmt;
15469
15470 /* Get the required length, allocate the buffer and do it for real. */
15471 did_emsg = FALSE;
15472 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015473 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015474 if (!did_emsg)
15475 {
15476 s = alloc(len + 1);
15477 if (s != NULL)
15478 {
15479 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015480 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015481 }
15482 }
15483 did_emsg |= saved_did_emsg;
15484 }
15485#endif
15486}
15487
15488/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015489 * "pumvisible()" function
15490 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015491 static void
15492f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015493 typval_T *argvars UNUSED;
15494 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015495{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015496#ifdef FEAT_INS_EXPAND
15497 if (pum_visible())
15498 rettv->vval.v_number = 1;
15499#endif
15500}
15501
Bram Moolenaardb913952012-06-29 12:54:53 +020015502#ifdef FEAT_PYTHON3
15503/*
15504 * "py3eval()" function
15505 */
15506 static void
15507f_py3eval(argvars, rettv)
15508 typval_T *argvars;
15509 typval_T *rettv;
15510{
15511 char_u *str;
15512 char_u buf[NUMBUFLEN];
15513
15514 str = get_tv_string_buf(&argvars[0], buf);
15515 do_py3eval(str, rettv);
15516}
15517#endif
15518
15519#ifdef FEAT_PYTHON
15520/*
15521 * "pyeval()" function
15522 */
15523 static void
15524f_pyeval(argvars, rettv)
15525 typval_T *argvars;
15526 typval_T *rettv;
15527{
15528 char_u *str;
15529 char_u buf[NUMBUFLEN];
15530
15531 str = get_tv_string_buf(&argvars[0], buf);
15532 do_pyeval(str, rettv);
15533}
15534#endif
15535
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015537 * "range()" function
15538 */
15539 static void
15540f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015541 typval_T *argvars;
15542 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015543{
15544 long start;
15545 long end;
15546 long stride = 1;
15547 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015548 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015550 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015551 if (argvars[1].v_type == VAR_UNKNOWN)
15552 {
15553 end = start - 1;
15554 start = 0;
15555 }
15556 else
15557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015560 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015561 }
15562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015563 if (error)
15564 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015565 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015566 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015567 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015568 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015569 else
15570 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015571 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015572 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015573 if (list_append_number(rettv->vval.v_list,
15574 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015576 }
15577}
15578
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015579/*
15580 * "readfile()" function
15581 */
15582 static void
15583f_readfile(argvars, rettv)
15584 typval_T *argvars;
15585 typval_T *rettv;
15586{
15587 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015588 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015589 char_u *fname;
15590 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015591 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15592 int io_size = sizeof(buf);
15593 int readlen; /* size of last fread() */
15594 char_u *prev = NULL; /* previously read bytes, if any */
15595 long prevlen = 0; /* length of data in prev */
15596 long prevsize = 0; /* size of prev buffer */
15597 long maxline = MAXLNUM;
15598 long cnt = 0;
15599 char_u *p; /* position in buf */
15600 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015601
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015602 if (argvars[1].v_type != VAR_UNKNOWN)
15603 {
15604 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15605 binary = TRUE;
15606 if (argvars[2].v_type != VAR_UNKNOWN)
15607 maxline = get_tv_number(&argvars[2]);
15608 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015611 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015612
15613 /* Always open the file in binary mode, library functions have a mind of
15614 * their own about CR-LF conversion. */
15615 fname = get_tv_string(&argvars[0]);
15616 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15617 {
15618 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15619 return;
15620 }
15621
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015622 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015623 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015624 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015625
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015626 /* This for loop processes what was read, but is also entered at end
15627 * of file so that either:
15628 * - an incomplete line gets written
15629 * - a "binary" file gets an empty line at the end if it ends in a
15630 * newline. */
15631 for (p = buf, start = buf;
15632 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15633 ++p)
15634 {
15635 if (*p == '\n' || readlen <= 0)
15636 {
15637 listitem_T *li;
15638 char_u *s = NULL;
15639 long_u len = p - start;
15640
15641 /* Finished a line. Remove CRs before NL. */
15642 if (readlen > 0 && !binary)
15643 {
15644 while (len > 0 && start[len - 1] == '\r')
15645 --len;
15646 /* removal may cross back to the "prev" string */
15647 if (len == 0)
15648 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15649 --prevlen;
15650 }
15651 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015652 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015653 else
15654 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015655 /* Change "prev" buffer to be the right size. This way
15656 * the bytes are only copied once, and very long lines are
15657 * allocated only once. */
15658 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015660 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015661 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015662 prev = NULL; /* the list will own the string */
15663 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015664 }
15665 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015666 if (s == NULL)
15667 {
15668 do_outofmem_msg((long_u) prevlen + len + 1);
15669 failed = TRUE;
15670 break;
15671 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015672
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015673 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015674 {
15675 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015676 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015677 break;
15678 }
15679 li->li_tv.v_type = VAR_STRING;
15680 li->li_tv.v_lock = 0;
15681 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015682 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015683
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015684 start = p + 1; /* step over newline */
15685 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015686 break;
15687 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015688 else if (*p == NUL)
15689 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015690#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015691 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15692 * when finding the BF and check the previous two bytes. */
15693 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015694 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015695 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15696 * + 1, these may be in the "prev" string. */
15697 char_u back1 = p >= buf + 1 ? p[-1]
15698 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15699 char_u back2 = p >= buf + 2 ? p[-2]
15700 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15701 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015702
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015703 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015704 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015705 char_u *dest = p - 2;
15706
15707 /* Usually a BOM is at the beginning of a file, and so at
15708 * the beginning of a line; then we can just step over it.
15709 */
15710 if (start == dest)
15711 start = p + 1;
15712 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015713 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015714 /* have to shuffle buf to close gap */
15715 int adjust_prevlen = 0;
15716
15717 if (dest < buf)
15718 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015719 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015720 dest = buf;
15721 }
15722 if (readlen > p - buf + 1)
15723 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15724 readlen -= 3 - adjust_prevlen;
15725 prevlen -= adjust_prevlen;
15726 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015727 }
15728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015730#endif
15731 } /* for */
15732
15733 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15734 break;
15735 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015736 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015737 /* There's part of a line in buf, store it in "prev". */
15738 if (p - start + prevlen >= prevsize)
15739 {
15740 /* need bigger "prev" buffer */
15741 char_u *newprev;
15742
15743 /* A common use case is ordinary text files and "prev" gets a
15744 * fragment of a line, so the first allocation is made
15745 * small, to avoid repeatedly 'allocing' large and
15746 * 'reallocing' small. */
15747 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015748 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015749 else
15750 {
15751 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015752 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015753 prevsize = grow50pc > growmin ? grow50pc : growmin;
15754 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015755 newprev = prev == NULL ? alloc(prevsize)
15756 : vim_realloc(prev, prevsize);
15757 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015758 {
15759 do_outofmem_msg((long_u)prevsize);
15760 failed = TRUE;
15761 break;
15762 }
15763 prev = newprev;
15764 }
15765 /* Add the line part to end of "prev". */
15766 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015767 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015768 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015769 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015770
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015771 /*
15772 * For a negative line count use only the lines at the end of the file,
15773 * free the rest.
15774 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015775 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015776 while (cnt > -maxline)
15777 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015778 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015779 --cnt;
15780 }
15781
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015782 if (failed)
15783 {
15784 list_free(rettv->vval.v_list, TRUE);
15785 /* readfile doc says an empty list is returned on error */
15786 rettv->vval.v_list = list_alloc();
15787 }
15788
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015789 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015790 fclose(fd);
15791}
15792
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015793#if defined(FEAT_RELTIME)
15794static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15795
15796/*
15797 * Convert a List to proftime_T.
15798 * Return FAIL when there is something wrong.
15799 */
15800 static int
15801list2proftime(arg, tm)
15802 typval_T *arg;
15803 proftime_T *tm;
15804{
15805 long n1, n2;
15806 int error = FALSE;
15807
15808 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15809 || arg->vval.v_list->lv_len != 2)
15810 return FAIL;
15811 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15812 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15813# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015814 tm->HighPart = n1;
15815 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015816# else
15817 tm->tv_sec = n1;
15818 tm->tv_usec = n2;
15819# endif
15820 return error ? FAIL : OK;
15821}
15822#endif /* FEAT_RELTIME */
15823
15824/*
15825 * "reltime()" function
15826 */
15827 static void
15828f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015829 typval_T *argvars UNUSED;
15830 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015831{
15832#ifdef FEAT_RELTIME
15833 proftime_T res;
15834 proftime_T start;
15835
15836 if (argvars[0].v_type == VAR_UNKNOWN)
15837 {
15838 /* No arguments: get current time. */
15839 profile_start(&res);
15840 }
15841 else if (argvars[1].v_type == VAR_UNKNOWN)
15842 {
15843 if (list2proftime(&argvars[0], &res) == FAIL)
15844 return;
15845 profile_end(&res);
15846 }
15847 else
15848 {
15849 /* Two arguments: compute the difference. */
15850 if (list2proftime(&argvars[0], &start) == FAIL
15851 || list2proftime(&argvars[1], &res) == FAIL)
15852 return;
15853 profile_sub(&res, &start);
15854 }
15855
15856 if (rettv_list_alloc(rettv) == OK)
15857 {
15858 long n1, n2;
15859
15860# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015861 n1 = res.HighPart;
15862 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015863# else
15864 n1 = res.tv_sec;
15865 n2 = res.tv_usec;
15866# endif
15867 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15868 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15869 }
15870#endif
15871}
15872
15873/*
15874 * "reltimestr()" function
15875 */
15876 static void
15877f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015878 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015879 typval_T *rettv;
15880{
15881#ifdef FEAT_RELTIME
15882 proftime_T tm;
15883#endif
15884
15885 rettv->v_type = VAR_STRING;
15886 rettv->vval.v_string = NULL;
15887#ifdef FEAT_RELTIME
15888 if (list2proftime(&argvars[0], &tm) == OK)
15889 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15890#endif
15891}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015892
Bram Moolenaar0d660222005-01-07 21:51:51 +000015893#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15894static void make_connection __ARGS((void));
15895static int check_connection __ARGS((void));
15896
15897 static void
15898make_connection()
15899{
15900 if (X_DISPLAY == NULL
15901# ifdef FEAT_GUI
15902 && !gui.in_use
15903# endif
15904 )
15905 {
15906 x_force_connect = TRUE;
15907 setup_term_clip();
15908 x_force_connect = FALSE;
15909 }
15910}
15911
15912 static int
15913check_connection()
15914{
15915 make_connection();
15916 if (X_DISPLAY == NULL)
15917 {
15918 EMSG(_("E240: No connection to Vim server"));
15919 return FAIL;
15920 }
15921 return OK;
15922}
15923#endif
15924
15925#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015926static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015927
15928 static void
15929remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015930 typval_T *argvars;
15931 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932 int expr;
15933{
15934 char_u *server_name;
15935 char_u *keys;
15936 char_u *r = NULL;
15937 char_u buf[NUMBUFLEN];
15938# ifdef WIN32
15939 HWND w;
15940# else
15941 Window w;
15942# endif
15943
15944 if (check_restricted() || check_secure())
15945 return;
15946
15947# ifdef FEAT_X11
15948 if (check_connection() == FAIL)
15949 return;
15950# endif
15951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015952 server_name = get_tv_string_chk(&argvars[0]);
15953 if (server_name == NULL)
15954 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 keys = get_tv_string_buf(&argvars[1], buf);
15956# ifdef WIN32
15957 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15958# else
15959 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15960 < 0)
15961# endif
15962 {
15963 if (r != NULL)
15964 EMSG(r); /* sending worked but evaluation failed */
15965 else
15966 EMSG2(_("E241: Unable to send to %s"), server_name);
15967 return;
15968 }
15969
15970 rettv->vval.v_string = r;
15971
15972 if (argvars[2].v_type != VAR_UNKNOWN)
15973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015974 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015975 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015976 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015977
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015978 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 v.di_tv.v_type = VAR_STRING;
15980 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015981 idvar = get_tv_string_chk(&argvars[2]);
15982 if (idvar != NULL)
15983 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015984 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015985 }
15986}
15987#endif
15988
15989/*
15990 * "remote_expr()" function
15991 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015992 static void
15993f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015996{
15997 rettv->v_type = VAR_STRING;
15998 rettv->vval.v_string = NULL;
15999#ifdef FEAT_CLIENTSERVER
16000 remote_common(argvars, rettv, TRUE);
16001#endif
16002}
16003
16004/*
16005 * "remote_foreground()" function
16006 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016007 static void
16008f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016009 typval_T *argvars UNUSED;
16010 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016012#ifdef FEAT_CLIENTSERVER
16013# ifdef WIN32
16014 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016015 {
16016 char_u *server_name = get_tv_string_chk(&argvars[0]);
16017
16018 if (server_name != NULL)
16019 serverForeground(server_name);
16020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016021# else
16022 /* Send a foreground() expression to the server. */
16023 argvars[1].v_type = VAR_STRING;
16024 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16025 argvars[2].v_type = VAR_UNKNOWN;
16026 remote_common(argvars, rettv, TRUE);
16027 vim_free(argvars[1].vval.v_string);
16028# endif
16029#endif
16030}
16031
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032 static void
16033f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016036{
16037#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016038 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016039 char_u *s = NULL;
16040# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016041 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016042# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016044
16045 if (check_restricted() || check_secure())
16046 {
16047 rettv->vval.v_number = -1;
16048 return;
16049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 serverid = get_tv_string_chk(&argvars[0]);
16051 if (serverid == NULL)
16052 {
16053 rettv->vval.v_number = -1;
16054 return; /* type error; errmsg already given */
16055 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016056# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016057 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 if (n == 0)
16059 rettv->vval.v_number = -1;
16060 else
16061 {
16062 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16063 rettv->vval.v_number = (s != NULL);
16064 }
16065# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016066 if (check_connection() == FAIL)
16067 return;
16068
16069 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016070 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016071# endif
16072
16073 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 char_u *retvar;
16076
Bram Moolenaar33570922005-01-25 22:26:29 +000016077 v.di_tv.v_type = VAR_STRING;
16078 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 retvar = get_tv_string_chk(&argvars[1]);
16080 if (retvar != NULL)
16081 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 }
16084#else
16085 rettv->vval.v_number = -1;
16086#endif
16087}
16088
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089 static void
16090f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093{
16094 char_u *r = NULL;
16095
16096#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 char_u *serverid = get_tv_string_chk(&argvars[0]);
16098
16099 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016100 {
16101# ifdef WIN32
16102 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016103 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016104
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016105 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106 if (n != 0)
16107 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16108 if (r == NULL)
16109# else
16110 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016112# endif
16113 EMSG(_("E277: Unable to read a server reply"));
16114 }
16115#endif
16116 rettv->v_type = VAR_STRING;
16117 rettv->vval.v_string = r;
16118}
16119
16120/*
16121 * "remote_send()" function
16122 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123 static void
16124f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127{
16128 rettv->v_type = VAR_STRING;
16129 rettv->vval.v_string = NULL;
16130#ifdef FEAT_CLIENTSERVER
16131 remote_common(argvars, rettv, FALSE);
16132#endif
16133}
16134
16135/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016136 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 */
16138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 typval_T *argvars;
16141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016142{
Bram Moolenaar33570922005-01-25 22:26:29 +000016143 list_T *l;
16144 listitem_T *item, *item2;
16145 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016146 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016147 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016148 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016149 dict_T *d;
16150 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016151 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016152
Bram Moolenaar8c711452005-01-14 21:53:12 +000016153 if (argvars[0].v_type == VAR_DICT)
16154 {
16155 if (argvars[2].v_type != VAR_UNKNOWN)
16156 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016157 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016158 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016160 key = get_tv_string_chk(&argvars[1]);
16161 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016163 di = dict_find(d, key, -1);
16164 if (di == NULL)
16165 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016166 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16167 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016168 {
16169 *rettv = di->di_tv;
16170 init_tv(&di->di_tv);
16171 dictitem_remove(d, di);
16172 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016174 }
16175 }
16176 else if (argvars[0].v_type != VAR_LIST)
16177 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016178 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016179 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016181 int error = FALSE;
16182
16183 idx = get_tv_number_chk(&argvars[1], &error);
16184 if (error)
16185 ; /* type error: do nothing, errmsg already given */
16186 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016187 EMSGN(_(e_listidx), idx);
16188 else
16189 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016190 if (argvars[2].v_type == VAR_UNKNOWN)
16191 {
16192 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016193 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016194 *rettv = item->li_tv;
16195 vim_free(item);
16196 }
16197 else
16198 {
16199 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 end = get_tv_number_chk(&argvars[2], &error);
16201 if (error)
16202 ; /* type error: do nothing */
16203 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016204 EMSGN(_(e_listidx), end);
16205 else
16206 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016207 int cnt = 0;
16208
16209 for (li = item; li != NULL; li = li->li_next)
16210 {
16211 ++cnt;
16212 if (li == item2)
16213 break;
16214 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016215 if (li == NULL) /* didn't find "item2" after "item" */
16216 EMSG(_(e_invrange));
16217 else
16218 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016219 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016220 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016221 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016222 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016223 l->lv_first = item;
16224 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016225 item->li_prev = NULL;
16226 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016227 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016228 }
16229 }
16230 }
16231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016232 }
16233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234}
16235
16236/*
16237 * "rename({from}, {to})" function
16238 */
16239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 typval_T *argvars;
16242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243{
16244 char_u buf[NUMBUFLEN];
16245
16246 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016247 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016249 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16250 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251}
16252
16253/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016254 * "repeat()" function
16255 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016256 static void
16257f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 typval_T *argvars;
16259 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016260{
16261 char_u *p;
16262 int n;
16263 int slen;
16264 int len;
16265 char_u *r;
16266 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016267
16268 n = get_tv_number(&argvars[1]);
16269 if (argvars[0].v_type == VAR_LIST)
16270 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016271 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016272 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016273 if (list_extend(rettv->vval.v_list,
16274 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016275 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016276 }
16277 else
16278 {
16279 p = get_tv_string(&argvars[0]);
16280 rettv->v_type = VAR_STRING;
16281 rettv->vval.v_string = NULL;
16282
16283 slen = (int)STRLEN(p);
16284 len = slen * n;
16285 if (len <= 0)
16286 return;
16287
16288 r = alloc(len + 1);
16289 if (r != NULL)
16290 {
16291 for (i = 0; i < n; i++)
16292 mch_memmove(r + i * slen, p, (size_t)slen);
16293 r[len] = NUL;
16294 }
16295
16296 rettv->vval.v_string = r;
16297 }
16298}
16299
16300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 * "resolve()" function
16302 */
16303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 typval_T *argvars;
16306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307{
16308 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016309#ifdef HAVE_READLINK
16310 char_u *buf = NULL;
16311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016313 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314#ifdef FEAT_SHORTCUT
16315 {
16316 char_u *v = NULL;
16317
16318 v = mch_resolve_shortcut(p);
16319 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016322 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 }
16324#else
16325# ifdef HAVE_READLINK
16326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 char_u *cpy;
16328 int len;
16329 char_u *remain = NULL;
16330 char_u *q;
16331 int is_relative_to_current = FALSE;
16332 int has_trailing_pathsep = FALSE;
16333 int limit = 100;
16334
16335 p = vim_strsave(p);
16336
16337 if (p[0] == '.' && (vim_ispathsep(p[1])
16338 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16339 is_relative_to_current = TRUE;
16340
16341 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016342 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016345 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347
16348 q = getnextcomp(p);
16349 if (*q != NUL)
16350 {
16351 /* Separate the first path component in "p", and keep the
16352 * remainder (beginning with the path separator). */
16353 remain = vim_strsave(q - 1);
16354 q[-1] = NUL;
16355 }
16356
Bram Moolenaard9462e32011-04-11 21:35:11 +020016357 buf = alloc(MAXPATHL + 1);
16358 if (buf == NULL)
16359 goto fail;
16360
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 for (;;)
16362 {
16363 for (;;)
16364 {
16365 len = readlink((char *)p, (char *)buf, MAXPATHL);
16366 if (len <= 0)
16367 break;
16368 buf[len] = NUL;
16369
16370 if (limit-- == 0)
16371 {
16372 vim_free(p);
16373 vim_free(remain);
16374 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 goto fail;
16377 }
16378
16379 /* Ensure that the result will have a trailing path separator
16380 * if the argument has one. */
16381 if (remain == NULL && has_trailing_pathsep)
16382 add_pathsep(buf);
16383
16384 /* Separate the first path component in the link value and
16385 * concatenate the remainders. */
16386 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16387 if (*q != NUL)
16388 {
16389 if (remain == NULL)
16390 remain = vim_strsave(q - 1);
16391 else
16392 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016393 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 if (cpy != NULL)
16395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 vim_free(remain);
16397 remain = cpy;
16398 }
16399 }
16400 q[-1] = NUL;
16401 }
16402
16403 q = gettail(p);
16404 if (q > p && *q == NUL)
16405 {
16406 /* Ignore trailing path separator. */
16407 q[-1] = NUL;
16408 q = gettail(p);
16409 }
16410 if (q > p && !mch_isFullName(buf))
16411 {
16412 /* symlink is relative to directory of argument */
16413 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16414 if (cpy != NULL)
16415 {
16416 STRCPY(cpy, p);
16417 STRCPY(gettail(cpy), buf);
16418 vim_free(p);
16419 p = cpy;
16420 }
16421 }
16422 else
16423 {
16424 vim_free(p);
16425 p = vim_strsave(buf);
16426 }
16427 }
16428
16429 if (remain == NULL)
16430 break;
16431
16432 /* Append the first path component of "remain" to "p". */
16433 q = getnextcomp(remain + 1);
16434 len = q - remain - (*q != NUL);
16435 cpy = vim_strnsave(p, STRLEN(p) + len);
16436 if (cpy != NULL)
16437 {
16438 STRNCAT(cpy, remain, len);
16439 vim_free(p);
16440 p = cpy;
16441 }
16442 /* Shorten "remain". */
16443 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016444 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 else
16446 {
16447 vim_free(remain);
16448 remain = NULL;
16449 }
16450 }
16451
16452 /* If the result is a relative path name, make it explicitly relative to
16453 * the current directory if and only if the argument had this form. */
16454 if (!vim_ispathsep(*p))
16455 {
16456 if (is_relative_to_current
16457 && *p != NUL
16458 && !(p[0] == '.'
16459 && (p[1] == NUL
16460 || vim_ispathsep(p[1])
16461 || (p[1] == '.'
16462 && (p[2] == NUL
16463 || vim_ispathsep(p[2]))))))
16464 {
16465 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016466 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 if (cpy != NULL)
16468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 vim_free(p);
16470 p = cpy;
16471 }
16472 }
16473 else if (!is_relative_to_current)
16474 {
16475 /* Strip leading "./". */
16476 q = p;
16477 while (q[0] == '.' && vim_ispathsep(q[1]))
16478 q += 2;
16479 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016480 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 }
16482 }
16483
16484 /* Ensure that the result will have no trailing path separator
16485 * if the argument had none. But keep "/" or "//". */
16486 if (!has_trailing_pathsep)
16487 {
16488 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016489 if (after_pathsep(p, q))
16490 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 }
16492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016493 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 }
16495# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497# endif
16498#endif
16499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501
16502#ifdef HAVE_READLINK
16503fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016504 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507}
16508
16509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 */
16512 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016514 typval_T *argvars;
16515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516{
Bram Moolenaar33570922005-01-25 22:26:29 +000016517 list_T *l;
16518 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520 if (argvars[0].v_type != VAR_LIST)
16521 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016522 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016523 && !tv_check_lock(l->lv_lock,
16524 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 {
16526 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016527 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016528 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 while (li != NULL)
16530 {
16531 ni = li->li_prev;
16532 list_append(l, li);
16533 li = ni;
16534 }
16535 rettv->vval.v_list = l;
16536 rettv->v_type = VAR_LIST;
16537 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016538 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540}
16541
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016542#define SP_NOMOVE 0x01 /* don't move cursor */
16543#define SP_REPEAT 0x02 /* repeat to find outer pair */
16544#define SP_RETCOUNT 0x04 /* return matchcount */
16545#define SP_SETPCMARK 0x08 /* set previous context mark */
16546#define SP_START 0x10 /* accept match at start position */
16547#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16548#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016549#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016550
Bram Moolenaar33570922005-01-25 22:26:29 +000016551static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552
16553/*
16554 * Get flags for a search function.
16555 * Possibly sets "p_ws".
16556 * Returns BACKWARD, FORWARD or zero (for an error).
16557 */
16558 static int
16559get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016560 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561 int *flagsp;
16562{
16563 int dir = FORWARD;
16564 char_u *flags;
16565 char_u nbuf[NUMBUFLEN];
16566 int mask;
16567
16568 if (varp->v_type != VAR_UNKNOWN)
16569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 flags = get_tv_string_buf_chk(varp, nbuf);
16571 if (flags == NULL)
16572 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 while (*flags != NUL)
16574 {
16575 switch (*flags)
16576 {
16577 case 'b': dir = BACKWARD; break;
16578 case 'w': p_ws = TRUE; break;
16579 case 'W': p_ws = FALSE; break;
16580 default: mask = 0;
16581 if (flagsp != NULL)
16582 switch (*flags)
16583 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016584 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016585 case 'e': mask = SP_END; break;
16586 case 'm': mask = SP_RETCOUNT; break;
16587 case 'n': mask = SP_NOMOVE; break;
16588 case 'p': mask = SP_SUBPAT; break;
16589 case 'r': mask = SP_REPEAT; break;
16590 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016591 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 }
16593 if (mask == 0)
16594 {
16595 EMSG2(_(e_invarg2), flags);
16596 dir = 0;
16597 }
16598 else
16599 *flagsp |= mask;
16600 }
16601 if (dir == 0)
16602 break;
16603 ++flags;
16604 }
16605 }
16606 return dir;
16607}
16608
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016610 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016612 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016613search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016615 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016616 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016618 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 char_u *pat;
16620 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016621 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622 int save_p_ws = p_ws;
16623 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016624 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016625 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016626 proftime_T tm;
16627#ifdef FEAT_RELTIME
16628 long time_limit = 0;
16629#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016630 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016631 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016634 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016635 if (dir == 0)
16636 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016637 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016638 if (flags & SP_START)
16639 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016640 if (flags & SP_END)
16641 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016642 if (flags & SP_COLUMN)
16643 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016644
Bram Moolenaar76929292008-01-06 19:07:36 +000016645 /* Optional arguments: line number to stop searching and timeout. */
16646 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016647 {
16648 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16649 if (lnum_stop < 0)
16650 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016651#ifdef FEAT_RELTIME
16652 if (argvars[3].v_type != VAR_UNKNOWN)
16653 {
16654 time_limit = get_tv_number_chk(&argvars[3], NULL);
16655 if (time_limit < 0)
16656 goto theend;
16657 }
16658#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016659 }
16660
Bram Moolenaar76929292008-01-06 19:07:36 +000016661#ifdef FEAT_RELTIME
16662 /* Set the time limit, if there is one. */
16663 profile_setlimit(time_limit, &tm);
16664#endif
16665
Bram Moolenaar231334e2005-07-25 20:46:57 +000016666 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016667 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016668 * Check to make sure only those flags are set.
16669 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16670 * flags cannot be set. Check for that condition also.
16671 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016672 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016673 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016676 goto theend;
16677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016679 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016680 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016681 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016682 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016684 if (flags & SP_SUBPAT)
16685 retval = subpatnum;
16686 else
16687 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016688 if (flags & SP_SETPCMARK)
16689 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016691 if (match_pos != NULL)
16692 {
16693 /* Store the match cursor position */
16694 match_pos->lnum = pos.lnum;
16695 match_pos->col = pos.col + 1;
16696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 /* "/$" will put the cursor after the end of the line, may need to
16698 * correct that here */
16699 check_cursor();
16700 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016701
16702 /* If 'n' flag is used: restore cursor position. */
16703 if (flags & SP_NOMOVE)
16704 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016705 else
16706 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016707theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016709
16710 return retval;
16711}
16712
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016713#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016714
16715/*
16716 * round() is not in C90, use ceil() or floor() instead.
16717 */
16718 float_T
16719vim_round(f)
16720 float_T f;
16721{
16722 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16723}
16724
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016725/*
16726 * "round({float})" function
16727 */
16728 static void
16729f_round(argvars, rettv)
16730 typval_T *argvars;
16731 typval_T *rettv;
16732{
16733 float_T f;
16734
16735 rettv->v_type = VAR_FLOAT;
16736 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016737 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016738 else
16739 rettv->vval.v_float = 0.0;
16740}
16741#endif
16742
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016743/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016744 * "screenattr()" function
16745 */
16746 static void
16747f_screenattr(argvars, rettv)
16748 typval_T *argvars UNUSED;
16749 typval_T *rettv;
16750{
16751 int row;
16752 int col;
16753 int c;
16754
16755 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16756 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16757 if (row < 0 || row >= screen_Rows
16758 || col < 0 || col >= screen_Columns)
16759 c = -1;
16760 else
16761 c = ScreenAttrs[LineOffset[row] + col];
16762 rettv->vval.v_number = c;
16763}
16764
16765/*
16766 * "screenchar()" function
16767 */
16768 static void
16769f_screenchar(argvars, rettv)
16770 typval_T *argvars UNUSED;
16771 typval_T *rettv;
16772{
16773 int row;
16774 int col;
16775 int off;
16776 int c;
16777
16778 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16779 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16780 if (row < 0 || row >= screen_Rows
16781 || col < 0 || col >= screen_Columns)
16782 c = -1;
16783 else
16784 {
16785 off = LineOffset[row] + col;
16786#ifdef FEAT_MBYTE
16787 if (enc_utf8 && ScreenLinesUC[off] != 0)
16788 c = ScreenLinesUC[off];
16789 else
16790#endif
16791 c = ScreenLines[off];
16792 }
16793 rettv->vval.v_number = c;
16794}
16795
16796/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016797 * "screencol()" function
16798 *
16799 * First column is 1 to be consistent with virtcol().
16800 */
16801 static void
16802f_screencol(argvars, rettv)
16803 typval_T *argvars UNUSED;
16804 typval_T *rettv;
16805{
16806 rettv->vval.v_number = screen_screencol() + 1;
16807}
16808
16809/*
16810 * "screenrow()" function
16811 */
16812 static void
16813f_screenrow(argvars, rettv)
16814 typval_T *argvars UNUSED;
16815 typval_T *rettv;
16816{
16817 rettv->vval.v_number = screen_screenrow() + 1;
16818}
16819
16820/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016821 * "search()" function
16822 */
16823 static void
16824f_search(argvars, rettv)
16825 typval_T *argvars;
16826 typval_T *rettv;
16827{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016828 int flags = 0;
16829
16830 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831}
16832
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016834 * "searchdecl()" function
16835 */
16836 static void
16837f_searchdecl(argvars, rettv)
16838 typval_T *argvars;
16839 typval_T *rettv;
16840{
16841 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016842 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016843 int error = FALSE;
16844 char_u *name;
16845
16846 rettv->vval.v_number = 1; /* default: FAIL */
16847
16848 name = get_tv_string_chk(&argvars[0]);
16849 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016850 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016851 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016852 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16853 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16854 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016855 if (!error && name != NULL)
16856 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016857 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016858}
16859
16860/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016861 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016863 static int
16864searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016865 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016866 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867{
16868 char_u *spat, *mpat, *epat;
16869 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 int dir;
16872 int flags = 0;
16873 char_u nbuf1[NUMBUFLEN];
16874 char_u nbuf2[NUMBUFLEN];
16875 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016876 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016877 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016878 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 spat = get_tv_string_chk(&argvars[0]);
16882 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16883 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16884 if (spat == NULL || mpat == NULL || epat == NULL)
16885 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 /* Handle the optional fourth argument: flags */
16888 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016889 if (dir == 0)
16890 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016891
16892 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016893 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16894 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016895 if ((flags & (SP_END | SP_SUBPAT)) != 0
16896 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016897 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016898 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016899 goto theend;
16900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016902 /* Using 'r' implies 'W', otherwise it doesn't work. */
16903 if (flags & SP_REPEAT)
16904 p_ws = FALSE;
16905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016906 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016907 if (argvars[3].v_type == VAR_UNKNOWN
16908 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 skip = (char_u *)"";
16910 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016913 if (argvars[5].v_type != VAR_UNKNOWN)
16914 {
16915 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16916 if (lnum_stop < 0)
16917 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016918#ifdef FEAT_RELTIME
16919 if (argvars[6].v_type != VAR_UNKNOWN)
16920 {
16921 time_limit = get_tv_number_chk(&argvars[6], NULL);
16922 if (time_limit < 0)
16923 goto theend;
16924 }
16925#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016926 }
16927 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016928 if (skip == NULL)
16929 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016931 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016932 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016933
16934theend:
16935 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016936
16937 return retval;
16938}
16939
16940/*
16941 * "searchpair()" function
16942 */
16943 static void
16944f_searchpair(argvars, rettv)
16945 typval_T *argvars;
16946 typval_T *rettv;
16947{
16948 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16949}
16950
16951/*
16952 * "searchpairpos()" function
16953 */
16954 static void
16955f_searchpairpos(argvars, rettv)
16956 typval_T *argvars;
16957 typval_T *rettv;
16958{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016959 pos_T match_pos;
16960 int lnum = 0;
16961 int col = 0;
16962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016964 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016965
16966 if (searchpair_cmn(argvars, &match_pos) > 0)
16967 {
16968 lnum = match_pos.lnum;
16969 col = match_pos.col;
16970 }
16971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016972 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16973 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016974}
16975
16976/*
16977 * Search for a start/middle/end thing.
16978 * Used by searchpair(), see its documentation for the details.
16979 * Returns 0 or -1 for no match,
16980 */
16981 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016982do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16983 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016984 char_u *spat; /* start pattern */
16985 char_u *mpat; /* middle pattern */
16986 char_u *epat; /* end pattern */
16987 int dir; /* BACKWARD or FORWARD */
16988 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016989 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016990 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016991 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016992 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016993{
16994 char_u *save_cpo;
16995 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16996 long retval = 0;
16997 pos_T pos;
16998 pos_T firstpos;
16999 pos_T foundpos;
17000 pos_T save_cursor;
17001 pos_T save_pos;
17002 int n;
17003 int r;
17004 int nest = 1;
17005 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017006 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017007 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017008
17009 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17010 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017011 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017012
Bram Moolenaar76929292008-01-06 19:07:36 +000017013#ifdef FEAT_RELTIME
17014 /* Set the time limit, if there is one. */
17015 profile_setlimit(time_limit, &tm);
17016#endif
17017
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017018 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17019 * start/middle/end (pat3, for the top pair). */
17020 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17021 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17022 if (pat2 == NULL || pat3 == NULL)
17023 goto theend;
17024 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17025 if (*mpat == NUL)
17026 STRCPY(pat3, pat2);
17027 else
17028 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17029 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017030 if (flags & SP_START)
17031 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017032
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 save_cursor = curwin->w_cursor;
17034 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017035 clearpos(&firstpos);
17036 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 pat = pat3;
17038 for (;;)
17039 {
17040 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017041 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17043 /* didn't find it or found the first match again: FAIL */
17044 break;
17045
17046 if (firstpos.lnum == 0)
17047 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017048 if (equalpos(pos, foundpos))
17049 {
17050 /* Found the same position again. Can happen with a pattern that
17051 * has "\zs" at the end and searching backwards. Advance one
17052 * character and try again. */
17053 if (dir == BACKWARD)
17054 decl(&pos);
17055 else
17056 incl(&pos);
17057 }
17058 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017060 /* clear the start flag to avoid getting stuck here */
17061 options &= ~SEARCH_START;
17062
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 /* If the skip pattern matches, ignore this match. */
17064 if (*skip != NUL)
17065 {
17066 save_pos = curwin->w_cursor;
17067 curwin->w_cursor = pos;
17068 r = eval_to_bool(skip, &err, NULL, FALSE);
17069 curwin->w_cursor = save_pos;
17070 if (err)
17071 {
17072 /* Evaluating {skip} caused an error, break here. */
17073 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017074 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 break;
17076 }
17077 if (r)
17078 continue;
17079 }
17080
17081 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17082 {
17083 /* Found end when searching backwards or start when searching
17084 * forward: nested pair. */
17085 ++nest;
17086 pat = pat2; /* nested, don't search for middle */
17087 }
17088 else
17089 {
17090 /* Found end when searching forward or start when searching
17091 * backward: end of (nested) pair; or found middle in outer pair. */
17092 if (--nest == 1)
17093 pat = pat3; /* outer level, search for middle */
17094 }
17095
17096 if (nest == 0)
17097 {
17098 /* Found the match: return matchcount or line number. */
17099 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017100 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017102 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017103 if (flags & SP_SETPCMARK)
17104 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 curwin->w_cursor = pos;
17106 if (!(flags & SP_REPEAT))
17107 break;
17108 nest = 1; /* search for next unmatched */
17109 }
17110 }
17111
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017112 if (match_pos != NULL)
17113 {
17114 /* Store the match cursor position */
17115 match_pos->lnum = curwin->w_cursor.lnum;
17116 match_pos->col = curwin->w_cursor.col + 1;
17117 }
17118
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017120 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 curwin->w_cursor = save_cursor;
17122
17123theend:
17124 vim_free(pat2);
17125 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017126 if (p_cpo == empty_option)
17127 p_cpo = save_cpo;
17128 else
17129 /* Darn, evaluating the {skip} expression changed the value. */
17130 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017131
17132 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133}
17134
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017135/*
17136 * "searchpos()" function
17137 */
17138 static void
17139f_searchpos(argvars, rettv)
17140 typval_T *argvars;
17141 typval_T *rettv;
17142{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017143 pos_T match_pos;
17144 int lnum = 0;
17145 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017146 int n;
17147 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017148
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017149 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017150 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017151
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017152 n = search_cmn(argvars, &match_pos, &flags);
17153 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017154 {
17155 lnum = match_pos.lnum;
17156 col = match_pos.col;
17157 }
17158
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017159 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17160 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017161 if (flags & SP_SUBPAT)
17162 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017163}
17164
17165
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 static void
17167f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171#ifdef FEAT_CLIENTSERVER
17172 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 char_u *server = get_tv_string_chk(&argvars[0]);
17174 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 if (server == NULL || reply == NULL)
17178 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 if (check_restricted() || check_secure())
17180 return;
17181# ifdef FEAT_X11
17182 if (check_connection() == FAIL)
17183 return;
17184# endif
17185
17186 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 EMSG(_("E258: Unable to send to client"));
17189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 rettv->vval.v_number = 0;
17192#else
17193 rettv->vval.v_number = -1;
17194#endif
17195}
17196
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 static void
17198f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201{
17202 char_u *r = NULL;
17203
17204#ifdef FEAT_CLIENTSERVER
17205# ifdef WIN32
17206 r = serverGetVimNames();
17207# else
17208 make_connection();
17209 if (X_DISPLAY != NULL)
17210 r = serverGetVimNames(X_DISPLAY);
17211# endif
17212#endif
17213 rettv->v_type = VAR_STRING;
17214 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
17218 * "setbufvar()" function
17219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017221f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017222 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017223 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224{
17225 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 char_u nbuf[NUMBUFLEN];
17230
17231 if (check_restricted() || check_secure())
17232 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17234 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017235 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 varp = &argvars[2];
17237
17238 if (buf != NULL && varname != NULL && varp != NULL)
17239 {
17240 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242
17243 if (*varname == '&')
17244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 long numval;
17246 char_u *strval;
17247 int error = FALSE;
17248
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017250 numval = get_tv_number_chk(varp, &error);
17251 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 if (!error && strval != NULL)
17253 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 }
17255 else
17256 {
17257 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17258 if (bufvarname != NULL)
17259 {
17260 STRCPY(bufvarname, "b:");
17261 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017262 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 vim_free(bufvarname);
17264 }
17265 }
17266
17267 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270}
17271
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017272 static void
17273f_setcharsearch(argvars, rettv)
17274 typval_T *argvars;
17275 typval_T *rettv UNUSED;
17276{
17277 dict_T *d;
17278 dictitem_T *di;
17279 char_u *csearch;
17280
17281 if (argvars[0].v_type != VAR_DICT)
17282 {
17283 EMSG(_(e_dictreq));
17284 return;
17285 }
17286
17287 if ((d = argvars[0].vval.v_dict) != NULL)
17288 {
17289 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17290 if (csearch != NULL)
17291 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017292#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017293 if (enc_utf8)
17294 {
17295 int pcc[MAX_MCO];
17296 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017297
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017298 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17299 }
17300 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017301#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017302 set_last_csearch(PTR2CHAR(csearch),
17303 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017304 }
17305
17306 di = dict_find(d, (char_u *)"forward", -1);
17307 if (di != NULL)
17308 set_csearch_direction(get_tv_number(&di->di_tv)
17309 ? FORWARD : BACKWARD);
17310
17311 di = dict_find(d, (char_u *)"until", -1);
17312 if (di != NULL)
17313 set_csearch_until(!!get_tv_number(&di->di_tv));
17314 }
17315}
17316
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317/*
17318 * "setcmdpos()" function
17319 */
17320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 typval_T *argvars;
17323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017325 int pos = (int)get_tv_number(&argvars[0]) - 1;
17326
17327 if (pos >= 0)
17328 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329}
17330
17331/*
17332 * "setline()" function
17333 */
17334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017336 typval_T *argvars;
17337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338{
17339 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017340 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017341 list_T *l = NULL;
17342 listitem_T *li = NULL;
17343 long added = 0;
17344 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017346 lnum = get_tv_lnum(&argvars[0]);
17347 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017349 l = argvars[1].vval.v_list;
17350 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017352 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017354
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017355 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017356 for (;;)
17357 {
17358 if (l != NULL)
17359 {
17360 /* list argument, get next string */
17361 if (li == NULL)
17362 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017364 li = li->li_next;
17365 }
17366
17367 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017369 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017370
17371 /* When coming here from Insert mode, sync undo, so that this can be
17372 * undone separately from what was previously inserted. */
17373 if (u_sync_once == 2)
17374 {
17375 u_sync_once = 1; /* notify that u_sync() was called */
17376 u_sync(TRUE);
17377 }
17378
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017379 if (lnum <= curbuf->b_ml.ml_line_count)
17380 {
17381 /* existing line, replace it */
17382 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17383 {
17384 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017385 if (lnum == curwin->w_cursor.lnum)
17386 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017387 rettv->vval.v_number = 0; /* OK */
17388 }
17389 }
17390 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17391 {
17392 /* lnum is one past the last line, append the line */
17393 ++added;
17394 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17395 rettv->vval.v_number = 0; /* OK */
17396 }
17397
17398 if (l == NULL) /* only one string argument */
17399 break;
17400 ++lnum;
17401 }
17402
17403 if (added > 0)
17404 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000017407static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
17408
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017410 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017411 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017412 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017413set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017414 win_T *wp UNUSED;
17415 typval_T *list_arg UNUSED;
17416 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017417 typval_T *rettv;
17418{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017419#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017420 char_u *act;
17421 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017422#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017423
Bram Moolenaar2641f772005-03-25 21:58:17 +000017424 rettv->vval.v_number = -1;
17425
17426#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017427 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017428 EMSG(_(e_listreq));
17429 else
17430 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017431 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017432
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017433 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017434 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017435 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017436 if (act == NULL)
17437 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017438 if (*act == 'a' || *act == 'r')
17439 action = *act;
17440 }
17441
Bram Moolenaar81484f42012-12-05 15:16:47 +010017442 if (l != NULL && set_errorlist(wp, l, action,
17443 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017444 rettv->vval.v_number = 0;
17445 }
17446#endif
17447}
17448
17449/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017450 * "setloclist()" function
17451 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017452 static void
17453f_setloclist(argvars, rettv)
17454 typval_T *argvars;
17455 typval_T *rettv;
17456{
17457 win_T *win;
17458
17459 rettv->vval.v_number = -1;
17460
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017461 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017462 if (win != NULL)
17463 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17464}
17465
17466/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017467 * "setmatches()" function
17468 */
17469 static void
17470f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017471 typval_T *argvars UNUSED;
17472 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017473{
17474#ifdef FEAT_SEARCH_EXTRA
17475 list_T *l;
17476 listitem_T *li;
17477 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017478 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017479
17480 rettv->vval.v_number = -1;
17481 if (argvars[0].v_type != VAR_LIST)
17482 {
17483 EMSG(_(e_listreq));
17484 return;
17485 }
17486 if ((l = argvars[0].vval.v_list) != NULL)
17487 {
17488
17489 /* To some extent make sure that we are dealing with a list from
17490 * "getmatches()". */
17491 li = l->lv_first;
17492 while (li != NULL)
17493 {
17494 if (li->li_tv.v_type != VAR_DICT
17495 || (d = li->li_tv.vval.v_dict) == NULL)
17496 {
17497 EMSG(_(e_invarg));
17498 return;
17499 }
17500 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017501 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17502 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017503 && dict_find(d, (char_u *)"priority", -1) != NULL
17504 && dict_find(d, (char_u *)"id", -1) != NULL))
17505 {
17506 EMSG(_(e_invarg));
17507 return;
17508 }
17509 li = li->li_next;
17510 }
17511
17512 clear_matches(curwin);
17513 li = l->lv_first;
17514 while (li != NULL)
17515 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017516 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017517 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017518 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017519 char_u *group;
17520 int priority;
17521 int id;
17522 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017523
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017524 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017525 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17526 {
17527 if (s == NULL)
17528 {
17529 s = list_alloc();
17530 if (s == NULL)
17531 return;
17532 }
17533
17534 /* match from matchaddpos() */
17535 for (i = 1; i < 9; i++)
17536 {
17537 sprintf((char *)buf, (char *)"pos%d", i);
17538 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17539 {
17540 if (di->di_tv.v_type != VAR_LIST)
17541 return;
17542
17543 list_append_tv(s, &di->di_tv);
17544 s->lv_refcount++;
17545 }
17546 else
17547 break;
17548 }
17549 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017550
17551 group = get_dict_string(d, (char_u *)"group", FALSE);
17552 priority = (int)get_dict_number(d, (char_u *)"priority");
17553 id = (int)get_dict_number(d, (char_u *)"id");
17554 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17555 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17556 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017557 if (i == 0)
17558 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017559 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017560 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017561 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017562 }
17563 else
17564 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017565 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017566 list_unref(s);
17567 s = NULL;
17568 }
17569
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017570 li = li->li_next;
17571 }
17572 rettv->vval.v_number = 0;
17573 }
17574#endif
17575}
17576
17577/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017578 * "setpos()" function
17579 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017580 static void
17581f_setpos(argvars, rettv)
17582 typval_T *argvars;
17583 typval_T *rettv;
17584{
17585 pos_T pos;
17586 int fnum;
17587 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017588 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017589
Bram Moolenaar08250432008-02-13 11:42:46 +000017590 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017591 name = get_tv_string_chk(argvars);
17592 if (name != NULL)
17593 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017594 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017595 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017596 if (--pos.col < 0)
17597 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017598 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017599 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017600 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017601 if (fnum == curbuf->b_fnum)
17602 {
17603 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017604 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017605 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017606 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017607 curwin->w_set_curswant = FALSE;
17608 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017609 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017610 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017611 }
17612 else
17613 EMSG(_(e_invarg));
17614 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017615 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17616 {
17617 /* set mark */
17618 if (setmark_pos(name[1], &pos, fnum) == OK)
17619 rettv->vval.v_number = 0;
17620 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017621 else
17622 EMSG(_(e_invarg));
17623 }
17624 }
17625}
17626
17627/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017628 * "setqflist()" function
17629 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017630 static void
17631f_setqflist(argvars, rettv)
17632 typval_T *argvars;
17633 typval_T *rettv;
17634{
17635 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17636}
17637
17638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 * "setreg()" function
17640 */
17641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *argvars;
17644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645{
17646 int regname;
17647 char_u *strregname;
17648 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017649 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 int append;
17651 char_u yank_type;
17652 long block_len;
17653
17654 block_len = -1;
17655 yank_type = MAUTO;
17656 append = FALSE;
17657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017658 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017661 if (strregname == NULL)
17662 return; /* type error; errmsg already given */
17663 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 if (regname == 0 || regname == '@')
17665 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017667 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017669 stropt = get_tv_string_chk(&argvars[2]);
17670 if (stropt == NULL)
17671 return; /* type error */
17672 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 switch (*stropt)
17674 {
17675 case 'a': case 'A': /* append */
17676 append = TRUE;
17677 break;
17678 case 'v': case 'c': /* character-wise selection */
17679 yank_type = MCHAR;
17680 break;
17681 case 'V': case 'l': /* line-wise selection */
17682 yank_type = MLINE;
17683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 case 'b': case Ctrl_V: /* block-wise selection */
17685 yank_type = MBLOCK;
17686 if (VIM_ISDIGIT(stropt[1]))
17687 {
17688 ++stropt;
17689 block_len = getdigits(&stropt) - 1;
17690 --stropt;
17691 }
17692 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 }
17694 }
17695
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017696 if (argvars[1].v_type == VAR_LIST)
17697 {
17698 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017699 char_u **allocval;
17700 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017701 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017702 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017703 int len = argvars[1].vval.v_list->lv_len;
17704 listitem_T *li;
17705
Bram Moolenaar7d647822014-04-05 21:28:56 +020017706 /* First half: use for pointers to result lines; second half: use for
17707 * pointers to allocated copies. */
17708 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017709 if (lstval == NULL)
17710 return;
17711 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017712 allocval = lstval + len + 2;
17713 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017714
17715 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17716 li = li->li_next)
17717 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017718 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017719 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017720 goto free_lstval;
17721 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017722 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017723 /* Need to make a copy, next get_tv_string_buf_chk() will
17724 * overwrite the string. */
17725 strval = vim_strsave(buf);
17726 if (strval == NULL)
17727 goto free_lstval;
17728 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017729 }
17730 *curval++ = strval;
17731 }
17732 *curval++ = NULL;
17733
17734 write_reg_contents_lst(regname, lstval, -1,
17735 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017736free_lstval:
17737 while (curallocval > allocval)
17738 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017739 vim_free(lstval);
17740 }
17741 else
17742 {
17743 strval = get_tv_string_chk(&argvars[1]);
17744 if (strval == NULL)
17745 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750}
17751
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017752/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017753 * "settabvar()" function
17754 */
17755 static void
17756f_settabvar(argvars, rettv)
17757 typval_T *argvars;
17758 typval_T *rettv;
17759{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017760#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017761 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017762 tabpage_T *tp;
17763#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017764 char_u *varname, *tabvarname;
17765 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017766
17767 rettv->vval.v_number = 0;
17768
17769 if (check_restricted() || check_secure())
17770 return;
17771
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017772#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017774#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017775 varname = get_tv_string_chk(&argvars[1]);
17776 varp = &argvars[2];
17777
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017778 if (varname != NULL && varp != NULL
17779#ifdef FEAT_WINDOWS
17780 && tp != NULL
17781#endif
17782 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017783 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017784#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017785 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017786 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017787#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017788
17789 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17790 if (tabvarname != NULL)
17791 {
17792 STRCPY(tabvarname, "t:");
17793 STRCPY(tabvarname + 2, varname);
17794 set_var(tabvarname, varp, TRUE);
17795 vim_free(tabvarname);
17796 }
17797
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017798#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017799 /* Restore current tabpage */
17800 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017801 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017802#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017803 }
17804}
17805
17806/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017807 * "settabwinvar()" function
17808 */
17809 static void
17810f_settabwinvar(argvars, rettv)
17811 typval_T *argvars;
17812 typval_T *rettv;
17813{
17814 setwinvar(argvars, rettv, 1);
17815}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816
17817/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017818 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017821f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 typval_T *argvars;
17823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017825 setwinvar(argvars, rettv, 0);
17826}
17827
17828/*
17829 * "setwinvar()" and "settabwinvar()" functions
17830 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017832 static void
17833setwinvar(argvars, rettv, off)
17834 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017835 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017836 int off;
17837{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 win_T *win;
17839#ifdef FEAT_WINDOWS
17840 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017841 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017842 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843#endif
17844 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017845 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017847 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848
17849 if (check_restricted() || check_secure())
17850 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017851
17852#ifdef FEAT_WINDOWS
17853 if (off == 1)
17854 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17855 else
17856 tp = curtab;
17857#endif
17858 win = find_win_by_nr(&argvars[off], tp);
17859 varname = get_tv_string_chk(&argvars[off + 1]);
17860 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861
17862 if (win != NULL && varname != NULL && varp != NULL)
17863 {
17864#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017865 need_switch_win = !(tp == curtab && win == curwin);
17866 if (!need_switch_win
17867 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017870 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017872 long numval;
17873 char_u *strval;
17874 int error = FALSE;
17875
17876 ++varname;
17877 numval = get_tv_number_chk(varp, &error);
17878 strval = get_tv_string_buf_chk(varp, nbuf);
17879 if (!error && strval != NULL)
17880 set_option_value(varname, numval, strval, OPT_LOCAL);
17881 }
17882 else
17883 {
17884 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17885 if (winvarname != NULL)
17886 {
17887 STRCPY(winvarname, "w:");
17888 STRCPY(winvarname + 2, varname);
17889 set_var(winvarname, varp, TRUE);
17890 vim_free(winvarname);
17891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 }
17893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020017895 if (need_switch_win)
17896 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897#endif
17898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899}
17900
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017901#ifdef FEAT_CRYPT
17902/*
17903 * "sha256({string})" function
17904 */
17905 static void
17906f_sha256(argvars, rettv)
17907 typval_T *argvars;
17908 typval_T *rettv;
17909{
17910 char_u *p;
17911
17912 p = get_tv_string(&argvars[0]);
17913 rettv->vval.v_string = vim_strsave(
17914 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17915 rettv->v_type = VAR_STRING;
17916}
17917#endif /* FEAT_CRYPT */
17918
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017920 * "shellescape({string})" function
17921 */
17922 static void
17923f_shellescape(argvars, rettv)
17924 typval_T *argvars;
17925 typval_T *rettv;
17926{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017927 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017928 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017929 rettv->v_type = VAR_STRING;
17930}
17931
17932/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017933 * shiftwidth() function
17934 */
17935 static void
17936f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017937 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017938 typval_T *rettv;
17939{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017940 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017941}
17942
17943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017944 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 */
17946 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017947f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017948 typval_T *argvars;
17949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017951 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 p = get_tv_string(&argvars[0]);
17954 rettv->vval.v_string = vim_strsave(p);
17955 simplify_filename(rettv->vval.v_string); /* simplify in place */
17956 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957}
17958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017959#ifdef FEAT_FLOAT
17960/*
17961 * "sin()" function
17962 */
17963 static void
17964f_sin(argvars, rettv)
17965 typval_T *argvars;
17966 typval_T *rettv;
17967{
17968 float_T f;
17969
17970 rettv->v_type = VAR_FLOAT;
17971 if (get_float_arg(argvars, &f) == OK)
17972 rettv->vval.v_float = sin(f);
17973 else
17974 rettv->vval.v_float = 0.0;
17975}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017976
17977/*
17978 * "sinh()" function
17979 */
17980 static void
17981f_sinh(argvars, rettv)
17982 typval_T *argvars;
17983 typval_T *rettv;
17984{
17985 float_T f;
17986
17987 rettv->v_type = VAR_FLOAT;
17988 if (get_float_arg(argvars, &f) == OK)
17989 rettv->vval.v_float = sinh(f);
17990 else
17991 rettv->vval.v_float = 0.0;
17992}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017993#endif
17994
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995static int
17996#ifdef __BORLANDC__
17997 _RTLENTRYF
17998#endif
17999 item_compare __ARGS((const void *s1, const void *s2));
18000static int
18001#ifdef __BORLANDC__
18002 _RTLENTRYF
18003#endif
18004 item_compare2 __ARGS((const void *s1, const void *s2));
18005
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018006/* struct used in the array that's given to qsort() */
18007typedef struct
18008{
18009 listitem_T *item;
18010 int idx;
18011} sortItem_T;
18012
Bram Moolenaar0d660222005-01-07 21:51:51 +000018013static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018014static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018015static int item_compare_numbers;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018017static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018018static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018019static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018020static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018021#define ITEM_COMPARE_FAIL 999
18022
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018024 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018026 static int
18027#ifdef __BORLANDC__
18028_RTLENTRYF
18029#endif
18030item_compare(s1, s2)
18031 const void *s1;
18032 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018034 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018035 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018037 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018038 int res;
18039 char_u numbuf1[NUMBUFLEN];
18040 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018042 si1 = (sortItem_T *)s1;
18043 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018044 tv1 = &si1->item->li_tv;
18045 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018046
18047 if (item_compare_numbers)
18048 {
18049 long v1 = get_tv_number(tv1);
18050 long v2 = get_tv_number(tv2);
18051
18052 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18053 }
18054
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018055 /* tv2string() puts quotes around a string and allocates memory. Don't do
18056 * that for string variables. Use a single quote when comparing with a
18057 * non-string to do what the docs promise. */
18058 if (tv1->v_type == VAR_STRING)
18059 {
18060 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18061 p1 = (char_u *)"'";
18062 else
18063 p1 = tv1->vval.v_string;
18064 }
18065 else
18066 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18067 if (tv2->v_type == VAR_STRING)
18068 {
18069 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18070 p2 = (char_u *)"'";
18071 else
18072 p2 = tv2->vval.v_string;
18073 }
18074 else
18075 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018076 if (p1 == NULL)
18077 p1 = (char_u *)"";
18078 if (p2 == NULL)
18079 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018080 if (!item_compare_numeric)
18081 {
18082 if (item_compare_ic)
18083 res = STRICMP(p1, p2);
18084 else
18085 res = STRCMP(p1, p2);
18086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018088 {
18089 double n1, n2;
18090 n1 = strtod((char *)p1, (char **)&p1);
18091 n2 = strtod((char *)p2, (char **)&p2);
18092 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18093 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018094
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018095 /* When the result would be zero, compare the item indexes. Makes the
18096 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018097 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018098 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018099
Bram Moolenaar0d660222005-01-07 21:51:51 +000018100 vim_free(tofree1);
18101 vim_free(tofree2);
18102 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103}
18104
18105 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018106#ifdef __BORLANDC__
18107_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018109item_compare2(s1, s2)
18110 const void *s1;
18111 const void *s2;
18112{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018113 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018115 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018116 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018119 /* shortcut after failure in previous call; compare all items equal */
18120 if (item_compare_func_err)
18121 return 0;
18122
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018123 si1 = (sortItem_T *)s1;
18124 si2 = (sortItem_T *)s2;
18125
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018126 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018127 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018128 copy_tv(&si1->item->li_tv, &argv[0]);
18129 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018130
18131 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018132 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018133 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18134 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018135 clear_tv(&argv[0]);
18136 clear_tv(&argv[1]);
18137
18138 if (res == FAIL)
18139 res = ITEM_COMPARE_FAIL;
18140 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018141 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18142 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018143 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018144 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018145
18146 /* When the result would be zero, compare the pointers themselves. Makes
18147 * the sort stable. */
18148 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018149 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018150
Bram Moolenaar0d660222005-01-07 21:51:51 +000018151 return res;
18152}
18153
18154/*
18155 * "sort({list})" function
18156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018158do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018159 typval_T *argvars;
18160 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018161 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162{
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 list_T *l;
18164 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018165 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166 long len;
18167 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168
Bram Moolenaar0d660222005-01-07 21:51:51 +000018169 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018170 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 else
18172 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018174 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018175 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18176 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018177 return;
18178 rettv->vval.v_list = l;
18179 rettv->v_type = VAR_LIST;
18180 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182 len = list_len(l);
18183 if (len <= 1)
18184 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018187 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018188 item_compare_numbers = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018189 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018190 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 if (argvars[1].v_type != VAR_UNKNOWN)
18192 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018193 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018194 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018195 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018196 else
18197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018198 int error = FALSE;
18199
18200 i = get_tv_number_chk(&argvars[1], &error);
18201 if (error)
18202 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 if (i == 1)
18204 item_compare_ic = TRUE;
18205 else
18206 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018207 if (item_compare_func != NULL)
18208 {
18209 if (STRCMP(item_compare_func, "n") == 0)
18210 {
18211 item_compare_func = NULL;
18212 item_compare_numeric = TRUE;
18213 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018214 else if (STRCMP(item_compare_func, "N") == 0)
18215 {
18216 item_compare_func = NULL;
18217 item_compare_numbers = TRUE;
18218 }
Bram Moolenaare8a34922014-06-25 17:31:09 +020018219 else if (STRCMP(item_compare_func, "i") == 0)
18220 {
18221 item_compare_func = NULL;
18222 item_compare_ic = TRUE;
18223 }
18224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018225 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018226
18227 if (argvars[2].v_type != VAR_UNKNOWN)
18228 {
18229 /* optional third argument: {dict} */
18230 if (argvars[2].v_type != VAR_DICT)
18231 {
18232 EMSG(_(e_dictreq));
18233 return;
18234 }
18235 item_compare_selfdict = argvars[2].vval.v_dict;
18236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238
Bram Moolenaar0d660222005-01-07 21:51:51 +000018239 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018240 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018241 if (ptrs == NULL)
18242 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
Bram Moolenaar327aa022014-03-25 18:24:23 +010018244 i = 0;
18245 if (sort)
18246 {
18247 /* sort(): ptrs will be the list to sort */
18248 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018249 {
18250 ptrs[i].item = li;
18251 ptrs[i].idx = i;
18252 ++i;
18253 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018254
18255 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018256 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018257 /* test the compare function */
18258 if (item_compare_func != NULL
18259 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018260 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018261 EMSG(_("E702: Sort compare function failed"));
18262 else
18263 {
18264 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018265 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018266 item_compare_func == NULL ? item_compare : item_compare2);
18267
18268 if (!item_compare_func_err)
18269 {
18270 /* Clear the List and append the items in sorted order. */
18271 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18272 l->lv_len = 0;
18273 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018275 }
18276 }
18277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018280 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
18281
18282 /* f_uniq(): ptrs will be a stack of items to remove */
18283 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018284 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018285 item_compare_func_ptr = item_compare_func
18286 ? item_compare2 : item_compare;
18287
18288 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18289 li = li->li_next)
18290 {
18291 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18292 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018293 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018294 if (item_compare_func_err)
18295 {
18296 EMSG(_("E882: Uniq compare function failed"));
18297 break;
18298 }
18299 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 if (!item_compare_func_err)
18302 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018303 while (--i >= 0)
18304 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018305 li = ptrs[i].item->li_next;
18306 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018307 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018308 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018309 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018310 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018311 list_fix_watch(l, li);
18312 listitem_free(li);
18313 l->lv_len--;
18314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 }
18317
18318 vim_free(ptrs);
18319 }
18320}
18321
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018322/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018323 * "sort({list})" function
18324 */
18325 static void
18326f_sort(argvars, rettv)
18327 typval_T *argvars;
18328 typval_T *rettv;
18329{
18330 do_sort_uniq(argvars, rettv, TRUE);
18331}
18332
18333/*
18334 * "uniq({list})" function
18335 */
18336 static void
18337f_uniq(argvars, rettv)
18338 typval_T *argvars;
18339 typval_T *rettv;
18340{
18341 do_sort_uniq(argvars, rettv, FALSE);
18342}
18343
18344/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018345 * "soundfold({word})" function
18346 */
18347 static void
18348f_soundfold(argvars, rettv)
18349 typval_T *argvars;
18350 typval_T *rettv;
18351{
18352 char_u *s;
18353
18354 rettv->v_type = VAR_STRING;
18355 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018356#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018357 rettv->vval.v_string = eval_soundfold(s);
18358#else
18359 rettv->vval.v_string = vim_strsave(s);
18360#endif
18361}
18362
18363/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018364 * "spellbadword()" function
18365 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018366 static void
18367f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018368 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018369 typval_T *rettv;
18370{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018371 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018372 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018373 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018374
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018375 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018376 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018377
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018378#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018379 if (argvars[0].v_type == VAR_UNKNOWN)
18380 {
18381 /* Find the start and length of the badly spelled word. */
18382 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18383 if (len != 0)
18384 word = ml_get_cursor();
18385 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018386 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018387 {
18388 char_u *str = get_tv_string_chk(&argvars[0]);
18389 int capcol = -1;
18390
18391 if (str != NULL)
18392 {
18393 /* Check the argument for spelling. */
18394 while (*str != NUL)
18395 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018396 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018397 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018398 {
18399 word = str;
18400 break;
18401 }
18402 str += len;
18403 }
18404 }
18405 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018406#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018407
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018408 list_append_string(rettv->vval.v_list, word, len);
18409 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018410 attr == HLF_SPB ? "bad" :
18411 attr == HLF_SPR ? "rare" :
18412 attr == HLF_SPL ? "local" :
18413 attr == HLF_SPC ? "caps" :
18414 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018415}
18416
18417/*
18418 * "spellsuggest()" function
18419 */
18420 static void
18421f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018422 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018423 typval_T *rettv;
18424{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018425#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018426 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018427 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018428 int maxcount;
18429 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018430 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018431 listitem_T *li;
18432 int need_capital = FALSE;
18433#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018435 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018436 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018437
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018438#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018439 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018440 {
18441 str = get_tv_string(&argvars[0]);
18442 if (argvars[1].v_type != VAR_UNKNOWN)
18443 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018444 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018445 if (maxcount <= 0)
18446 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018447 if (argvars[2].v_type != VAR_UNKNOWN)
18448 {
18449 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18450 if (typeerr)
18451 return;
18452 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018453 }
18454 else
18455 maxcount = 25;
18456
Bram Moolenaar4770d092006-01-12 23:22:24 +000018457 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018458
18459 for (i = 0; i < ga.ga_len; ++i)
18460 {
18461 str = ((char_u **)ga.ga_data)[i];
18462
18463 li = listitem_alloc();
18464 if (li == NULL)
18465 vim_free(str);
18466 else
18467 {
18468 li->li_tv.v_type = VAR_STRING;
18469 li->li_tv.v_lock = 0;
18470 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018471 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018472 }
18473 }
18474 ga_clear(&ga);
18475 }
18476#endif
18477}
18478
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018480f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 typval_T *argvars;
18482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483{
18484 char_u *str;
18485 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018486 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487 regmatch_T regmatch;
18488 char_u patbuf[NUMBUFLEN];
18489 char_u *save_cpo;
18490 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018491 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018492 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018493 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494
18495 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18496 save_cpo = p_cpo;
18497 p_cpo = (char_u *)"";
18498
18499 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018500 if (argvars[1].v_type != VAR_UNKNOWN)
18501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18503 if (pat == NULL)
18504 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018505 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018507 }
18508 if (pat == NULL || *pat == NUL)
18509 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018510
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018511 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018513 if (typeerr)
18514 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515
Bram Moolenaar0d660222005-01-07 21:51:51 +000018516 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18517 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018519 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018520 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018521 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018522 if (*str == NUL)
18523 match = FALSE; /* empty item at the end */
18524 else
18525 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018526 if (match)
18527 end = regmatch.startp[0];
18528 else
18529 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018530 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18531 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018532 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018533 if (list_append_string(rettv->vval.v_list, str,
18534 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018535 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536 }
18537 if (!match)
18538 break;
18539 /* Advance to just after the match. */
18540 if (regmatch.endp[0] > str)
18541 col = 0;
18542 else
18543 {
18544 /* Don't get stuck at the same match. */
18545#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018546 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547#else
18548 col = 1;
18549#endif
18550 }
18551 str = regmatch.endp[0];
18552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553
Bram Moolenaar473de612013-06-08 18:19:48 +020018554 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558}
18559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018560#ifdef FEAT_FLOAT
18561/*
18562 * "sqrt()" function
18563 */
18564 static void
18565f_sqrt(argvars, rettv)
18566 typval_T *argvars;
18567 typval_T *rettv;
18568{
18569 float_T f;
18570
18571 rettv->v_type = VAR_FLOAT;
18572 if (get_float_arg(argvars, &f) == OK)
18573 rettv->vval.v_float = sqrt(f);
18574 else
18575 rettv->vval.v_float = 0.0;
18576}
18577
18578/*
18579 * "str2float()" function
18580 */
18581 static void
18582f_str2float(argvars, rettv)
18583 typval_T *argvars;
18584 typval_T *rettv;
18585{
18586 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18587
18588 if (*p == '+')
18589 p = skipwhite(p + 1);
18590 (void)string2float(p, &rettv->vval.v_float);
18591 rettv->v_type = VAR_FLOAT;
18592}
18593#endif
18594
Bram Moolenaar2c932302006-03-18 21:42:09 +000018595/*
18596 * "str2nr()" function
18597 */
18598 static void
18599f_str2nr(argvars, rettv)
18600 typval_T *argvars;
18601 typval_T *rettv;
18602{
18603 int base = 10;
18604 char_u *p;
18605 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018606 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018607
18608 if (argvars[1].v_type != VAR_UNKNOWN)
18609 {
18610 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018611 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018612 {
18613 EMSG(_(e_invarg));
18614 return;
18615 }
18616 }
18617
18618 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018619 if (*p == '+')
18620 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018621 switch (base)
18622 {
18623 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18624 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18625 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18626 default: what = 0;
18627 }
18628 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018629 rettv->vval.v_number = n;
18630}
18631
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632#ifdef HAVE_STRFTIME
18633/*
18634 * "strftime({format}[, {time}])" function
18635 */
18636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018637f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018638 typval_T *argvars;
18639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640{
18641 char_u result_buf[256];
18642 struct tm *curtime;
18643 time_t seconds;
18644 char_u *p;
18645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018649 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 seconds = time(NULL);
18651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 curtime = localtime(&seconds);
18654 /* MSVC returns NULL for an invalid value of seconds. */
18655 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 else
18658 {
18659# ifdef FEAT_MBYTE
18660 vimconv_T conv;
18661 char_u *enc;
18662
18663 conv.vc_type = CONV_NONE;
18664 enc = enc_locale();
18665 convert_setup(&conv, p_enc, enc);
18666 if (conv.vc_type != CONV_NONE)
18667 p = string_convert(&conv, p, NULL);
18668# endif
18669 if (p != NULL)
18670 (void)strftime((char *)result_buf, sizeof(result_buf),
18671 (char *)p, curtime);
18672 else
18673 result_buf[0] = NUL;
18674
18675# ifdef FEAT_MBYTE
18676 if (conv.vc_type != CONV_NONE)
18677 vim_free(p);
18678 convert_setup(&conv, enc, p_enc);
18679 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 else
18682# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018683 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684
18685# ifdef FEAT_MBYTE
18686 /* Release conversion descriptors */
18687 convert_setup(&conv, NULL, NULL);
18688 vim_free(enc);
18689# endif
18690 }
18691}
18692#endif
18693
18694/*
18695 * "stridx()" function
18696 */
18697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018699 typval_T *argvars;
18700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701{
18702 char_u buf[NUMBUFLEN];
18703 char_u *needle;
18704 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018705 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018709 needle = get_tv_string_chk(&argvars[1]);
18710 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018711 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018712 if (needle == NULL || haystack == NULL)
18713 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714
Bram Moolenaar33570922005-01-25 22:26:29 +000018715 if (argvars[2].v_type != VAR_UNKNOWN)
18716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018717 int error = FALSE;
18718
18719 start_idx = get_tv_number_chk(&argvars[2], &error);
18720 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018721 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018722 if (start_idx >= 0)
18723 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 }
18725
18726 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18727 if (pos != NULL)
18728 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729}
18730
18731/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018732 * "string()" function
18733 */
18734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 typval_T *argvars;
18737 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018738{
18739 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018740 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018743 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018744 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018745 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018746 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747}
18748
18749/*
18750 * "strlen()" function
18751 */
18752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018753f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 typval_T *argvars;
18755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757 rettv->vval.v_number = (varnumber_T)(STRLEN(
18758 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759}
18760
18761/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018762 * "strchars()" function
18763 */
18764 static void
18765f_strchars(argvars, rettv)
18766 typval_T *argvars;
18767 typval_T *rettv;
18768{
18769 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018770 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018771#ifdef FEAT_MBYTE
18772 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018773 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018774#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018775
18776 if (argvars[1].v_type != VAR_UNKNOWN)
18777 skipcc = get_tv_number_chk(&argvars[1], NULL);
18778 if (skipcc < 0 || skipcc > 1)
18779 EMSG(_(e_invarg));
18780 else
18781 {
18782#ifdef FEAT_MBYTE
18783 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18784 while (*s != NUL)
18785 {
18786 func_mb_ptr2char_adv(&s);
18787 ++len;
18788 }
18789 rettv->vval.v_number = len;
18790#else
18791 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18792#endif
18793 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018794}
18795
18796/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018797 * "strdisplaywidth()" function
18798 */
18799 static void
18800f_strdisplaywidth(argvars, rettv)
18801 typval_T *argvars;
18802 typval_T *rettv;
18803{
18804 char_u *s = get_tv_string(&argvars[0]);
18805 int col = 0;
18806
18807 if (argvars[1].v_type != VAR_UNKNOWN)
18808 col = get_tv_number(&argvars[1]);
18809
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018810 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018811}
18812
18813/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018814 * "strwidth()" function
18815 */
18816 static void
18817f_strwidth(argvars, rettv)
18818 typval_T *argvars;
18819 typval_T *rettv;
18820{
18821 char_u *s = get_tv_string(&argvars[0]);
18822
18823 rettv->vval.v_number = (varnumber_T)(
18824#ifdef FEAT_MBYTE
18825 mb_string2cells(s, -1)
18826#else
18827 STRLEN(s)
18828#endif
18829 );
18830}
18831
18832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 * "strpart()" function
18834 */
18835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018837 typval_T *argvars;
18838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839{
18840 char_u *p;
18841 int n;
18842 int len;
18843 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018844 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 slen = (int)STRLEN(p);
18848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018849 n = get_tv_number_chk(&argvars[1], &error);
18850 if (error)
18851 len = 0;
18852 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018853 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 else
18855 len = slen - n; /* default len: all bytes that are available. */
18856
18857 /*
18858 * Only return the overlap between the specified part and the actual
18859 * string.
18860 */
18861 if (n < 0)
18862 {
18863 len += n;
18864 n = 0;
18865 }
18866 else if (n > slen)
18867 n = slen;
18868 if (len < 0)
18869 len = 0;
18870 else if (n + len > slen)
18871 len = slen - n;
18872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv->v_type = VAR_STRING;
18874 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875}
18876
18877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018878 * "strridx()" function
18879 */
18880 static void
18881f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018882 typval_T *argvars;
18883 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884{
18885 char_u buf[NUMBUFLEN];
18886 char_u *needle;
18887 char_u *haystack;
18888 char_u *rest;
18889 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018890 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018892 needle = get_tv_string_chk(&argvars[1]);
18893 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018894
18895 rettv->vval.v_number = -1;
18896 if (needle == NULL || haystack == NULL)
18897 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018898
18899 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018900 if (argvars[2].v_type != VAR_UNKNOWN)
18901 {
18902 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018903 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018904 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018905 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018906 }
18907 else
18908 end_idx = haystack_len;
18909
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018911 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018912 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018913 lastmatch = haystack + end_idx;
18914 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018916 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917 for (rest = haystack; *rest != '\0'; ++rest)
18918 {
18919 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018920 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 break;
18922 lastmatch = rest;
18923 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018924 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925
18926 if (lastmatch == NULL)
18927 rettv->vval.v_number = -1;
18928 else
18929 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18930}
18931
18932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 * "strtrans()" function
18934 */
18935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018936f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018937 typval_T *argvars;
18938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940 rettv->v_type = VAR_STRING;
18941 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018945 * "submatch()" function
18946 */
18947 static void
18948f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018949 typval_T *argvars;
18950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951{
Bram Moolenaar41571762014-04-02 19:00:58 +020018952 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018953 int no;
18954 int retList = 0;
18955
18956 no = (int)get_tv_number_chk(&argvars[0], &error);
18957 if (error)
18958 return;
18959 error = FALSE;
18960 if (argvars[1].v_type != VAR_UNKNOWN)
18961 retList = get_tv_number_chk(&argvars[1], &error);
18962 if (error)
18963 return;
18964
18965 if (retList == 0)
18966 {
18967 rettv->v_type = VAR_STRING;
18968 rettv->vval.v_string = reg_submatch(no);
18969 }
18970 else
18971 {
18972 rettv->v_type = VAR_LIST;
18973 rettv->vval.v_list = reg_submatch_list(no);
18974 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975}
18976
18977/*
18978 * "substitute()" function
18979 */
18980 static void
18981f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 typval_T *argvars;
18983 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018984{
18985 char_u patbuf[NUMBUFLEN];
18986 char_u subbuf[NUMBUFLEN];
18987 char_u flagsbuf[NUMBUFLEN];
18988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 char_u *str = get_tv_string_chk(&argvars[0]);
18990 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18991 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18992 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18993
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018995 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18996 rettv->vval.v_string = NULL;
18997 else
18998 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999}
19000
19001/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019002 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019005f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019006 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008{
19009 int id = 0;
19010#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019011 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 long col;
19013 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019014 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019016 lnum = get_tv_lnum(argvars); /* -1 on type error */
19017 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19018 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019020 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019021 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019022 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023#endif
19024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019025 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026}
19027
19028/*
19029 * "synIDattr(id, what [, mode])" function
19030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019032f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035{
19036 char_u *p = NULL;
19037#ifdef FEAT_SYN_HL
19038 int id;
19039 char_u *what;
19040 char_u *mode;
19041 char_u modebuf[NUMBUFLEN];
19042 int modec;
19043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044 id = get_tv_number(&argvars[0]);
19045 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019048 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019050 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 modec = 0; /* replace invalid with current */
19052 }
19053 else
19054 {
19055#ifdef FEAT_GUI
19056 if (gui.in_use)
19057 modec = 'g';
19058 else
19059#endif
19060 if (t_colors > 1)
19061 modec = 'c';
19062 else
19063 modec = 't';
19064 }
19065
19066
19067 switch (TOLOWER_ASC(what[0]))
19068 {
19069 case 'b':
19070 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19071 p = highlight_color(id, what, modec);
19072 else /* bold */
19073 p = highlight_has_attr(id, HL_BOLD, modec);
19074 break;
19075
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019076 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 p = highlight_color(id, what, modec);
19078 break;
19079
19080 case 'i':
19081 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19082 p = highlight_has_attr(id, HL_INVERSE, modec);
19083 else /* italic */
19084 p = highlight_has_attr(id, HL_ITALIC, modec);
19085 break;
19086
19087 case 'n': /* name */
19088 p = get_highlight_name(NULL, id - 1);
19089 break;
19090
19091 case 'r': /* reverse */
19092 p = highlight_has_attr(id, HL_INVERSE, modec);
19093 break;
19094
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019095 case 's':
19096 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19097 p = highlight_color(id, what, modec);
19098 else /* standout */
19099 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 break;
19101
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019102 case 'u':
19103 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19104 /* underline */
19105 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19106 else
19107 /* undercurl */
19108 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 break;
19110 }
19111
19112 if (p != NULL)
19113 p = vim_strsave(p);
19114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115 rettv->v_type = VAR_STRING;
19116 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117}
19118
19119/*
19120 * "synIDtrans(id)" function
19121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019124 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019125 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126{
19127 int id;
19128
19129#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131
19132 if (id > 0)
19133 id = syn_get_final_id(id);
19134 else
19135#endif
19136 id = 0;
19137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139}
19140
19141/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019142 * "synconcealed(lnum, col)" function
19143 */
19144 static void
19145f_synconcealed(argvars, rettv)
19146 typval_T *argvars UNUSED;
19147 typval_T *rettv;
19148{
19149#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19150 long lnum;
19151 long col;
19152 int syntax_flags = 0;
19153 int cchar;
19154 int matchid = 0;
19155 char_u str[NUMBUFLEN];
19156#endif
19157
19158 rettv->v_type = VAR_LIST;
19159 rettv->vval.v_list = NULL;
19160
19161#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19162 lnum = get_tv_lnum(argvars); /* -1 on type error */
19163 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19164
19165 vim_memset(str, NUL, sizeof(str));
19166
19167 if (rettv_list_alloc(rettv) != FAIL)
19168 {
19169 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19170 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19171 && curwin->w_p_cole > 0)
19172 {
19173 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19174 syntax_flags = get_syntax_info(&matchid);
19175
19176 /* get the conceal character */
19177 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19178 {
19179 cchar = syn_get_sub_char();
19180 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19181 cchar = lcs_conceal;
19182 if (cchar != NUL)
19183 {
19184# ifdef FEAT_MBYTE
19185 if (has_mbyte)
19186 (*mb_char2bytes)(cchar, str);
19187 else
19188# endif
19189 str[0] = cchar;
19190 }
19191 }
19192 }
19193
19194 list_append_number(rettv->vval.v_list,
19195 (syntax_flags & HL_CONCEAL) != 0);
19196 /* -1 to auto-determine strlen */
19197 list_append_string(rettv->vval.v_list, str, -1);
19198 list_append_number(rettv->vval.v_list, matchid);
19199 }
19200#endif
19201}
19202
19203/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019204 * "synstack(lnum, col)" function
19205 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019206 static void
19207f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019208 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019209 typval_T *rettv;
19210{
19211#ifdef FEAT_SYN_HL
19212 long lnum;
19213 long col;
19214 int i;
19215 int id;
19216#endif
19217
19218 rettv->v_type = VAR_LIST;
19219 rettv->vval.v_list = NULL;
19220
19221#ifdef FEAT_SYN_HL
19222 lnum = get_tv_lnum(argvars); /* -1 on type error */
19223 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19224
19225 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019226 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019227 && rettv_list_alloc(rettv) != FAIL)
19228 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019229 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019230 for (i = 0; ; ++i)
19231 {
19232 id = syn_get_stack_item(i);
19233 if (id < 0)
19234 break;
19235 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19236 break;
19237 }
19238 }
19239#endif
19240}
19241
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019243get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019244 typval_T *argvars;
19245 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019246 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019248 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019250 char_u *infile = NULL;
19251 char_u buf[NUMBUFLEN];
19252 int err = FALSE;
19253 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019254 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019255 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019257 rettv->v_type = VAR_STRING;
19258 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019259 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019260 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019261
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019262 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019263 {
19264 /*
19265 * Write the string to a temp file, to be used for input of the shell
19266 * command.
19267 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019268 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019269 {
19270 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019271 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019272 }
19273
19274 fd = mch_fopen((char *)infile, WRITEBIN);
19275 if (fd == NULL)
19276 {
19277 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019278 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019279 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019280 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019281 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019282 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19283 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019284 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019285 else
19286 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019287 size_t len;
19288
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019289 p = get_tv_string_buf_chk(&argvars[1], buf);
19290 if (p == NULL)
19291 {
19292 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019293 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019294 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019295 len = STRLEN(p);
19296 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019297 err = TRUE;
19298 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019299 if (fclose(fd) != 0)
19300 err = TRUE;
19301 if (err)
19302 {
19303 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019304 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019305 }
19306 }
19307
Bram Moolenaar52a72462014-08-29 15:53:52 +020019308 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19309 * echoes typeahead, that messes up the display. */
19310 if (!msg_silent)
19311 flags += SHELL_COOKED;
19312
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019313 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019315 int len;
19316 listitem_T *li;
19317 char_u *s = NULL;
19318 char_u *start;
19319 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019320 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321
Bram Moolenaar52a72462014-08-29 15:53:52 +020019322 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019323 if (res == NULL)
19324 goto errret;
19325
19326 list = list_alloc();
19327 if (list == NULL)
19328 goto errret;
19329
19330 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019332 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019333 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019334 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019335 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019336
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019337 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019338 if (s == NULL)
19339 goto errret;
19340
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019341 for (p = s; start < end; ++p, ++start)
19342 *p = *start == NUL ? NL : *start;
19343 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019344
19345 li = listitem_alloc();
19346 if (li == NULL)
19347 {
19348 vim_free(s);
19349 goto errret;
19350 }
19351 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019352 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019353 li->li_tv.vval.v_string = s;
19354 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019356
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019357 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019358 rettv->v_type = VAR_LIST;
19359 rettv->vval.v_list = list;
19360 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019362 else
19363 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019364 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019365#ifdef USE_CR
19366 /* translate <CR> into <NL> */
19367 if (res != NULL)
19368 {
19369 char_u *s;
19370
19371 for (s = res; *s; ++s)
19372 {
19373 if (*s == CAR)
19374 *s = NL;
19375 }
19376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377#else
19378# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019379 /* translate <CR><NL> into <NL> */
19380 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019382 char_u *s, *d;
19383
19384 d = res;
19385 for (s = res; *s; ++s)
19386 {
19387 if (s[0] == CAR && s[1] == NL)
19388 ++s;
19389 *d++ = *s;
19390 }
19391 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393# endif
19394#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019395 rettv->vval.v_string = res;
19396 res = NULL;
19397 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019398
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019399errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019400 if (infile != NULL)
19401 {
19402 mch_remove(infile);
19403 vim_free(infile);
19404 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019405 if (res != NULL)
19406 vim_free(res);
19407 if (list != NULL)
19408 list_free(list, TRUE);
19409}
19410
19411/*
19412 * "system()" function
19413 */
19414 static void
19415f_system(argvars, rettv)
19416 typval_T *argvars;
19417 typval_T *rettv;
19418{
19419 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19420}
19421
19422/*
19423 * "systemlist()" function
19424 */
19425 static void
19426f_systemlist(argvars, rettv)
19427 typval_T *argvars;
19428 typval_T *rettv;
19429{
19430 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431}
19432
19433/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019434 * "tabpagebuflist()" function
19435 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019436 static void
19437f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019438 typval_T *argvars UNUSED;
19439 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019440{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019441#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019442 tabpage_T *tp;
19443 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019444
19445 if (argvars[0].v_type == VAR_UNKNOWN)
19446 wp = firstwin;
19447 else
19448 {
19449 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19450 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019451 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019452 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019453 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019454 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019455 for (; wp != NULL; wp = wp->w_next)
19456 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019457 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019458 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019459 }
19460#endif
19461}
19462
19463
19464/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019465 * "tabpagenr()" function
19466 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019467 static void
19468f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019469 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019470 typval_T *rettv;
19471{
19472 int nr = 1;
19473#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019474 char_u *arg;
19475
19476 if (argvars[0].v_type != VAR_UNKNOWN)
19477 {
19478 arg = get_tv_string_chk(&argvars[0]);
19479 nr = 0;
19480 if (arg != NULL)
19481 {
19482 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019483 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019484 else
19485 EMSG2(_(e_invexpr2), arg);
19486 }
19487 }
19488 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019489 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019490#endif
19491 rettv->vval.v_number = nr;
19492}
19493
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019494
19495#ifdef FEAT_WINDOWS
19496static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
19497
19498/*
19499 * Common code for tabpagewinnr() and winnr().
19500 */
19501 static int
19502get_winnr(tp, argvar)
19503 tabpage_T *tp;
19504 typval_T *argvar;
19505{
19506 win_T *twin;
19507 int nr = 1;
19508 win_T *wp;
19509 char_u *arg;
19510
19511 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19512 if (argvar->v_type != VAR_UNKNOWN)
19513 {
19514 arg = get_tv_string_chk(argvar);
19515 if (arg == NULL)
19516 nr = 0; /* type error; errmsg already given */
19517 else if (STRCMP(arg, "$") == 0)
19518 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19519 else if (STRCMP(arg, "#") == 0)
19520 {
19521 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19522 if (twin == NULL)
19523 nr = 0;
19524 }
19525 else
19526 {
19527 EMSG2(_(e_invexpr2), arg);
19528 nr = 0;
19529 }
19530 }
19531
19532 if (nr > 0)
19533 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19534 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019535 {
19536 if (wp == NULL)
19537 {
19538 /* didn't find it in this tabpage */
19539 nr = 0;
19540 break;
19541 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019542 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019543 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019544 return nr;
19545}
19546#endif
19547
19548/*
19549 * "tabpagewinnr()" function
19550 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019551 static void
19552f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019553 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019554 typval_T *rettv;
19555{
19556 int nr = 1;
19557#ifdef FEAT_WINDOWS
19558 tabpage_T *tp;
19559
19560 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19561 if (tp == NULL)
19562 nr = 0;
19563 else
19564 nr = get_winnr(tp, &argvars[1]);
19565#endif
19566 rettv->vval.v_number = nr;
19567}
19568
19569
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019570/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019571 * "tagfiles()" function
19572 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019573 static void
19574f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019575 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019576 typval_T *rettv;
19577{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019578 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019579 tagname_T tn;
19580 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019582 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019583 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019584 fname = alloc(MAXPATHL);
19585 if (fname == NULL)
19586 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019587
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019588 for (first = TRUE; ; first = FALSE)
19589 if (get_tagfname(&tn, first, fname) == FAIL
19590 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019591 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019592 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019593 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019594}
19595
19596/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019597 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019598 */
19599 static void
19600f_taglist(argvars, rettv)
19601 typval_T *argvars;
19602 typval_T *rettv;
19603{
19604 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019605
19606 tag_pattern = get_tv_string(&argvars[0]);
19607
19608 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019609 if (*tag_pattern == NUL)
19610 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019611
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019612 if (rettv_list_alloc(rettv) == OK)
19613 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019614}
19615
19616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 * "tempname()" function
19618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019621 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623{
19624 static int x = 'A';
19625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019626 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019627 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628
19629 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19630 * names. Skip 'I' and 'O', they are used for shell redirection. */
19631 do
19632 {
19633 if (x == 'Z')
19634 x = '0';
19635 else if (x == '9')
19636 x = 'A';
19637 else
19638 {
19639#ifdef EBCDIC
19640 if (x == 'I')
19641 x = 'J';
19642 else if (x == 'R')
19643 x = 'S';
19644 else
19645#endif
19646 ++x;
19647 }
19648 } while (x == 'I' || x == 'O');
19649}
19650
19651/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019652 * "test(list)" function: Just checking the walls...
19653 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019654 static void
19655f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019656 typval_T *argvars UNUSED;
19657 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019658{
19659 /* Used for unit testing. Change the code below to your liking. */
19660#if 0
19661 listitem_T *li;
19662 list_T *l;
19663 char_u *bad, *good;
19664
19665 if (argvars[0].v_type != VAR_LIST)
19666 return;
19667 l = argvars[0].vval.v_list;
19668 if (l == NULL)
19669 return;
19670 li = l->lv_first;
19671 if (li == NULL)
19672 return;
19673 bad = get_tv_string(&li->li_tv);
19674 li = li->li_next;
19675 if (li == NULL)
19676 return;
19677 good = get_tv_string(&li->li_tv);
19678 rettv->vval.v_number = test_edit_score(bad, good);
19679#endif
19680}
19681
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019682#ifdef FEAT_FLOAT
19683/*
19684 * "tan()" function
19685 */
19686 static void
19687f_tan(argvars, rettv)
19688 typval_T *argvars;
19689 typval_T *rettv;
19690{
19691 float_T f;
19692
19693 rettv->v_type = VAR_FLOAT;
19694 if (get_float_arg(argvars, &f) == OK)
19695 rettv->vval.v_float = tan(f);
19696 else
19697 rettv->vval.v_float = 0.0;
19698}
19699
19700/*
19701 * "tanh()" function
19702 */
19703 static void
19704f_tanh(argvars, rettv)
19705 typval_T *argvars;
19706 typval_T *rettv;
19707{
19708 float_T f;
19709
19710 rettv->v_type = VAR_FLOAT;
19711 if (get_float_arg(argvars, &f) == OK)
19712 rettv->vval.v_float = tanh(f);
19713 else
19714 rettv->vval.v_float = 0.0;
19715}
19716#endif
19717
Bram Moolenaard52d9742005-08-21 22:20:28 +000019718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 * "tolower(string)" function
19720 */
19721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019723 typval_T *argvars;
19724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725{
19726 char_u *p;
19727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 p = vim_strsave(get_tv_string(&argvars[0]));
19729 rettv->v_type = VAR_STRING;
19730 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731
19732 if (p != NULL)
19733 while (*p != NUL)
19734 {
19735#ifdef FEAT_MBYTE
19736 int l;
19737
19738 if (enc_utf8)
19739 {
19740 int c, lc;
19741
19742 c = utf_ptr2char(p);
19743 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019744 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 /* TODO: reallocate string when byte count changes. */
19746 if (utf_char2len(lc) == l)
19747 utf_char2bytes(lc, p);
19748 p += l;
19749 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019750 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 p += l; /* skip multi-byte character */
19752 else
19753#endif
19754 {
19755 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19756 ++p;
19757 }
19758 }
19759}
19760
19761/*
19762 * "toupper(string)" function
19763 */
19764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019766 typval_T *argvars;
19767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019770 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771}
19772
19773/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019774 * "tr(string, fromstr, tostr)" function
19775 */
19776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 typval_T *argvars;
19779 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019780{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019781 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019782 char_u *fromstr;
19783 char_u *tostr;
19784 char_u *p;
19785#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019786 int inlen;
19787 int fromlen;
19788 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019789 int idx;
19790 char_u *cpstr;
19791 int cplen;
19792 int first = TRUE;
19793#endif
19794 char_u buf[NUMBUFLEN];
19795 char_u buf2[NUMBUFLEN];
19796 garray_T ga;
19797
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019798 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019799 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19800 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019801
19802 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803 rettv->v_type = VAR_STRING;
19804 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019805 if (fromstr == NULL || tostr == NULL)
19806 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019807 ga_init2(&ga, (int)sizeof(char), 80);
19808
19809#ifdef FEAT_MBYTE
19810 if (!has_mbyte)
19811#endif
19812 /* not multi-byte: fromstr and tostr must be the same length */
19813 if (STRLEN(fromstr) != STRLEN(tostr))
19814 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019815#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019816error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019817#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019818 EMSG2(_(e_invarg2), fromstr);
19819 ga_clear(&ga);
19820 return;
19821 }
19822
19823 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019824 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019825 {
19826#ifdef FEAT_MBYTE
19827 if (has_mbyte)
19828 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019829 inlen = (*mb_ptr2len)(in_str);
19830 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019831 cplen = inlen;
19832 idx = 0;
19833 for (p = fromstr; *p != NUL; p += fromlen)
19834 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019835 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019836 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019837 {
19838 for (p = tostr; *p != NUL; p += tolen)
19839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019840 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019841 if (idx-- == 0)
19842 {
19843 cplen = tolen;
19844 cpstr = p;
19845 break;
19846 }
19847 }
19848 if (*p == NUL) /* tostr is shorter than fromstr */
19849 goto error;
19850 break;
19851 }
19852 ++idx;
19853 }
19854
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019855 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019856 {
19857 /* Check that fromstr and tostr have the same number of
19858 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019859 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019860 first = FALSE;
19861 for (p = tostr; *p != NUL; p += tolen)
19862 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019863 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019864 --idx;
19865 }
19866 if (idx != 0)
19867 goto error;
19868 }
19869
Bram Moolenaarcde88542015-08-11 19:14:00 +020019870 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019871 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019872 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019873
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019874 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019875 }
19876 else
19877#endif
19878 {
19879 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019880 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019881 if (p != NULL)
19882 ga_append(&ga, tostr[p - fromstr]);
19883 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019884 ga_append(&ga, *in_str);
19885 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019886 }
19887 }
19888
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019889 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019890 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019891 ga_append(&ga, NUL);
19892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019894}
19895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019896#ifdef FEAT_FLOAT
19897/*
19898 * "trunc({float})" function
19899 */
19900 static void
19901f_trunc(argvars, rettv)
19902 typval_T *argvars;
19903 typval_T *rettv;
19904{
19905 float_T f;
19906
19907 rettv->v_type = VAR_FLOAT;
19908 if (get_float_arg(argvars, &f) == OK)
19909 /* trunc() is not in C90, use floor() or ceil() instead. */
19910 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19911 else
19912 rettv->vval.v_float = 0.0;
19913}
19914#endif
19915
Bram Moolenaar8299df92004-07-10 09:47:34 +000019916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 * "type(expr)" function
19918 */
19919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019921 typval_T *argvars;
19922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019924 int n;
19925
19926 switch (argvars[0].v_type)
19927 {
19928 case VAR_NUMBER: n = 0; break;
19929 case VAR_STRING: n = 1; break;
19930 case VAR_FUNC: n = 2; break;
19931 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019932 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019933#ifdef FEAT_FLOAT
19934 case VAR_FLOAT: n = 5; break;
19935#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019936 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19937 }
19938 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939}
19940
19941/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019942 * "undofile(name)" function
19943 */
19944 static void
19945f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019946 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019947 typval_T *rettv;
19948{
19949 rettv->v_type = VAR_STRING;
19950#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019951 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019952 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019953
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019954 if (*fname == NUL)
19955 {
19956 /* If there is no file name there will be no undo file. */
19957 rettv->vval.v_string = NULL;
19958 }
19959 else
19960 {
19961 char_u *ffname = FullName_save(fname, FALSE);
19962
19963 if (ffname != NULL)
19964 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19965 vim_free(ffname);
19966 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019967 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019968#else
19969 rettv->vval.v_string = NULL;
19970#endif
19971}
19972
19973/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019974 * "undotree()" function
19975 */
19976 static void
19977f_undotree(argvars, rettv)
19978 typval_T *argvars UNUSED;
19979 typval_T *rettv;
19980{
19981 if (rettv_dict_alloc(rettv) == OK)
19982 {
19983 dict_T *dict = rettv->vval.v_dict;
19984 list_T *list;
19985
Bram Moolenaar730cde92010-06-27 05:18:54 +020019986 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019987 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019988 dict_add_nr_str(dict, "save_last",
19989 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019990 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19991 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019992 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019993
19994 list = list_alloc();
19995 if (list != NULL)
19996 {
19997 u_eval_tree(curbuf->b_u_oldhead, list);
19998 dict_add_list(dict, "entries", list);
19999 }
20000 }
20001}
20002
20003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020004 * "values(dict)" function
20005 */
20006 static void
20007f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 typval_T *argvars;
20009 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020010{
20011 dict_list(argvars, rettv, 1);
20012}
20013
20014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015 * "virtcol(string)" function
20016 */
20017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 typval_T *argvars;
20020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021{
20022 colnr_T vcol = 0;
20023 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020024 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020026 fp = var2fpos(&argvars[0], FALSE, &fnum);
20027 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20028 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 {
20030 getvvcol(curwin, fp, NULL, NULL, &vcol);
20031 ++vcol;
20032 }
20033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035}
20036
20037/*
20038 * "visualmode()" function
20039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020042 typval_T *argvars UNUSED;
20043 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 char_u str[2];
20046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 str[0] = curbuf->b_visual_mode_eval;
20049 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051
20052 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020053 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055}
20056
20057/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020058 * "wildmenumode()" function
20059 */
20060 static void
20061f_wildmenumode(argvars, rettv)
20062 typval_T *argvars UNUSED;
20063 typval_T *rettv UNUSED;
20064{
20065#ifdef FEAT_WILDMENU
20066 if (wild_menu_showing)
20067 rettv->vval.v_number = 1;
20068#endif
20069}
20070
20071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 * "winbufnr(nr)" function
20073 */
20074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 typval_T *argvars;
20077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078{
20079 win_T *wp;
20080
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020081 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086}
20087
20088/*
20089 * "wincol()" function
20090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020093 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
20096 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098}
20099
20100/*
20101 * "winheight(nr)" function
20102 */
20103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020105 typval_T *argvars;
20106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107{
20108 win_T *wp;
20109
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020110 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115}
20116
20117/*
20118 * "winline()" function
20119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020122 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124{
20125 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127}
20128
20129/*
20130 * "winnr()" function
20131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020133f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136{
20137 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020138
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020140 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020142 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143}
20144
20145/*
20146 * "winrestcmd()" function
20147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020150 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152{
20153#ifdef FEAT_WINDOWS
20154 win_T *wp;
20155 int winnr = 1;
20156 garray_T ga;
20157 char_u buf[50];
20158
20159 ga_init2(&ga, (int)sizeof(char), 70);
20160 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20161 {
20162 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20163 ga_concat(&ga, buf);
20164# ifdef FEAT_VERTSPLIT
20165 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20166 ga_concat(&ga, buf);
20167# endif
20168 ++winnr;
20169 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020170 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177}
20178
20179/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020180 * "winrestview()" function
20181 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020182 static void
20183f_winrestview(argvars, rettv)
20184 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020185 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020186{
20187 dict_T *dict;
20188
20189 if (argvars[0].v_type != VAR_DICT
20190 || (dict = argvars[0].vval.v_dict) == NULL)
20191 EMSG(_(e_invarg));
20192 else
20193 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020194 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20195 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20196 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20197 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020198#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020199 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20200 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020201#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020202 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20203 {
20204 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20205 curwin->w_set_curswant = FALSE;
20206 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020207
Bram Moolenaar82c25852014-05-28 16:47:16 +020020208 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20209 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020210#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020211 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20212 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020213#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020214 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20215 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20216 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20217 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020218
20219 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020220 win_new_height(curwin, curwin->w_height);
20221# ifdef FEAT_VERTSPLIT
20222 win_new_width(curwin, W_WIDTH(curwin));
20223# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020224 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020225
Bram Moolenaarb851a962014-10-31 15:45:52 +010020226 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020227 curwin->w_topline = 1;
20228 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20229 curwin->w_topline = curbuf->b_ml.ml_line_count;
20230#ifdef FEAT_DIFF
20231 check_topfill(curwin, TRUE);
20232#endif
20233 }
20234}
20235
20236/*
20237 * "winsaveview()" function
20238 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020239 static void
20240f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020241 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020242 typval_T *rettv;
20243{
20244 dict_T *dict;
20245
Bram Moolenaara800b422010-06-27 01:15:55 +020020246 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020247 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020248 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020249
20250 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20251 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20252#ifdef FEAT_VIRTUALEDIT
20253 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20254#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020255 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020256 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20257
20258 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20259#ifdef FEAT_DIFF
20260 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20261#endif
20262 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20263 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20264}
20265
20266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 * "winwidth(nr)" function
20268 */
20269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020271 typval_T *argvars;
20272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273{
20274 win_T *wp;
20275
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020276 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 else
20280#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020281 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020283 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284#endif
20285}
20286
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020288 * "wordcount()" function
20289 */
20290 static void
20291f_wordcount(argvars, rettv)
20292 typval_T *argvars UNUSED;
20293 typval_T *rettv;
20294{
20295 if (rettv_dict_alloc(rettv) == FAIL)
20296 return;
20297 cursor_pos_info(rettv->vval.v_dict);
20298}
20299
20300/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020301 * Write list of strings to file
20302 */
20303 static int
20304write_list(fd, list, binary)
20305 FILE *fd;
20306 list_T *list;
20307 int binary;
20308{
20309 listitem_T *li;
20310 int c;
20311 int ret = OK;
20312 char_u *s;
20313
20314 for (li = list->lv_first; li != NULL; li = li->li_next)
20315 {
20316 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20317 {
20318 if (*s == '\n')
20319 c = putc(NUL, fd);
20320 else
20321 c = putc(*s, fd);
20322 if (c == EOF)
20323 {
20324 ret = FAIL;
20325 break;
20326 }
20327 }
20328 if (!binary || li->li_next != NULL)
20329 if (putc('\n', fd) == EOF)
20330 {
20331 ret = FAIL;
20332 break;
20333 }
20334 if (ret == FAIL)
20335 {
20336 EMSG(_(e_write));
20337 break;
20338 }
20339 }
20340 return ret;
20341}
20342
20343/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020344 * "writefile()" function
20345 */
20346 static void
20347f_writefile(argvars, rettv)
20348 typval_T *argvars;
20349 typval_T *rettv;
20350{
20351 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020352 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020353 char_u *fname;
20354 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020355 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020356
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020357 if (check_restricted() || check_secure())
20358 return;
20359
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020360 if (argvars[0].v_type != VAR_LIST)
20361 {
20362 EMSG2(_(e_listarg), "writefile()");
20363 return;
20364 }
20365 if (argvars[0].vval.v_list == NULL)
20366 return;
20367
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020368 if (argvars[2].v_type != VAR_UNKNOWN)
20369 {
20370 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20371 binary = TRUE;
20372 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20373 append = TRUE;
20374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020375
20376 /* Always open the file in binary mode, library functions have a mind of
20377 * their own about CR-LF conversion. */
20378 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020379 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20380 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020381 {
20382 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20383 ret = -1;
20384 }
20385 else
20386 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020387 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20388 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020389 fclose(fd);
20390 }
20391
20392 rettv->vval.v_number = ret;
20393}
20394
20395/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020396 * "xor(expr, expr)" function
20397 */
20398 static void
20399f_xor(argvars, rettv)
20400 typval_T *argvars;
20401 typval_T *rettv;
20402{
20403 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20404 ^ get_tv_number_chk(&argvars[1], NULL);
20405}
20406
20407
20408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020410 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 */
20412 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020413var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020414 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020415 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020416 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020418 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020420 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421
Bram Moolenaara5525202006-03-02 22:52:09 +000020422 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020423 if (varp->v_type == VAR_LIST)
20424 {
20425 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020426 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020427 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020428 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020429
20430 l = varp->vval.v_list;
20431 if (l == NULL)
20432 return NULL;
20433
20434 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020435 pos.lnum = list_find_nr(l, 0L, &error);
20436 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020437 return NULL; /* invalid line number */
20438
20439 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020440 pos.col = list_find_nr(l, 1L, &error);
20441 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020442 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020443 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020444
20445 /* We accept "$" for the column number: last column. */
20446 li = list_find(l, 1L);
20447 if (li != NULL && li->li_tv.v_type == VAR_STRING
20448 && li->li_tv.vval.v_string != NULL
20449 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20450 pos.col = len + 1;
20451
Bram Moolenaara5525202006-03-02 22:52:09 +000020452 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020453 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020454 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020455 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020456
Bram Moolenaara5525202006-03-02 22:52:09 +000020457#ifdef FEAT_VIRTUALEDIT
20458 /* Get the virtual offset. Defaults to zero. */
20459 pos.coladd = list_find_nr(l, 2L, &error);
20460 if (error)
20461 pos.coladd = 0;
20462#endif
20463
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020464 return &pos;
20465 }
20466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020467 name = get_tv_string_chk(varp);
20468 if (name == NULL)
20469 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020470 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020472 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20473 {
20474 if (VIsual_active)
20475 return &VIsual;
20476 return &curwin->w_cursor;
20477 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020478 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020480 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20482 return NULL;
20483 return pp;
20484 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020485
20486#ifdef FEAT_VIRTUALEDIT
20487 pos.coladd = 0;
20488#endif
20489
Bram Moolenaar477933c2007-07-17 14:32:23 +000020490 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020491 {
20492 pos.col = 0;
20493 if (name[1] == '0') /* "w0": first visible line */
20494 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020495 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020496 pos.lnum = curwin->w_topline;
20497 return &pos;
20498 }
20499 else if (name[1] == '$') /* "w$": last visible line */
20500 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020501 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020502 pos.lnum = curwin->w_botline - 1;
20503 return &pos;
20504 }
20505 }
20506 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020508 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 {
20510 pos.lnum = curbuf->b_ml.ml_line_count;
20511 pos.col = 0;
20512 }
20513 else
20514 {
20515 pos.lnum = curwin->w_cursor.lnum;
20516 pos.col = (colnr_T)STRLEN(ml_get_curline());
20517 }
20518 return &pos;
20519 }
20520 return NULL;
20521}
20522
20523/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020524 * Convert list in "arg" into a position and optional file number.
20525 * When "fnump" is NULL there is no file number, only 3 items.
20526 * Note that the column is passed on as-is, the caller may want to decrement
20527 * it to use 1 for the first column.
20528 * Return FAIL when conversion is not possible, doesn't check the position for
20529 * validity.
20530 */
20531 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020020532list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020533 typval_T *arg;
20534 pos_T *posp;
20535 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020020536 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020537{
20538 list_T *l = arg->vval.v_list;
20539 long i = 0;
20540 long n;
20541
Bram Moolenaar493c1782014-05-28 14:34:46 +020020542 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20543 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020544 if (arg->v_type != VAR_LIST
20545 || l == NULL
20546 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020547 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020548 return FAIL;
20549
20550 if (fnump != NULL)
20551 {
20552 n = list_find_nr(l, i++, NULL); /* fnum */
20553 if (n < 0)
20554 return FAIL;
20555 if (n == 0)
20556 n = curbuf->b_fnum; /* current buffer */
20557 *fnump = n;
20558 }
20559
20560 n = list_find_nr(l, i++, NULL); /* lnum */
20561 if (n < 0)
20562 return FAIL;
20563 posp->lnum = n;
20564
20565 n = list_find_nr(l, i++, NULL); /* col */
20566 if (n < 0)
20567 return FAIL;
20568 posp->col = n;
20569
20570#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020571 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020572 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020573 posp->coladd = 0;
20574 else
20575 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020576#endif
20577
Bram Moolenaar493c1782014-05-28 14:34:46 +020020578 if (curswantp != NULL)
20579 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20580
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020581 return OK;
20582}
20583
20584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 * Get the length of an environment variable name.
20586 * Advance "arg" to the first character after the name.
20587 * Return 0 for error.
20588 */
20589 static int
20590get_env_len(arg)
20591 char_u **arg;
20592{
20593 char_u *p;
20594 int len;
20595
20596 for (p = *arg; vim_isIDc(*p); ++p)
20597 ;
20598 if (p == *arg) /* no name found */
20599 return 0;
20600
20601 len = (int)(p - *arg);
20602 *arg = p;
20603 return len;
20604}
20605
20606/*
20607 * Get the length of the name of a function or internal variable.
20608 * "arg" is advanced to the first non-white character after the name.
20609 * Return 0 if something is wrong.
20610 */
20611 static int
20612get_id_len(arg)
20613 char_u **arg;
20614{
20615 char_u *p;
20616 int len;
20617
20618 /* Find the end of the name. */
20619 for (p = *arg; eval_isnamec(*p); ++p)
20620 ;
20621 if (p == *arg) /* no name found */
20622 return 0;
20623
20624 len = (int)(p - *arg);
20625 *arg = skipwhite(p);
20626
20627 return len;
20628}
20629
20630/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020631 * Get the length of the name of a variable or function.
20632 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020634 * Return -1 if curly braces expansion failed.
20635 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 * If the name contains 'magic' {}'s, expand them and return the
20637 * expanded name in an allocated string via 'alias' - caller must free.
20638 */
20639 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020640get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 char_u **arg;
20642 char_u **alias;
20643 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020644 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645{
20646 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 char_u *p;
20648 char_u *expr_start;
20649 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650
20651 *alias = NULL; /* default to no alias */
20652
20653 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20654 && (*arg)[2] == (int)KE_SNR)
20655 {
20656 /* hard coded <SNR>, already translated */
20657 *arg += 3;
20658 return get_id_len(arg) + 3;
20659 }
20660 len = eval_fname_script(*arg);
20661 if (len > 0)
20662 {
20663 /* literal "<SID>", "s:" or "<SNR>" */
20664 *arg += len;
20665 }
20666
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020668 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020670 p = find_name_end(*arg, &expr_start, &expr_end,
20671 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 if (expr_start != NULL)
20673 {
20674 char_u *temp_string;
20675
20676 if (!evaluate)
20677 {
20678 len += (int)(p - *arg);
20679 *arg = skipwhite(p);
20680 return len;
20681 }
20682
20683 /*
20684 * Include any <SID> etc in the expanded string:
20685 * Thus the -len here.
20686 */
20687 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20688 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020689 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690 *alias = temp_string;
20691 *arg = skipwhite(p);
20692 return (int)STRLEN(temp_string);
20693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694
20695 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020696 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 EMSG2(_(e_invexpr2), *arg);
20698
20699 return len;
20700}
20701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020702/*
20703 * Find the end of a variable or function name, taking care of magic braces.
20704 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20705 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020706 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020707 * Return a pointer to just after the name. Equal to "arg" if there is no
20708 * valid name.
20709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020711find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 char_u *arg;
20713 char_u **expr_start;
20714 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020715 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020717 int mb_nest = 0;
20718 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 char_u *p;
20720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020721 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020723 *expr_start = NULL;
20724 *expr_end = NULL;
20725 }
20726
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020727 /* Quick check for valid starting character. */
20728 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20729 return arg;
20730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020731 for (p = arg; *p != NUL
20732 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020733 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020734 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020736 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020737 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020738 if (*p == '\'')
20739 {
20740 /* skip over 'string' to avoid counting [ and ] inside it. */
20741 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20742 ;
20743 if (*p == NUL)
20744 break;
20745 }
20746 else if (*p == '"')
20747 {
20748 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20749 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20750 if (*p == '\\' && p[1] != NUL)
20751 ++p;
20752 if (*p == NUL)
20753 break;
20754 }
20755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020756 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020758 if (*p == '[')
20759 ++br_nest;
20760 else if (*p == ']')
20761 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020764 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020766 if (*p == '{')
20767 {
20768 mb_nest++;
20769 if (expr_start != NULL && *expr_start == NULL)
20770 *expr_start = p;
20771 }
20772 else if (*p == '}')
20773 {
20774 mb_nest--;
20775 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20776 *expr_end = p;
20777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 }
20780
20781 return p;
20782}
20783
20784/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020785 * Expands out the 'magic' {}'s in a variable/function name.
20786 * Note that this can call itself recursively, to deal with
20787 * constructs like foo{bar}{baz}{bam}
20788 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20789 * "in_start" ^
20790 * "expr_start" ^
20791 * "expr_end" ^
20792 * "in_end" ^
20793 *
20794 * Returns a new allocated string, which the caller must free.
20795 * Returns NULL for failure.
20796 */
20797 static char_u *
20798make_expanded_name(in_start, expr_start, expr_end, in_end)
20799 char_u *in_start;
20800 char_u *expr_start;
20801 char_u *expr_end;
20802 char_u *in_end;
20803{
20804 char_u c1;
20805 char_u *retval = NULL;
20806 char_u *temp_result;
20807 char_u *nextcmd = NULL;
20808
20809 if (expr_end == NULL || in_end == NULL)
20810 return NULL;
20811 *expr_start = NUL;
20812 *expr_end = NUL;
20813 c1 = *in_end;
20814 *in_end = NUL;
20815
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020816 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020817 if (temp_result != NULL && nextcmd == NULL)
20818 {
20819 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20820 + (in_end - expr_end) + 1));
20821 if (retval != NULL)
20822 {
20823 STRCPY(retval, in_start);
20824 STRCAT(retval, temp_result);
20825 STRCAT(retval, expr_end + 1);
20826 }
20827 }
20828 vim_free(temp_result);
20829
20830 *in_end = c1; /* put char back for error messages */
20831 *expr_start = '{';
20832 *expr_end = '}';
20833
20834 if (retval != NULL)
20835 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020836 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020837 if (expr_start != NULL)
20838 {
20839 /* Further expansion! */
20840 temp_result = make_expanded_name(retval, expr_start,
20841 expr_end, temp_result);
20842 vim_free(retval);
20843 retval = temp_result;
20844 }
20845 }
20846
20847 return retval;
20848}
20849
20850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020852 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 */
20854 static int
20855eval_isnamec(c)
20856 int c;
20857{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020858 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20859}
20860
20861/*
20862 * Return TRUE if character "c" can be used as the first character in a
20863 * variable or function name (excluding '{' and '}').
20864 */
20865 static int
20866eval_isnamec1(c)
20867 int c;
20868{
20869 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 * Set number v: variable to "val".
20874 */
20875 void
20876set_vim_var_nr(idx, val)
20877 int idx;
20878 long val;
20879{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020880 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881}
20882
20883/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020884 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 */
20886 long
20887get_vim_var_nr(idx)
20888 int idx;
20889{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020890 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891}
20892
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020893/*
20894 * Get string v: variable value. Uses a static buffer, can only be used once.
20895 */
20896 char_u *
20897get_vim_var_str(idx)
20898 int idx;
20899{
20900 return get_tv_string(&vimvars[idx].vv_tv);
20901}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020902
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020904 * Get List v: variable value. Caller must take care of reference count when
20905 * needed.
20906 */
20907 list_T *
20908get_vim_var_list(idx)
20909 int idx;
20910{
20911 return vimvars[idx].vv_list;
20912}
20913
20914/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020915 * Set v:char to character "c".
20916 */
20917 void
20918set_vim_var_char(c)
20919 int c;
20920{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020921 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020922
20923#ifdef FEAT_MBYTE
20924 if (has_mbyte)
20925 buf[(*mb_char2bytes)(c, buf)] = NUL;
20926 else
20927#endif
20928 {
20929 buf[0] = c;
20930 buf[1] = NUL;
20931 }
20932 set_vim_var_string(VV_CHAR, buf, -1);
20933}
20934
20935/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020936 * Set v:count to "count" and v:count1 to "count1".
20937 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 */
20939 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020940set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 long count;
20942 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020943 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020945 if (set_prevcount)
20946 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020947 vimvars[VV_COUNT].vv_nr = count;
20948 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949}
20950
20951/*
20952 * Set string v: variable to a copy of "val".
20953 */
20954 void
20955set_vim_var_string(idx, val, len)
20956 int idx;
20957 char_u *val;
20958 int len; /* length of "val" to use or -1 (whole string) */
20959{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020960 /* Need to do this (at least) once, since we can't initialize a union.
20961 * Will always be invoked when "v:progname" is set. */
20962 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20963
Bram Moolenaare9a41262005-01-15 22:18:47 +000020964 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020966 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020968 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020970 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971}
20972
20973/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020974 * Set List v: variable to "val".
20975 */
20976 void
20977set_vim_var_list(idx, val)
20978 int idx;
20979 list_T *val;
20980{
20981 list_unref(vimvars[idx].vv_list);
20982 vimvars[idx].vv_list = val;
20983 if (val != NULL)
20984 ++val->lv_refcount;
20985}
20986
20987/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020020988 * Set Dictionary v: variable to "val".
20989 */
20990 void
20991set_vim_var_dict(idx, val)
20992 int idx;
20993 dict_T *val;
20994{
20995 int todo;
20996 hashitem_T *hi;
20997
20998 dict_unref(vimvars[idx].vv_dict);
20999 vimvars[idx].vv_dict = val;
21000 if (val != NULL)
21001 {
21002 ++val->dv_refcount;
21003
21004 /* Set readonly */
21005 todo = (int)val->dv_hashtab.ht_used;
21006 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21007 {
21008 if (HASHITEM_EMPTY(hi))
21009 continue;
21010 --todo;
21011 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21012 }
21013 }
21014}
21015
21016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 * Set v:register if needed.
21018 */
21019 void
21020set_reg_var(c)
21021 int c;
21022{
21023 char_u regname;
21024
21025 if (c == 0 || c == ' ')
21026 regname = '"';
21027 else
21028 regname = c;
21029 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021030 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 set_vim_var_string(VV_REG, &regname, 1);
21032}
21033
21034/*
21035 * Get or set v:exception. If "oldval" == NULL, return the current value.
21036 * Otherwise, restore the value to "oldval" and return NULL.
21037 * Must always be called in pairs to save and restore v:exception! Does not
21038 * take care of memory allocations.
21039 */
21040 char_u *
21041v_exception(oldval)
21042 char_u *oldval;
21043{
21044 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021045 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046
Bram Moolenaare9a41262005-01-15 22:18:47 +000021047 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 return NULL;
21049}
21050
21051/*
21052 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21053 * Otherwise, restore the value to "oldval" and return NULL.
21054 * Must always be called in pairs to save and restore v:throwpoint! Does not
21055 * take care of memory allocations.
21056 */
21057 char_u *
21058v_throwpoint(oldval)
21059 char_u *oldval;
21060{
21061 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021062 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063
Bram Moolenaare9a41262005-01-15 22:18:47 +000021064 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065 return NULL;
21066}
21067
21068#if defined(FEAT_AUTOCMD) || defined(PROTO)
21069/*
21070 * Set v:cmdarg.
21071 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21072 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21073 * Must always be called in pairs!
21074 */
21075 char_u *
21076set_cmdarg(eap, oldarg)
21077 exarg_T *eap;
21078 char_u *oldarg;
21079{
21080 char_u *oldval;
21081 char_u *newval;
21082 unsigned len;
21083
Bram Moolenaare9a41262005-01-15 22:18:47 +000021084 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021085 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021087 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021088 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021089 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 }
21091
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021092 if (eap->force_bin == FORCE_BIN)
21093 len = 6;
21094 else if (eap->force_bin == FORCE_NOBIN)
21095 len = 8;
21096 else
21097 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021098
21099 if (eap->read_edit)
21100 len += 7;
21101
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021102 if (eap->force_ff != 0)
21103 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21104# ifdef FEAT_MBYTE
21105 if (eap->force_enc != 0)
21106 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021107 if (eap->bad_char != 0)
21108 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021109# endif
21110
21111 newval = alloc(len + 1);
21112 if (newval == NULL)
21113 return NULL;
21114
21115 if (eap->force_bin == FORCE_BIN)
21116 sprintf((char *)newval, " ++bin");
21117 else if (eap->force_bin == FORCE_NOBIN)
21118 sprintf((char *)newval, " ++nobin");
21119 else
21120 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021121
21122 if (eap->read_edit)
21123 STRCAT(newval, " ++edit");
21124
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021125 if (eap->force_ff != 0)
21126 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21127 eap->cmd + eap->force_ff);
21128# ifdef FEAT_MBYTE
21129 if (eap->force_enc != 0)
21130 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21131 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021132 if (eap->bad_char == BAD_KEEP)
21133 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21134 else if (eap->bad_char == BAD_DROP)
21135 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21136 else if (eap->bad_char != 0)
21137 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021138# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021139 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021140 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142#endif
21143
21144/*
21145 * Get the value of internal variable "name".
21146 * Return OK or FAIL.
21147 */
21148 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021149get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 char_u *name;
21151 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021152 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021153 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021154 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021155 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156{
21157 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021158 typval_T *tv = NULL;
21159 typval_T atv;
21160 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162
21163 /* truncate the name, so that we can use strcmp() */
21164 cc = name[len];
21165 name[len] = NUL;
21166
21167 /*
21168 * Check for "b:changedtick".
21169 */
21170 if (STRCMP(name, "b:changedtick") == 0)
21171 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021172 atv.v_type = VAR_NUMBER;
21173 atv.vval.v_number = curbuf->b_changedtick;
21174 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 }
21176
21177 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 * Check for user-defined variables.
21179 */
21180 else
21181 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021182 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021184 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021185 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021186 if (dip != NULL)
21187 *dip = v;
21188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 }
21190
Bram Moolenaare9a41262005-01-15 22:18:47 +000021191 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021193 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021194 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 ret = FAIL;
21196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021198 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199
21200 name[len] = cc;
21201
21202 return ret;
21203}
21204
21205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021206 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21207 * Also handle function call with Funcref variable: func(expr)
21208 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21209 */
21210 static int
21211handle_subscript(arg, rettv, evaluate, verbose)
21212 char_u **arg;
21213 typval_T *rettv;
21214 int evaluate; /* do more than finding the end */
21215 int verbose; /* give error messages */
21216{
21217 int ret = OK;
21218 dict_T *selfdict = NULL;
21219 char_u *s;
21220 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021221 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021222
21223 while (ret == OK
21224 && (**arg == '['
21225 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021226 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021227 && !vim_iswhite(*(*arg - 1)))
21228 {
21229 if (**arg == '(')
21230 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021231 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021232 if (evaluate)
21233 {
21234 functv = *rettv;
21235 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021236
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021237 /* Invoke the function. Recursive! */
21238 s = functv.vval.v_string;
21239 }
21240 else
21241 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021242 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021243 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21244 &len, evaluate, selfdict);
21245
21246 /* Clear the funcref afterwards, so that deleting it while
21247 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021248 if (evaluate)
21249 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021250
21251 /* Stop the expression evaluation when immediately aborting on
21252 * error, or when an interrupt occurred or an exception was thrown
21253 * but not caught. */
21254 if (aborting())
21255 {
21256 if (ret == OK)
21257 clear_tv(rettv);
21258 ret = FAIL;
21259 }
21260 dict_unref(selfdict);
21261 selfdict = NULL;
21262 }
21263 else /* **arg == '[' || **arg == '.' */
21264 {
21265 dict_unref(selfdict);
21266 if (rettv->v_type == VAR_DICT)
21267 {
21268 selfdict = rettv->vval.v_dict;
21269 if (selfdict != NULL)
21270 ++selfdict->dv_refcount;
21271 }
21272 else
21273 selfdict = NULL;
21274 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21275 {
21276 clear_tv(rettv);
21277 ret = FAIL;
21278 }
21279 }
21280 }
21281 dict_unref(selfdict);
21282 return ret;
21283}
21284
21285/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021286 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021287 * value).
21288 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021289 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021290alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291{
Bram Moolenaar33570922005-01-25 22:26:29 +000021292 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021293}
21294
21295/*
21296 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 * The string "s" must have been allocated, it is consumed.
21298 * Return NULL for out of memory, the variable otherwise.
21299 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021301alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 char_u *s;
21303{
Bram Moolenaar33570922005-01-25 22:26:29 +000021304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021306 rettv = alloc_tv();
21307 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021309 rettv->v_type = VAR_STRING;
21310 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 }
21312 else
21313 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021314 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315}
21316
21317/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021318 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021320 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021321free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021322 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323{
21324 if (varp != NULL)
21325 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021326 switch (varp->v_type)
21327 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021328 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021329 func_unref(varp->vval.v_string);
21330 /*FALLTHROUGH*/
21331 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021332 vim_free(varp->vval.v_string);
21333 break;
21334 case VAR_LIST:
21335 list_unref(varp->vval.v_list);
21336 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021337 case VAR_DICT:
21338 dict_unref(varp->vval.v_dict);
21339 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021340 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021341#ifdef FEAT_FLOAT
21342 case VAR_FLOAT:
21343#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021344 case VAR_UNKNOWN:
21345 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021346 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021347 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021348 break;
21349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 vim_free(varp);
21351 }
21352}
21353
21354/*
21355 * Free the memory for a variable value and set the value to NULL or 0.
21356 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021357 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021358clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021359 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360{
21361 if (varp != NULL)
21362 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021363 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021365 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021366 func_unref(varp->vval.v_string);
21367 /*FALLTHROUGH*/
21368 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021369 vim_free(varp->vval.v_string);
21370 varp->vval.v_string = NULL;
21371 break;
21372 case VAR_LIST:
21373 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021374 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021376 case VAR_DICT:
21377 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021378 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021379 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021380 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021381 varp->vval.v_number = 0;
21382 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021383#ifdef FEAT_FLOAT
21384 case VAR_FLOAT:
21385 varp->vval.v_float = 0.0;
21386 break;
21387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021388 case VAR_UNKNOWN:
21389 break;
21390 default:
21391 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021393 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 }
21395}
21396
21397/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 * Set the value of a variable to NULL without freeing items.
21399 */
21400 static void
21401init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021402 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403{
21404 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021405 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406}
21407
21408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 * Get the number value of a variable.
21410 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021411 * For incompatible types, return 0.
21412 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21413 * caller of incompatible types: it sets *denote to TRUE if "denote"
21414 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 */
21416 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021417get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021418 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021420 int error = FALSE;
21421
21422 return get_tv_number_chk(varp, &error); /* return 0L on error */
21423}
21424
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021425 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021426get_tv_number_chk(varp, denote)
21427 typval_T *varp;
21428 int *denote;
21429{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021432 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021434 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021435 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021436#ifdef FEAT_FLOAT
21437 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021438 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021439 break;
21440#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021441 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021442 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021443 break;
21444 case VAR_STRING:
21445 if (varp->vval.v_string != NULL)
21446 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021447 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021448 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021449 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021450 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021451 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021453 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021455 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021456 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021459 if (denote == NULL) /* useful for values that must be unsigned */
21460 n = -1;
21461 else
21462 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021463 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464}
21465
21466/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021467 * Get the lnum from the first argument.
21468 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021469 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 */
21471 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021472get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000021473 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474{
Bram Moolenaar33570922005-01-25 22:26:29 +000021475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 linenr_T lnum;
21477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021478 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 if (lnum == 0) /* no valid number, try using line() */
21480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481 rettv.v_type = VAR_NUMBER;
21482 f_line(argvars, &rettv);
21483 lnum = rettv.vval.v_number;
21484 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 }
21486 return lnum;
21487}
21488
21489/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021490 * Get the lnum from the first argument.
21491 * Also accepts "$", then "buf" is used.
21492 * Returns 0 on error.
21493 */
21494 static linenr_T
21495get_tv_lnum_buf(argvars, buf)
21496 typval_T *argvars;
21497 buf_T *buf;
21498{
21499 if (argvars[0].v_type == VAR_STRING
21500 && argvars[0].vval.v_string != NULL
21501 && argvars[0].vval.v_string[0] == '$'
21502 && buf != NULL)
21503 return buf->b_ml.ml_line_count;
21504 return get_tv_number_chk(&argvars[0], NULL);
21505}
21506
21507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 * Get the string value of a variable.
21509 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021510 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21511 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 * If the String variable has never been set, return an empty string.
21513 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021514 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21515 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 */
21517 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021519 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520{
21521 static char_u mybuf[NUMBUFLEN];
21522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021523 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524}
21525
21526 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000021528 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021529 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021531 char_u *res = get_tv_string_buf_chk(varp, buf);
21532
21533 return res != NULL ? res : (char_u *)"";
21534}
21535
Bram Moolenaar7d647822014-04-05 21:28:56 +020021536/*
21537 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21538 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021539 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021540get_tv_string_chk(varp)
21541 typval_T *varp;
21542{
21543 static char_u mybuf[NUMBUFLEN];
21544
21545 return get_tv_string_buf_chk(varp, mybuf);
21546}
21547
21548 static char_u *
21549get_tv_string_buf_chk(varp, buf)
21550 typval_T *varp;
21551 char_u *buf;
21552{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021553 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021555 case VAR_NUMBER:
21556 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21557 return buf;
21558 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021559 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021560 break;
21561 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021562 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021563 break;
21564 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021565 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021566 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021567#ifdef FEAT_FLOAT
21568 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021569 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021570 break;
21571#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021572 case VAR_STRING:
21573 if (varp->vval.v_string != NULL)
21574 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021575 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021576 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021577 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021580 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581}
21582
21583/*
21584 * Find variable "name" in the list of variables.
21585 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021586 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021587 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021588 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021591find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021593 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021594 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598
Bram Moolenaara7043832005-01-21 11:56:39 +000021599 ht = find_var_ht(name, &varname);
21600 if (htp != NULL)
21601 *htp = ht;
21602 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021604 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605}
21606
21607/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021608 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021609 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021612find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020021614 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000021615 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021616 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000021617{
Bram Moolenaar33570922005-01-25 22:26:29 +000021618 hashitem_T *hi;
21619
21620 if (*varname == NUL)
21621 {
21622 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021623 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021624 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021625 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 case 'g': return &globvars_var;
21627 case 'v': return &vimvars_var;
21628 case 'b': return &curbuf->b_bufvar;
21629 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021630#ifdef FEAT_WINDOWS
21631 case 't': return &curtab->tp_winvar;
21632#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021633 case 'l': return current_funccal == NULL
21634 ? NULL : &current_funccal->l_vars_var;
21635 case 'a': return current_funccal == NULL
21636 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021637 }
21638 return NULL;
21639 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021640
21641 hi = hash_find(ht, varname);
21642 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021643 {
21644 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021645 * worked find the variable again. Don't auto-load a script if it was
21646 * loaded already, otherwise it would be loaded every time when
21647 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021648 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021649 {
21650 /* Note: script_autoload() may make "hi" invalid. It must either
21651 * be obtained again or not used. */
21652 if (!script_autoload(varname, FALSE) || aborting())
21653 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021654 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021655 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021656 if (HASHITEM_EMPTY(hi))
21657 return NULL;
21658 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021659 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021660}
21661
21662/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021664 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021665 * Set "varname" to the start of name without ':'.
21666 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000021668find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 char_u *name;
21670 char_u **varname;
21671{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021672 hashitem_T *hi;
21673
Bram Moolenaar73627d02015-08-11 15:46:09 +020021674 if (name[0] == NUL)
21675 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 if (name[1] != ':')
21677 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021678 /* The name must not start with a colon or #. */
21679 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 return NULL;
21681 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021682
21683 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021684 hi = hash_find(&compat_hashtab, name);
21685 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021686 return &compat_hashtab;
21687
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 return &globvarht; /* global variable */
21690 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021693 if (*name == 'g') /* global variable */
21694 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021695 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21696 */
21697 if (vim_strchr(name + 2, ':') != NULL
21698 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021699 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021701 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021703 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021704#ifdef FEAT_WINDOWS
21705 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021706 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021707#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021708 if (*name == 'v') /* v: variable */
21709 return &vimvarht;
21710 if (*name == 'a' && current_funccal != NULL) /* function argument */
21711 return &current_funccal->l_avars.dv_hashtab;
21712 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21713 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 if (*name == 's' /* script variable */
21715 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21716 return &SCRIPT_VARS(current_SID);
21717 return NULL;
21718}
21719
21720/*
21721 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021722 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 * Returns NULL when it doesn't exist.
21724 */
21725 char_u *
21726get_var_value(name)
21727 char_u *name;
21728{
Bram Moolenaar33570922005-01-25 22:26:29 +000021729 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021731 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 if (v == NULL)
21733 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021734 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735}
21736
21737/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 * sourcing this script and when executing functions defined in the script.
21740 */
21741 void
21742new_script_vars(id)
21743 scid_T id;
21744{
Bram Moolenaara7043832005-01-21 11:56:39 +000021745 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 hashtab_T *ht;
21747 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021748
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21750 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021751 /* Re-allocating ga_data means that an ht_array pointing to
21752 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021754 for (i = 1; i <= ga_scripts.ga_len; ++i)
21755 {
21756 ht = &SCRIPT_VARS(i);
21757 if (ht->ht_mask == HT_INIT_SIZE - 1)
21758 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021759 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021761 }
21762
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 while (ga_scripts.ga_len < id)
21764 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021765 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021766 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021767 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 }
21770 }
21771}
21772
21773/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021774 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21775 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 */
21777 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021778init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 dict_T *dict;
21780 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021781 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782{
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021784 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021785 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021786 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021787 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 dict_var->di_tv.vval.v_dict = dict;
21789 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021790 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21792 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793}
21794
21795/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021796 * Unreference a dictionary initialized by init_var_dict().
21797 */
21798 void
21799unref_var_dict(dict)
21800 dict_T *dict;
21801{
21802 /* Now the dict needs to be freed if no one else is using it, go back to
21803 * normal reference counting. */
21804 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21805 dict_unref(dict);
21806}
21807
21808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 * Frees all allocated variables and the value they contain.
21811 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 */
21813 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021814vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021815 hashtab_T *ht;
21816{
21817 vars_clear_ext(ht, TRUE);
21818}
21819
21820/*
21821 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21822 */
21823 static void
21824vars_clear_ext(ht, free_val)
21825 hashtab_T *ht;
21826 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827{
Bram Moolenaara7043832005-01-21 11:56:39 +000021828 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 hashitem_T *hi;
21830 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021833 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021834 for (hi = ht->ht_array; todo > 0; ++hi)
21835 {
21836 if (!HASHITEM_EMPTY(hi))
21837 {
21838 --todo;
21839
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021841 * ht_array might change then. hash_clear() takes care of it
21842 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 v = HI2DI(hi);
21844 if (free_val)
21845 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020021846 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021848 }
21849 }
21850 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021851 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852}
21853
Bram Moolenaara7043832005-01-21 11:56:39 +000021854/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021855 * Delete a variable from hashtab "ht" at item "hi".
21856 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021859delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021860 hashtab_T *ht;
21861 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862{
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021864
21865 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 clear_tv(&di->di_tv);
21867 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868}
21869
21870/*
21871 * List the value of one internal variable.
21872 */
21873 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021874list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021877 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021879 char_u *tofree;
21880 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021881 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021883 current_copyID += COPYID_INC;
21884 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021886 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021887 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888}
21889
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021891list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 char_u *prefix;
21893 char_u *name;
21894 int type;
21895 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021896 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897{
Bram Moolenaar31859182007-08-14 20:41:13 +000021898 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21899 msg_start();
21900 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 if (name != NULL) /* "a:" vars don't have a name stored */
21902 msg_puts(name);
21903 msg_putchar(' ');
21904 msg_advance(22);
21905 if (type == VAR_NUMBER)
21906 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021907 else if (type == VAR_FUNC)
21908 msg_putchar('*');
21909 else if (type == VAR_LIST)
21910 {
21911 msg_putchar('[');
21912 if (*string == '[')
21913 ++string;
21914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021915 else if (type == VAR_DICT)
21916 {
21917 msg_putchar('{');
21918 if (*string == '{')
21919 ++string;
21920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 else
21922 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021923
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925
21926 if (type == VAR_FUNC)
21927 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021928 if (*first)
21929 {
21930 msg_clr_eos();
21931 *first = FALSE;
21932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933}
21934
21935/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021936 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 * If the variable already exists, the value is updated.
21938 * Otherwise the variable is created.
21939 */
21940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021943 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021944 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945{
Bram Moolenaar33570922005-01-25 22:26:29 +000021946 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021950 ht = find_var_ht(name, &varname);
21951 if (ht == NULL || *varname == NUL)
21952 {
21953 EMSG2(_(e_illvar), name);
21954 return;
21955 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021956 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021957
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021958 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21959 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960
Bram Moolenaar33570922005-01-25 22:26:29 +000021961 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020021964 if (var_check_ro(v->di_flags, name, FALSE)
21965 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 return;
21967 if (v->di_tv.v_type != tv->v_type
21968 && !((v->di_tv.v_type == VAR_STRING
21969 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021970 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021971 || tv->v_type == VAR_NUMBER))
21972#ifdef FEAT_FLOAT
21973 && !((v->di_tv.v_type == VAR_NUMBER
21974 || v->di_tv.v_type == VAR_FLOAT)
21975 && (tv->v_type == VAR_NUMBER
21976 || tv->v_type == VAR_FLOAT))
21977#endif
21978 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021979 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021980 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981 return;
21982 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021983
21984 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021985 * Handle setting internal v: variables separately where needed to
21986 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 */
21988 if (ht == &vimvarht)
21989 {
21990 if (v->di_tv.v_type == VAR_STRING)
21991 {
21992 vim_free(v->di_tv.vval.v_string);
21993 if (copy || tv->v_type != VAR_STRING)
21994 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21995 else
21996 {
21997 /* Take over the string to avoid an extra alloc/free. */
21998 v->di_tv.vval.v_string = tv->vval.v_string;
21999 tv->vval.v_string = NULL;
22000 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022001 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022003 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022004 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022006 if (STRCMP(varname, "searchforward") == 0)
22007 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022008#ifdef FEAT_SEARCH_EXTRA
22009 else if (STRCMP(varname, "hlsearch") == 0)
22010 {
22011 no_hlsearch = !v->di_tv.vval.v_number;
22012 redraw_all_later(SOME_VALID);
22013 }
22014#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022015 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022016 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022017 else if (v->di_tv.v_type != tv->v_type)
22018 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 }
22020
22021 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 }
22023 else /* add a new variable */
22024 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022025 /* Can't add "v:" variable. */
22026 if (ht == &vimvarht)
22027 {
22028 EMSG2(_(e_illvar), name);
22029 return;
22030 }
22031
Bram Moolenaar92124a32005-06-17 22:03:40 +000022032 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022033 if (!valid_varname(varname))
22034 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022035
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022036 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22037 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022038 if (v == NULL)
22039 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022040 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022043 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 return;
22045 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022046 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022048
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022049 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022051 else
22052 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022053 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022054 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022055 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057}
22058
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022060 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 * Also give an error message.
22062 */
22063 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022064var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022065 int flags;
22066 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022067 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022068{
22069 if (flags & DI_FLAGS_RO)
22070 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022071 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 return TRUE;
22073 }
22074 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22075 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022076 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022077 return TRUE;
22078 }
22079 return FALSE;
22080}
22081
22082/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022083 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22084 * Also give an error message.
22085 */
22086 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022087var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022088 int flags;
22089 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022090 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022091{
22092 if (flags & DI_FLAGS_FIX)
22093 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022094 EMSG2(_("E795: Cannot delete variable %s"),
22095 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022096 return TRUE;
22097 }
22098 return FALSE;
22099}
22100
22101/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022102 * Check if a funcref is assigned to a valid variable name.
22103 * Return TRUE and give an error if not.
22104 */
22105 static int
22106var_check_func_name(name, new_var)
22107 char_u *name; /* points to start of variable name */
22108 int new_var; /* TRUE when creating the variable */
22109{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022110 /* Allow for w: b: s: and t:. */
22111 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022112 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22113 ? name[2] : name[0]))
22114 {
22115 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22116 name);
22117 return TRUE;
22118 }
22119 /* Don't allow hiding a function. When "v" is not NULL we might be
22120 * assigning another function to the same var, the type is checked
22121 * below. */
22122 if (new_var && function_exists(name))
22123 {
22124 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22125 name);
22126 return TRUE;
22127 }
22128 return FALSE;
22129}
22130
22131/*
22132 * Check if a variable name is valid.
22133 * Return FALSE and give an error if not.
22134 */
22135 static int
22136valid_varname(varname)
22137 char_u *varname;
22138{
22139 char_u *p;
22140
22141 for (p = varname; *p != NUL; ++p)
22142 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22143 && *p != AUTOLOAD_CHAR)
22144 {
22145 EMSG2(_(e_illvar), varname);
22146 return FALSE;
22147 }
22148 return TRUE;
22149}
22150
22151/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022152 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022153 * Also give an error message, using "name" or _("name") when use_gettext is
22154 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022155 */
22156 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022157tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022158 int lock;
22159 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022160 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022161{
22162 if (lock & VAR_LOCKED)
22163 {
22164 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022165 name == NULL ? (char_u *)_("Unknown")
22166 : use_gettext ? (char_u *)_(name)
22167 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022168 return TRUE;
22169 }
22170 if (lock & VAR_FIXED)
22171 {
22172 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022173 name == NULL ? (char_u *)_("Unknown")
22174 : use_gettext ? (char_u *)_(name)
22175 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022176 return TRUE;
22177 }
22178 return FALSE;
22179}
22180
22181/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022183 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022184 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022185 * It is OK for "from" and "to" to point to the same item. This is used to
22186 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022188 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022189copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 typval_T *from;
22191 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022193 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022194 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022195 switch (from->v_type)
22196 {
22197 case VAR_NUMBER:
22198 to->vval.v_number = from->vval.v_number;
22199 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022200#ifdef FEAT_FLOAT
22201 case VAR_FLOAT:
22202 to->vval.v_float = from->vval.v_float;
22203 break;
22204#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 case VAR_STRING:
22206 case VAR_FUNC:
22207 if (from->vval.v_string == NULL)
22208 to->vval.v_string = NULL;
22209 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022211 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022212 if (from->v_type == VAR_FUNC)
22213 func_ref(to->vval.v_string);
22214 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022215 break;
22216 case VAR_LIST:
22217 if (from->vval.v_list == NULL)
22218 to->vval.v_list = NULL;
22219 else
22220 {
22221 to->vval.v_list = from->vval.v_list;
22222 ++to->vval.v_list->lv_refcount;
22223 }
22224 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022225 case VAR_DICT:
22226 if (from->vval.v_dict == NULL)
22227 to->vval.v_dict = NULL;
22228 else
22229 {
22230 to->vval.v_dict = from->vval.v_dict;
22231 ++to->vval.v_dict->dv_refcount;
22232 }
22233 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022234 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022235 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022236 break;
22237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238}
22239
22240/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022241 * Make a copy of an item.
22242 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022243 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22244 * reference to an already copied list/dict can be used.
22245 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022246 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022247 static int
22248item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022249 typval_T *from;
22250 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022251 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022252 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022253{
22254 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022255 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022256
Bram Moolenaar33570922005-01-25 22:26:29 +000022257 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022258 {
22259 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022260 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 }
22262 ++recurse;
22263
22264 switch (from->v_type)
22265 {
22266 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022267#ifdef FEAT_FLOAT
22268 case VAR_FLOAT:
22269#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022270 case VAR_STRING:
22271 case VAR_FUNC:
22272 copy_tv(from, to);
22273 break;
22274 case VAR_LIST:
22275 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022276 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022277 if (from->vval.v_list == NULL)
22278 to->vval.v_list = NULL;
22279 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22280 {
22281 /* use the copy made earlier */
22282 to->vval.v_list = from->vval.v_list->lv_copylist;
22283 ++to->vval.v_list->lv_refcount;
22284 }
22285 else
22286 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22287 if (to->vval.v_list == NULL)
22288 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022289 break;
22290 case VAR_DICT:
22291 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022292 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022293 if (from->vval.v_dict == NULL)
22294 to->vval.v_dict = NULL;
22295 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22296 {
22297 /* use the copy made earlier */
22298 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22299 ++to->vval.v_dict->dv_refcount;
22300 }
22301 else
22302 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22303 if (to->vval.v_dict == NULL)
22304 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022305 break;
22306 default:
22307 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022309 }
22310 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022311 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022312}
22313
22314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 * ":echo expr1 ..." print each argument separated with a space, add a
22316 * newline at the end.
22317 * ":echon expr1 ..." print each argument plain.
22318 */
22319 void
22320ex_echo(eap)
22321 exarg_T *eap;
22322{
22323 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022325 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 char_u *p;
22327 int needclr = TRUE;
22328 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022329 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330
22331 if (eap->skip)
22332 ++emsg_skip;
22333 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22334 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022335 /* If eval1() causes an error message the text from the command may
22336 * still need to be cleared. E.g., "echo 22,44". */
22337 need_clr_eos = needclr;
22338
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022340 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 {
22342 /*
22343 * Report the invalid expression unless the expression evaluation
22344 * has been cancelled due to an aborting error, an interrupt, or an
22345 * exception.
22346 */
22347 if (!aborting())
22348 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022349 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 break;
22351 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022352 need_clr_eos = FALSE;
22353
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 if (!eap->skip)
22355 {
22356 if (atstart)
22357 {
22358 atstart = FALSE;
22359 /* Call msg_start() after eval1(), evaluating the expression
22360 * may cause a message to appear. */
22361 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022362 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022363 /* Mark the saved text as finishing the line, so that what
22364 * follows is displayed on a new line when scrolling back
22365 * at the more prompt. */
22366 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370 else if (eap->cmdidx == CMD_echo)
22371 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000022372 current_copyID += COPYID_INC;
22373 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022374 if (p != NULL)
22375 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022377 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022379 if (*p != TAB && needclr)
22380 {
22381 /* remove any text still there from the command */
22382 msg_clr_eos();
22383 needclr = FALSE;
22384 }
22385 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 }
22387 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022388 {
22389#ifdef FEAT_MBYTE
22390 if (has_mbyte)
22391 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022392 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022393
22394 (void)msg_outtrans_len_attr(p, i, echo_attr);
22395 p += i - 1;
22396 }
22397 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022399 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022402 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022404 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 arg = skipwhite(arg);
22406 }
22407 eap->nextcmd = check_nextcmd(arg);
22408
22409 if (eap->skip)
22410 --emsg_skip;
22411 else
22412 {
22413 /* remove text that may still be there from the command */
22414 if (needclr)
22415 msg_clr_eos();
22416 if (eap->cmdidx == CMD_echo)
22417 msg_end();
22418 }
22419}
22420
22421/*
22422 * ":echohl {name}".
22423 */
22424 void
22425ex_echohl(eap)
22426 exarg_T *eap;
22427{
22428 int id;
22429
22430 id = syn_name2id(eap->arg);
22431 if (id == 0)
22432 echo_attr = 0;
22433 else
22434 echo_attr = syn_id2attr(id);
22435}
22436
22437/*
22438 * ":execute expr1 ..." execute the result of an expression.
22439 * ":echomsg expr1 ..." Print a message
22440 * ":echoerr expr1 ..." Print an error
22441 * Each gets spaces around each argument and a newline at the end for
22442 * echo commands
22443 */
22444 void
22445ex_execute(eap)
22446 exarg_T *eap;
22447{
22448 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 int ret = OK;
22451 char_u *p;
22452 garray_T ga;
22453 int len;
22454 int save_did_emsg;
22455
22456 ga_init2(&ga, 1, 80);
22457
22458 if (eap->skip)
22459 ++emsg_skip;
22460 while (*arg != NUL && *arg != '|' && *arg != '\n')
22461 {
22462 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022463 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 {
22465 /*
22466 * Report the invalid expression unless the expression evaluation
22467 * has been cancelled due to an aborting error, an interrupt, or an
22468 * exception.
22469 */
22470 if (!aborting())
22471 EMSG2(_(e_invexpr2), p);
22472 ret = FAIL;
22473 break;
22474 }
22475
22476 if (!eap->skip)
22477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022478 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 len = (int)STRLEN(p);
22480 if (ga_grow(&ga, len + 2) == FAIL)
22481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483 ret = FAIL;
22484 break;
22485 }
22486 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 ga.ga_len += len;
22490 }
22491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 arg = skipwhite(arg);
22494 }
22495
22496 if (ret != FAIL && ga.ga_data != NULL)
22497 {
22498 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022501 out_flush();
22502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 else if (eap->cmdidx == CMD_echoerr)
22504 {
22505 /* We don't want to abort following commands, restore did_emsg. */
22506 save_did_emsg = did_emsg;
22507 EMSG((char_u *)ga.ga_data);
22508 if (!force_abort)
22509 did_emsg = save_did_emsg;
22510 }
22511 else if (eap->cmdidx == CMD_execute)
22512 do_cmdline((char_u *)ga.ga_data,
22513 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22514 }
22515
22516 ga_clear(&ga);
22517
22518 if (eap->skip)
22519 --emsg_skip;
22520
22521 eap->nextcmd = check_nextcmd(arg);
22522}
22523
22524/*
22525 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22526 * "arg" points to the "&" or '+' when called, to "option" when returning.
22527 * Returns NULL when no option name found. Otherwise pointer to the char
22528 * after the option name.
22529 */
22530 static char_u *
22531find_option_end(arg, opt_flags)
22532 char_u **arg;
22533 int *opt_flags;
22534{
22535 char_u *p = *arg;
22536
22537 ++p;
22538 if (*p == 'g' && p[1] == ':')
22539 {
22540 *opt_flags = OPT_GLOBAL;
22541 p += 2;
22542 }
22543 else if (*p == 'l' && p[1] == ':')
22544 {
22545 *opt_flags = OPT_LOCAL;
22546 p += 2;
22547 }
22548 else
22549 *opt_flags = 0;
22550
22551 if (!ASCII_ISALPHA(*p))
22552 return NULL;
22553 *arg = p;
22554
22555 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22556 p += 4; /* termcap option */
22557 else
22558 while (ASCII_ISALPHA(*p))
22559 ++p;
22560 return p;
22561}
22562
22563/*
22564 * ":function"
22565 */
22566 void
22567ex_function(eap)
22568 exarg_T *eap;
22569{
22570 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022571 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 int j;
22573 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022575 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 char_u *name = NULL;
22577 char_u *p;
22578 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022579 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 garray_T newargs;
22581 garray_T newlines;
22582 int varargs = FALSE;
22583 int mustend = FALSE;
22584 int flags = 0;
22585 ufunc_T *fp;
22586 int indent;
22587 int nesting;
22588 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 dictitem_T *v;
22590 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022591 static int func_nr = 0; /* number for nameless function */
22592 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022593 hashtab_T *ht;
22594 int todo;
22595 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022596 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597
22598 /*
22599 * ":function" without argument: list functions.
22600 */
22601 if (ends_excmd(*eap->arg))
22602 {
22603 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022604 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022605 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022606 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022607 {
22608 if (!HASHITEM_EMPTY(hi))
22609 {
22610 --todo;
22611 fp = HI2UF(hi);
22612 if (!isdigit(*fp->uf_name))
22613 list_func_head(fp, FALSE);
22614 }
22615 }
22616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 eap->nextcmd = check_nextcmd(eap->arg);
22618 return;
22619 }
22620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022621 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022622 * ":function /pat": list functions matching pattern.
22623 */
22624 if (*eap->arg == '/')
22625 {
22626 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22627 if (!eap->skip)
22628 {
22629 regmatch_T regmatch;
22630
22631 c = *p;
22632 *p = NUL;
22633 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22634 *p = c;
22635 if (regmatch.regprog != NULL)
22636 {
22637 regmatch.rm_ic = p_ic;
22638
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022639 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022640 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22641 {
22642 if (!HASHITEM_EMPTY(hi))
22643 {
22644 --todo;
22645 fp = HI2UF(hi);
22646 if (!isdigit(*fp->uf_name)
22647 && vim_regexec(&regmatch, fp->uf_name, 0))
22648 list_func_head(fp, FALSE);
22649 }
22650 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022651 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022652 }
22653 }
22654 if (*p == '/')
22655 ++p;
22656 eap->nextcmd = check_nextcmd(p);
22657 return;
22658 }
22659
22660 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022661 * Get the function name. There are these situations:
22662 * func normal function name
22663 * "name" == func, "fudi.fd_dict" == NULL
22664 * dict.func new dictionary entry
22665 * "name" == NULL, "fudi.fd_dict" set,
22666 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22667 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022668 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022669 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22670 * dict.func existing dict entry that's not a Funcref
22671 * "name" == NULL, "fudi.fd_dict" set,
22672 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022673 * s:func script-local function name
22674 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 name = trans_function_name(&p, eap->skip, 0, &fudi);
22678 paren = (vim_strchr(p, '(') != NULL);
22679 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 {
22681 /*
22682 * Return on an invalid expression in braces, unless the expression
22683 * evaluation has been cancelled due to an aborting error, an
22684 * interrupt, or an exception.
22685 */
22686 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022688 if (!eap->skip && fudi.fd_newkey != NULL)
22689 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022690 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 else
22694 eap->skip = TRUE;
22695 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022696
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 /* An error in a function call during evaluation of an expression in magic
22698 * braces should not cause the function not to be defined. */
22699 saved_did_emsg = did_emsg;
22700 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701
22702 /*
22703 * ":function func" with only function name: list function.
22704 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022705 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
22707 if (!ends_excmd(*skipwhite(p)))
22708 {
22709 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022710 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 }
22712 eap->nextcmd = check_nextcmd(p);
22713 if (eap->nextcmd != NULL)
22714 *p = NUL;
22715 if (!eap->skip && !got_int)
22716 {
22717 fp = find_func(name);
22718 if (fp != NULL)
22719 {
22720 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022721 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022723 if (FUNCLINE(fp, j) == NULL)
22724 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 msg_putchar('\n');
22726 msg_outnum((long)(j + 1));
22727 if (j < 9)
22728 msg_putchar(' ');
22729 if (j < 99)
22730 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022731 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 out_flush(); /* show a line at a time */
22733 ui_breakcheck();
22734 }
22735 if (!got_int)
22736 {
22737 msg_putchar('\n');
22738 msg_puts((char_u *)" endfunction");
22739 }
22740 }
22741 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022742 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 }
22746
22747 /*
22748 * ":function name(arg1, arg2)" Define function.
22749 */
22750 p = skipwhite(p);
22751 if (*p != '(')
22752 {
22753 if (!eap->skip)
22754 {
22755 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022756 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 }
22758 /* attempt to continue by skipping some text */
22759 if (vim_strchr(p, '(') != NULL)
22760 p = vim_strchr(p, '(');
22761 }
22762 p = skipwhite(p + 1);
22763
22764 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22765 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22766
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022767 if (!eap->skip)
22768 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022769 /* Check the name of the function. Unless it's a dictionary function
22770 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022771 if (name != NULL)
22772 arg = name;
22773 else
22774 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022775 if (arg != NULL && (fudi.fd_di == NULL
22776 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022777 {
22778 if (*arg == K_SPECIAL)
22779 j = 3;
22780 else
22781 j = 0;
22782 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22783 : eval_isnamec(arg[j])))
22784 ++j;
22785 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022786 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022787 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022788 /* Disallow using the g: dict. */
22789 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22790 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022791 }
22792
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 /*
22794 * Isolate the arguments: "arg1, arg2, ...)"
22795 */
22796 while (*p != ')')
22797 {
22798 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22799 {
22800 varargs = TRUE;
22801 p += 3;
22802 mustend = TRUE;
22803 }
22804 else
22805 {
22806 arg = p;
22807 while (ASCII_ISALNUM(*p) || *p == '_')
22808 ++p;
22809 if (arg == p || isdigit(*arg)
22810 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22811 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22812 {
22813 if (!eap->skip)
22814 EMSG2(_("E125: Illegal argument: %s"), arg);
22815 break;
22816 }
22817 if (ga_grow(&newargs, 1) == FAIL)
22818 goto erret;
22819 c = *p;
22820 *p = NUL;
22821 arg = vim_strsave(arg);
22822 if (arg == NULL)
22823 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022824
22825 /* Check for duplicate argument name. */
22826 for (i = 0; i < newargs.ga_len; ++i)
22827 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22828 {
22829 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022830 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022831 goto erret;
22832 }
22833
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22835 *p = c;
22836 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 if (*p == ',')
22838 ++p;
22839 else
22840 mustend = TRUE;
22841 }
22842 p = skipwhite(p);
22843 if (mustend && *p != ')')
22844 {
22845 if (!eap->skip)
22846 EMSG2(_(e_invarg2), eap->arg);
22847 break;
22848 }
22849 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020022850 if (*p != ')')
22851 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 ++p; /* skip the ')' */
22853
Bram Moolenaare9a41262005-01-15 22:18:47 +000022854 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 for (;;)
22856 {
22857 p = skipwhite(p);
22858 if (STRNCMP(p, "range", 5) == 0)
22859 {
22860 flags |= FC_RANGE;
22861 p += 5;
22862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022863 else if (STRNCMP(p, "dict", 4) == 0)
22864 {
22865 flags |= FC_DICT;
22866 p += 4;
22867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 else if (STRNCMP(p, "abort", 5) == 0)
22869 {
22870 flags |= FC_ABORT;
22871 p += 5;
22872 }
22873 else
22874 break;
22875 }
22876
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022877 /* When there is a line break use what follows for the function body.
22878 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22879 if (*p == '\n')
22880 line_arg = p + 1;
22881 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 EMSG(_(e_trailing));
22883
22884 /*
22885 * Read the body of the function, until ":endfunction" is found.
22886 */
22887 if (KeyTyped)
22888 {
22889 /* Check if the function already exists, don't let the user type the
22890 * whole function before telling him it doesn't work! For a script we
22891 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 if (!eap->skip && !eap->forceit)
22893 {
22894 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22895 EMSG(_(e_funcdict));
22896 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022897 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022900 if (!eap->skip && did_emsg)
22901 goto erret;
22902
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 msg_putchar('\n'); /* don't overwrite the function name */
22904 cmdline_row = msg_row;
22905 }
22906
22907 indent = 2;
22908 nesting = 0;
22909 for (;;)
22910 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022911 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022912 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022913 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022914 saved_wait_return = FALSE;
22915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022917 sourcing_lnum_off = sourcing_lnum;
22918
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022919 if (line_arg != NULL)
22920 {
22921 /* Use eap->arg, split up in parts by line breaks. */
22922 theline = line_arg;
22923 p = vim_strchr(theline, '\n');
22924 if (p == NULL)
22925 line_arg += STRLEN(line_arg);
22926 else
22927 {
22928 *p = NUL;
22929 line_arg = p + 1;
22930 }
22931 }
22932 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 theline = getcmdline(':', 0L, indent);
22934 else
22935 theline = eap->getline(':', eap->cookie, indent);
22936 if (KeyTyped)
22937 lines_left = Rows - 1;
22938 if (theline == NULL)
22939 {
22940 EMSG(_("E126: Missing :endfunction"));
22941 goto erret;
22942 }
22943
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022944 /* Detect line continuation: sourcing_lnum increased more than one. */
22945 if (sourcing_lnum > sourcing_lnum_off + 1)
22946 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22947 else
22948 sourcing_lnum_off = 0;
22949
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 if (skip_until != NULL)
22951 {
22952 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22953 * don't check for ":endfunc". */
22954 if (STRCMP(theline, skip_until) == 0)
22955 {
22956 vim_free(skip_until);
22957 skip_until = NULL;
22958 }
22959 }
22960 else
22961 {
22962 /* skip ':' and blanks*/
22963 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22964 ;
22965
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022966 /* Check for "endfunction". */
22967 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022969 if (line_arg == NULL)
22970 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 break;
22972 }
22973
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022974 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 * at "end". */
22976 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22977 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022978 else if (STRNCMP(p, "if", 2) == 0
22979 || STRNCMP(p, "wh", 2) == 0
22980 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 || STRNCMP(p, "try", 3) == 0)
22982 indent += 2;
22983
22984 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022985 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022987 if (*p == '!')
22988 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022990 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22991 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022993 ++nesting;
22994 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 }
22996 }
22997
22998 /* Check for ":append" or ":insert". */
22999 p = skip_range(p, NULL);
23000 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23001 || (p[0] == 'i'
23002 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23003 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23004 skip_until = vim_strsave((char_u *)".");
23005
23006 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23007 arg = skipwhite(skiptowhite(p));
23008 if (arg[0] == '<' && arg[1] =='<'
23009 && ((p[0] == 'p' && p[1] == 'y'
23010 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23011 || (p[0] == 'p' && p[1] == 'e'
23012 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23013 || (p[0] == 't' && p[1] == 'c'
23014 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023015 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23016 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23018 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023019 || (p[0] == 'm' && p[1] == 'z'
23020 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 ))
23022 {
23023 /* ":python <<" continues until a dot, like ":append" */
23024 p = skipwhite(arg + 2);
23025 if (*p == NUL)
23026 skip_until = vim_strsave((char_u *)".");
23027 else
23028 skip_until = vim_strsave(p);
23029 }
23030 }
23031
23032 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023033 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023034 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023035 if (line_arg == NULL)
23036 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023038 }
23039
23040 /* Copy the line to newly allocated memory. get_one_sourceline()
23041 * allocates 250 bytes per line, this saves 80% on average. The cost
23042 * is an extra alloc/free. */
23043 p = vim_strsave(theline);
23044 if (p != NULL)
23045 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023046 if (line_arg == NULL)
23047 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023048 theline = p;
23049 }
23050
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023051 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23052
23053 /* Add NULL lines for continuation lines, so that the line count is
23054 * equal to the index in the growarray. */
23055 while (sourcing_lnum_off-- > 0)
23056 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023057
23058 /* Check for end of eap->arg. */
23059 if (line_arg != NULL && *line_arg == NUL)
23060 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 }
23062
23063 /* Don't define the function when skipping commands or when an error was
23064 * detected. */
23065 if (eap->skip || did_emsg)
23066 goto erret;
23067
23068 /*
23069 * If there are no errors, add the function
23070 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023071 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023072 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023073 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023075 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023076 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023077 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023078 goto erret;
23079 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023081 fp = find_func(name);
23082 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023084 if (!eap->forceit)
23085 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023086 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023087 goto erret;
23088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023089 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023090 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023091 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023092 name);
23093 goto erret;
23094 }
23095 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023096 ga_clear_strings(&(fp->uf_args));
23097 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023098 vim_free(name);
23099 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 }
23102 else
23103 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023104 char numbuf[20];
23105
23106 fp = NULL;
23107 if (fudi.fd_newkey == NULL && !eap->forceit)
23108 {
23109 EMSG(_(e_funcdict));
23110 goto erret;
23111 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023112 if (fudi.fd_di == NULL)
23113 {
23114 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023115 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023116 goto erret;
23117 }
23118 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023119 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023120 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023121
23122 /* Give the function a sequential number. Can only be used with a
23123 * Funcref! */
23124 vim_free(name);
23125 sprintf(numbuf, "%d", ++func_nr);
23126 name = vim_strsave((char_u *)numbuf);
23127 if (name == NULL)
23128 goto erret;
23129 }
23130
23131 if (fp == NULL)
23132 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023133 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023134 {
23135 int slen, plen;
23136 char_u *scriptname;
23137
23138 /* Check that the autoload name matches the script name. */
23139 j = FAIL;
23140 if (sourcing_name != NULL)
23141 {
23142 scriptname = autoload_name(name);
23143 if (scriptname != NULL)
23144 {
23145 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023146 plen = (int)STRLEN(p);
23147 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023148 if (slen > plen && fnamecmp(p,
23149 sourcing_name + slen - plen) == 0)
23150 j = OK;
23151 vim_free(scriptname);
23152 }
23153 }
23154 if (j == FAIL)
23155 {
23156 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23157 goto erret;
23158 }
23159 }
23160
23161 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 if (fp == NULL)
23163 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023164
23165 if (fudi.fd_dict != NULL)
23166 {
23167 if (fudi.fd_di == NULL)
23168 {
23169 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023170 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023171 if (fudi.fd_di == NULL)
23172 {
23173 vim_free(fp);
23174 goto erret;
23175 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023176 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23177 {
23178 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023179 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023180 goto erret;
23181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023182 }
23183 else
23184 /* overwrite existing dict entry */
23185 clear_tv(&fudi.fd_di->di_tv);
23186 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023187 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023188 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023189 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023190
23191 /* behave like "dict" was used */
23192 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023193 }
23194
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023196 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023197 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23198 {
23199 vim_free(fp);
23200 goto erret;
23201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023203 fp->uf_args = newargs;
23204 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023205#ifdef FEAT_PROFILE
23206 fp->uf_tml_count = NULL;
23207 fp->uf_tml_total = NULL;
23208 fp->uf_tml_self = NULL;
23209 fp->uf_profiling = FALSE;
23210 if (prof_def_func())
23211 func_do_profile(fp);
23212#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023213 fp->uf_varargs = varargs;
23214 fp->uf_flags = flags;
23215 fp->uf_calls = 0;
23216 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218
23219erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 ga_clear_strings(&newargs);
23221 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023222ret_free:
23223 vim_free(skip_until);
23224 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023227 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228}
23229
23230/*
23231 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023232 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023234 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023235 * TFN_INT: internal function name OK
23236 * TFN_QUIET: be quiet
23237 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 * Advances "pp" to just after the function name (if no error).
23239 */
23240 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023241trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 char_u **pp;
23243 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023244 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023247 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 char_u *start;
23249 char_u *end;
23250 int lead;
23251 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023253 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023254
23255 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023256 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023258
23259 /* Check for hard coded <SNR>: already translated function ID (from a user
23260 * command). */
23261 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23262 && (*pp)[2] == (int)KE_SNR)
23263 {
23264 *pp += 3;
23265 len = get_id_len(pp) + 3;
23266 return vim_strnsave(start, len);
23267 }
23268
23269 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23270 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023272 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023274
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023275 /* Note that TFN_ flags use the same values as GLV_ flags. */
23276 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023277 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023278 if (end == start)
23279 {
23280 if (!skip)
23281 EMSG(_("E129: Function name required"));
23282 goto theend;
23283 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023284 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023285 {
23286 /*
23287 * Report an invalid expression in braces, unless the expression
23288 * evaluation has been cancelled due to an aborting error, an
23289 * interrupt, or an exception.
23290 */
23291 if (!aborting())
23292 {
23293 if (end != NULL)
23294 EMSG2(_(e_invarg2), start);
23295 }
23296 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023297 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023298 goto theend;
23299 }
23300
23301 if (lv.ll_tv != NULL)
23302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023303 if (fdp != NULL)
23304 {
23305 fdp->fd_dict = lv.ll_dict;
23306 fdp->fd_newkey = lv.ll_newkey;
23307 lv.ll_newkey = NULL;
23308 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023309 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023310 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23311 {
23312 name = vim_strsave(lv.ll_tv->vval.v_string);
23313 *pp = end;
23314 }
23315 else
23316 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023317 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23318 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023319 EMSG(_(e_funcref));
23320 else
23321 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023322 name = NULL;
23323 }
23324 goto theend;
23325 }
23326
23327 if (lv.ll_name == NULL)
23328 {
23329 /* Error found, but continue after the function name. */
23330 *pp = end;
23331 goto theend;
23332 }
23333
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023334 /* Check if the name is a Funcref. If so, use the value. */
23335 if (lv.ll_exp_name != NULL)
23336 {
23337 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023338 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023339 if (name == lv.ll_exp_name)
23340 name = NULL;
23341 }
23342 else
23343 {
23344 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023345 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023346 if (name == *pp)
23347 name = NULL;
23348 }
23349 if (name != NULL)
23350 {
23351 name = vim_strsave(name);
23352 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023353 if (STRNCMP(name, "<SNR>", 5) == 0)
23354 {
23355 /* Change "<SNR>" to the byte sequence. */
23356 name[0] = K_SPECIAL;
23357 name[1] = KS_EXTRA;
23358 name[2] = (int)KE_SNR;
23359 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23360 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023361 goto theend;
23362 }
23363
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023364 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023365 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023366 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023367 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23368 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23369 {
23370 /* When there was "s:" already or the name expanded to get a
23371 * leading "s:" then remove it. */
23372 lv.ll_name += 2;
23373 len -= 2;
23374 lead = 2;
23375 }
23376 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023377 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023378 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023379 /* skip over "s:" and "g:" */
23380 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023381 lv.ll_name += 2;
23382 len = (int)(end - lv.ll_name);
23383 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023384
23385 /*
23386 * Copy the function name to allocated memory.
23387 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23388 * Accept <SNR>123_name() outside a script.
23389 */
23390 if (skip)
23391 lead = 0; /* do nothing */
23392 else if (lead > 0)
23393 {
23394 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023395 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23396 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023397 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023398 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023399 if (current_SID <= 0)
23400 {
23401 EMSG(_(e_usingsid));
23402 goto theend;
23403 }
23404 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23405 lead += (int)STRLEN(sid_buf);
23406 }
23407 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023408 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023409 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023410 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023411 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023412 goto theend;
23413 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023414 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023415 {
23416 char_u *cp = vim_strchr(lv.ll_name, ':');
23417
23418 if (cp != NULL && cp < end)
23419 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023420 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023421 goto theend;
23422 }
23423 }
23424
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023425 name = alloc((unsigned)(len + lead + 1));
23426 if (name != NULL)
23427 {
23428 if (lead > 0)
23429 {
23430 name[0] = K_SPECIAL;
23431 name[1] = KS_EXTRA;
23432 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023433 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023434 STRCPY(name + 3, sid_buf);
23435 }
23436 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023437 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023438 }
23439 *pp = end;
23440
23441theend:
23442 clear_lval(&lv);
23443 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444}
23445
23446/*
23447 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23448 * Return 2 if "p" starts with "s:".
23449 * Return 0 otherwise.
23450 */
23451 static int
23452eval_fname_script(p)
23453 char_u *p;
23454{
23455 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
23456 || STRNICMP(p + 1, "SNR>", 4) == 0))
23457 return 5;
23458 if (p[0] == 's' && p[1] == ':')
23459 return 2;
23460 return 0;
23461}
23462
23463/*
23464 * Return TRUE if "p" starts with "<SID>" or "s:".
23465 * Only works if eval_fname_script() returned non-zero for "p"!
23466 */
23467 static int
23468eval_fname_sid(p)
23469 char_u *p;
23470{
23471 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23472}
23473
23474/*
23475 * List the head of the function: "name(arg1, arg2)".
23476 */
23477 static void
23478list_func_head(fp, indent)
23479 ufunc_T *fp;
23480 int indent;
23481{
23482 int j;
23483
23484 msg_start();
23485 if (indent)
23486 MSG_PUTS(" ");
23487 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023488 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 {
23490 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023491 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 }
23493 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023494 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023496 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 {
23498 if (j)
23499 MSG_PUTS(", ");
23500 msg_puts(FUNCARG(fp, j));
23501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023502 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 {
23504 if (j)
23505 MSG_PUTS(", ");
23506 MSG_PUTS("...");
23507 }
23508 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023509 if (fp->uf_flags & FC_ABORT)
23510 MSG_PUTS(" abort");
23511 if (fp->uf_flags & FC_RANGE)
23512 MSG_PUTS(" range");
23513 if (fp->uf_flags & FC_DICT)
23514 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023515 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023516 if (p_verbose > 0)
23517 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518}
23519
23520/*
23521 * Find a function by name, return pointer to it in ufuncs.
23522 * Return NULL for unknown function.
23523 */
23524 static ufunc_T *
23525find_func(name)
23526 char_u *name;
23527{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023528 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023530 hi = hash_find(&func_hashtab, name);
23531 if (!HASHITEM_EMPTY(hi))
23532 return HI2UF(hi);
23533 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534}
23535
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023536#if defined(EXITFREE) || defined(PROTO)
23537 void
23538free_all_functions()
23539{
23540 hashitem_T *hi;
23541
23542 /* Need to start all over every time, because func_free() may change the
23543 * hash table. */
23544 while (func_hashtab.ht_used > 0)
23545 for (hi = func_hashtab.ht_array; ; ++hi)
23546 if (!HASHITEM_EMPTY(hi))
23547 {
23548 func_free(HI2UF(hi));
23549 break;
23550 }
23551}
23552#endif
23553
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023554 int
23555translated_function_exists(name)
23556 char_u *name;
23557{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023558 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023559 return find_internal_func(name) >= 0;
23560 return find_func(name) != NULL;
23561}
23562
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023563/*
23564 * Return TRUE if a function "name" exists.
23565 */
23566 static int
23567function_exists(name)
23568 char_u *name;
23569{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023570 char_u *nm = name;
23571 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023572 int n = FALSE;
23573
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023574 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23575 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023576 nm = skipwhite(nm);
23577
23578 /* Only accept "funcname", "funcname ", "funcname (..." and
23579 * "funcname(...", not "funcname!...". */
23580 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023581 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023582 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023583 return n;
23584}
23585
Bram Moolenaara1544c02013-05-30 12:35:52 +020023586 char_u *
23587get_expanded_name(name, check)
23588 char_u *name;
23589 int check;
23590{
23591 char_u *nm = name;
23592 char_u *p;
23593
23594 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23595
23596 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023597 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023598 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023599
Bram Moolenaara1544c02013-05-30 12:35:52 +020023600 vim_free(p);
23601 return NULL;
23602}
23603
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023604/*
23605 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023606 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23607 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023608 */
23609 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023610builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023611 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023612 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023613{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023614 char_u *p;
23615
23616 if (!ASCII_ISLOWER(name[0]))
23617 return FALSE;
23618 p = vim_strchr(name, AUTOLOAD_CHAR);
23619 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023620}
23621
Bram Moolenaar05159a02005-02-26 23:04:13 +000023622#if defined(FEAT_PROFILE) || defined(PROTO)
23623/*
23624 * Start profiling function "fp".
23625 */
23626 static void
23627func_do_profile(fp)
23628 ufunc_T *fp;
23629{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023630 int len = fp->uf_lines.ga_len;
23631
23632 if (len == 0)
23633 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023634 fp->uf_tm_count = 0;
23635 profile_zero(&fp->uf_tm_self);
23636 profile_zero(&fp->uf_tm_total);
23637 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023638 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023639 if (fp->uf_tml_total == NULL)
23640 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023641 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023642 if (fp->uf_tml_self == NULL)
23643 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023644 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023645 fp->uf_tml_idx = -1;
23646 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23647 || fp->uf_tml_self == NULL)
23648 return; /* out of memory */
23649
23650 fp->uf_profiling = TRUE;
23651}
23652
23653/*
23654 * Dump the profiling results for all functions in file "fd".
23655 */
23656 void
23657func_dump_profile(fd)
23658 FILE *fd;
23659{
23660 hashitem_T *hi;
23661 int todo;
23662 ufunc_T *fp;
23663 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023664 ufunc_T **sorttab;
23665 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023667 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023668 if (todo == 0)
23669 return; /* nothing to dump */
23670
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023671 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023672
Bram Moolenaar05159a02005-02-26 23:04:13 +000023673 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23674 {
23675 if (!HASHITEM_EMPTY(hi))
23676 {
23677 --todo;
23678 fp = HI2UF(hi);
23679 if (fp->uf_profiling)
23680 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023681 if (sorttab != NULL)
23682 sorttab[st_len++] = fp;
23683
Bram Moolenaar05159a02005-02-26 23:04:13 +000023684 if (fp->uf_name[0] == K_SPECIAL)
23685 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23686 else
23687 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23688 if (fp->uf_tm_count == 1)
23689 fprintf(fd, "Called 1 time\n");
23690 else
23691 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23692 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23693 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23694 fprintf(fd, "\n");
23695 fprintf(fd, "count total (s) self (s)\n");
23696
23697 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23698 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023699 if (FUNCLINE(fp, i) == NULL)
23700 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023701 prof_func_line(fd, fp->uf_tml_count[i],
23702 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023703 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23704 }
23705 fprintf(fd, "\n");
23706 }
23707 }
23708 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023709
23710 if (sorttab != NULL && st_len > 0)
23711 {
23712 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23713 prof_total_cmp);
23714 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23715 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23716 prof_self_cmp);
23717 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23718 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023719
23720 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023721}
Bram Moolenaar73830342005-02-28 22:48:19 +000023722
23723 static void
23724prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23725 FILE *fd;
23726 ufunc_T **sorttab;
23727 int st_len;
23728 char *title;
23729 int prefer_self; /* when equal print only self time */
23730{
23731 int i;
23732 ufunc_T *fp;
23733
23734 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23735 fprintf(fd, "count total (s) self (s) function\n");
23736 for (i = 0; i < 20 && i < st_len; ++i)
23737 {
23738 fp = sorttab[i];
23739 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23740 prefer_self);
23741 if (fp->uf_name[0] == K_SPECIAL)
23742 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23743 else
23744 fprintf(fd, " %s()\n", fp->uf_name);
23745 }
23746 fprintf(fd, "\n");
23747}
23748
23749/*
23750 * Print the count and times for one function or function line.
23751 */
23752 static void
23753prof_func_line(fd, count, total, self, prefer_self)
23754 FILE *fd;
23755 int count;
23756 proftime_T *total;
23757 proftime_T *self;
23758 int prefer_self; /* when equal print only self time */
23759{
23760 if (count > 0)
23761 {
23762 fprintf(fd, "%5d ", count);
23763 if (prefer_self && profile_equal(total, self))
23764 fprintf(fd, " ");
23765 else
23766 fprintf(fd, "%s ", profile_msg(total));
23767 if (!prefer_self && profile_equal(total, self))
23768 fprintf(fd, " ");
23769 else
23770 fprintf(fd, "%s ", profile_msg(self));
23771 }
23772 else
23773 fprintf(fd, " ");
23774}
23775
23776/*
23777 * Compare function for total time sorting.
23778 */
23779 static int
23780#ifdef __BORLANDC__
23781_RTLENTRYF
23782#endif
23783prof_total_cmp(s1, s2)
23784 const void *s1;
23785 const void *s2;
23786{
23787 ufunc_T *p1, *p2;
23788
23789 p1 = *(ufunc_T **)s1;
23790 p2 = *(ufunc_T **)s2;
23791 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23792}
23793
23794/*
23795 * Compare function for self time sorting.
23796 */
23797 static int
23798#ifdef __BORLANDC__
23799_RTLENTRYF
23800#endif
23801prof_self_cmp(s1, s2)
23802 const void *s1;
23803 const void *s2;
23804{
23805 ufunc_T *p1, *p2;
23806
23807 p1 = *(ufunc_T **)s1;
23808 p2 = *(ufunc_T **)s2;
23809 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23810}
23811
Bram Moolenaar05159a02005-02-26 23:04:13 +000023812#endif
23813
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023814/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023815 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023816 * Return TRUE if a package was loaded.
23817 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023818 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023819script_autoload(name, reload)
23820 char_u *name;
23821 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023822{
23823 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023824 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023825 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023826 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023827
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023828 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023829 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023830 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023831 return FALSE;
23832
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023833 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023834
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023835 /* Find the name in the list of previously loaded package names. Skip
23836 * "autoload/", it's always the same. */
23837 for (i = 0; i < ga_loaded.ga_len; ++i)
23838 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23839 break;
23840 if (!reload && i < ga_loaded.ga_len)
23841 ret = FALSE; /* was loaded already */
23842 else
23843 {
23844 /* Remember the name if it wasn't loaded already. */
23845 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23846 {
23847 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23848 tofree = NULL;
23849 }
23850
23851 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023852 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023853 ret = TRUE;
23854 }
23855
23856 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023857 return ret;
23858}
23859
23860/*
23861 * Return the autoload script name for a function or variable name.
23862 * Returns NULL when out of memory.
23863 */
23864 static char_u *
23865autoload_name(name)
23866 char_u *name;
23867{
23868 char_u *p;
23869 char_u *scriptname;
23870
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023871 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023872 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23873 if (scriptname == NULL)
23874 return FALSE;
23875 STRCPY(scriptname, "autoload/");
23876 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023877 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023878 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023879 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023880 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023881 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023882}
23883
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23885
23886/*
23887 * Function given to ExpandGeneric() to obtain the list of user defined
23888 * function names.
23889 */
23890 char_u *
23891get_user_func_name(xp, idx)
23892 expand_T *xp;
23893 int idx;
23894{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 static long_u done;
23896 static hashitem_T *hi;
23897 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898
23899 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023901 done = 0;
23902 hi = func_hashtab.ht_array;
23903 }
23904 if (done < func_hashtab.ht_used)
23905 {
23906 if (done++ > 0)
23907 ++hi;
23908 while (HASHITEM_EMPTY(hi))
23909 ++hi;
23910 fp = HI2UF(hi);
23911
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023912 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023913 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023914
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023915 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23916 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917
23918 cat_func_name(IObuff, fp);
23919 if (xp->xp_context != EXPAND_USER_FUNC)
23920 {
23921 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023922 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 STRCAT(IObuff, ")");
23924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 return IObuff;
23926 }
23927 return NULL;
23928}
23929
23930#endif /* FEAT_CMDL_COMPL */
23931
23932/*
23933 * Copy the function name of "fp" to buffer "buf".
23934 * "buf" must be able to hold the function name plus three bytes.
23935 * Takes care of script-local function names.
23936 */
23937 static void
23938cat_func_name(buf, fp)
23939 char_u *buf;
23940 ufunc_T *fp;
23941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023942 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 {
23944 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023945 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 }
23947 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023948 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949}
23950
23951/*
23952 * ":delfunction {name}"
23953 */
23954 void
23955ex_delfunction(eap)
23956 exarg_T *eap;
23957{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023958 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 char_u *p;
23960 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023961 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962
23963 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023964 name = trans_function_name(&p, eap->skip, 0, &fudi);
23965 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023967 {
23968 if (fudi.fd_dict != NULL && !eap->skip)
23969 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 if (!ends_excmd(*skipwhite(p)))
23973 {
23974 vim_free(name);
23975 EMSG(_(e_trailing));
23976 return;
23977 }
23978 eap->nextcmd = check_nextcmd(p);
23979 if (eap->nextcmd != NULL)
23980 *p = NUL;
23981
23982 if (!eap->skip)
23983 fp = find_func(name);
23984 vim_free(name);
23985
23986 if (!eap->skip)
23987 {
23988 if (fp == NULL)
23989 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023990 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 return;
23992 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 {
23995 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23996 return;
23997 }
23998
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024001 /* Delete the dict item that refers to the function, it will
24002 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024003 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024005 else
24006 func_free(fp);
24007 }
24008}
24009
24010/*
24011 * Free a function and remove it from the list of functions.
24012 */
24013 static void
24014func_free(fp)
24015 ufunc_T *fp;
24016{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024017 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024018
24019 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024020 ga_clear_strings(&(fp->uf_args));
24021 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024022#ifdef FEAT_PROFILE
24023 vim_free(fp->uf_tml_count);
24024 vim_free(fp->uf_tml_total);
24025 vim_free(fp->uf_tml_self);
24026#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024028 /* remove the function from the function hashtable */
24029 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24030 if (HASHITEM_EMPTY(hi))
24031 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024033 hash_remove(&func_hashtab, hi);
24034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024035 vim_free(fp);
24036}
24037
24038/*
24039 * Unreference a Function: decrement the reference count and free it when it
24040 * becomes zero. Only for numbered functions.
24041 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024042 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024043func_unref(name)
24044 char_u *name;
24045{
24046 ufunc_T *fp;
24047
24048 if (name != NULL && isdigit(*name))
24049 {
24050 fp = find_func(name);
24051 if (fp == NULL)
24052 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024053 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024054 {
24055 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 * when "uf_calls" becomes zero. */
24057 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 func_free(fp);
24059 }
24060 }
24061}
24062
24063/*
24064 * Count a reference to a Function.
24065 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024066 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024067func_ref(name)
24068 char_u *name;
24069{
24070 ufunc_T *fp;
24071
24072 if (name != NULL && isdigit(*name))
24073 {
24074 fp = find_func(name);
24075 if (fp == NULL)
24076 EMSG2(_(e_intern2), "func_ref()");
24077 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024078 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 }
24080}
24081
24082/*
24083 * Call a user function.
24084 */
24085 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024086call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 ufunc_T *fp; /* pointer to function */
24088 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 typval_T *argvars; /* arguments */
24090 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 linenr_T firstline; /* first line of range */
24092 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024093 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094{
Bram Moolenaar33570922005-01-25 22:26:29 +000024095 char_u *save_sourcing_name;
24096 linenr_T save_sourcing_lnum;
24097 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024098 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024099 int save_did_emsg;
24100 static int depth = 0;
24101 dictitem_T *v;
24102 int fixvar_idx = 0; /* index in fixvar[] */
24103 int i;
24104 int ai;
24105 char_u numbuf[NUMBUFLEN];
24106 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024107 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024108#ifdef FEAT_PROFILE
24109 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024110 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112
24113 /* If depth of calling is getting too high, don't execute the function */
24114 if (depth >= p_mfd)
24115 {
24116 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024117 rettv->v_type = VAR_NUMBER;
24118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 return;
24120 }
24121 ++depth;
24122
24123 line_breakcheck(); /* check for CTRL-C hit */
24124
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024125 fc = (funccall_T *)alloc(sizeof(funccall_T));
24126 fc->caller = current_funccal;
24127 current_funccal = fc;
24128 fc->func = fp;
24129 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024130 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024131 fc->linenr = 0;
24132 fc->returned = FALSE;
24133 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024135 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24136 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137
Bram Moolenaar33570922005-01-25 22:26:29 +000024138 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024139 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024140 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24141 * each argument variable and saves a lot of time.
24142 */
24143 /*
24144 * Init l: variables.
24145 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024146 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024147 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024148 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024149 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24150 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024151 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024152 name = v->di_key;
24153 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024154 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024155 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024156 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024157 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024158 v->di_tv.vval.v_dict = selfdict;
24159 ++selfdict->dv_refcount;
24160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024161
Bram Moolenaar33570922005-01-25 22:26:29 +000024162 /*
24163 * Init a: variables.
24164 * Set a:0 to "argcount".
24165 * Set a:000 to a list with room for the "..." arguments.
24166 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024167 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024168 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024169 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024170 /* Use "name" to avoid a warning from some compiler that checks the
24171 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024172 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024173 name = v->di_key;
24174 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024175 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024176 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024177 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024178 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024179 v->di_tv.vval.v_list = &fc->l_varlist;
24180 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24181 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24182 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024183
24184 /*
24185 * Set a:firstline to "firstline" and a:lastline to "lastline".
24186 * Set a:name to named arguments.
24187 * Set a:N to the "..." arguments.
24188 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024189 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024190 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024191 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024192 (varnumber_T)lastline);
24193 for (i = 0; i < argcount; ++i)
24194 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024195 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024196 if (ai < 0)
24197 /* named argument a:name */
24198 name = FUNCARG(fp, i);
24199 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024201 /* "..." argument a:1, a:2, etc. */
24202 sprintf((char *)numbuf, "%d", ai + 1);
24203 name = numbuf;
24204 }
24205 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24206 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024207 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024208 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24209 }
24210 else
24211 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024212 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24213 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024214 if (v == NULL)
24215 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024216 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024217 }
24218 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024219 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024220
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024221 /* Note: the values are copied directly to avoid alloc/free.
24222 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024224 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024225
24226 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24227 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024228 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24229 fc->l_listitems[ai].li_tv = argvars[i];
24230 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024231 }
24232 }
24233
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 /* Don't redraw while executing the function. */
24235 ++RedrawingDisabled;
24236 save_sourcing_name = sourcing_name;
24237 save_sourcing_lnum = sourcing_lnum;
24238 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024239 /* need space for function name + ("function " + 3) or "[number]" */
24240 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24241 + STRLEN(fp->uf_name) + 20;
24242 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 if (sourcing_name != NULL)
24244 {
24245 if (save_sourcing_name != NULL
24246 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024247 sprintf((char *)sourcing_name, "%s[%d]..",
24248 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 else
24250 STRCPY(sourcing_name, "function ");
24251 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24252
24253 if (p_verbose >= 12)
24254 {
24255 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024256 verbose_enter_scroll();
24257
Bram Moolenaar555b2802005-05-19 21:08:39 +000024258 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 if (p_verbose >= 14)
24260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024262 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024263 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024264 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265
24266 msg_puts((char_u *)"(");
24267 for (i = 0; i < argcount; ++i)
24268 {
24269 if (i > 0)
24270 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024271 if (argvars[i].v_type == VAR_NUMBER)
24272 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 else
24274 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024275 /* Do not want errors such as E724 here. */
24276 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024277 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024278 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024279 if (s != NULL)
24280 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024281 if (vim_strsize(s) > MSG_BUF_CLEN)
24282 {
24283 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24284 s = buf;
24285 }
24286 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024287 vim_free(tofree);
24288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 }
24290 }
24291 msg_puts((char_u *)")");
24292 }
24293 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024294
24295 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 --no_wait_return;
24297 }
24298 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024300 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024301 {
24302 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24303 func_do_profile(fp);
24304 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024305 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024306 {
24307 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024308 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024309 profile_zero(&fp->uf_tm_children);
24310 }
24311 script_prof_save(&wait_start);
24312 }
24313#endif
24314
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 save_did_emsg = did_emsg;
24318 did_emsg = FALSE;
24319
24320 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024321 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24323
24324 --RedrawingDisabled;
24325
24326 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024329 clear_tv(rettv);
24330 rettv->v_type = VAR_NUMBER;
24331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 }
24333
Bram Moolenaar05159a02005-02-26 23:04:13 +000024334#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024335 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024336 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024338 profile_end(&call_start);
24339 profile_sub_wait(&wait_start, &call_start);
24340 profile_add(&fp->uf_tm_total, &call_start);
24341 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024342 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024343 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024344 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24345 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024346 }
24347 }
24348#endif
24349
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 /* when being verbose, mention the return value */
24351 if (p_verbose >= 12)
24352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024354 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024357 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024358 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024359 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024360 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024363 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024364 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024365 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024366 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024367
Bram Moolenaar555b2802005-05-19 21:08:39 +000024368 /* The value may be very long. Skip the middle part, so that we
24369 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024370 * truncate it at the end. Don't want errors such as E724 here. */
24371 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024372 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024373 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024374 if (s != NULL)
24375 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024376 if (vim_strsize(s) > MSG_BUF_CLEN)
24377 {
24378 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24379 s = buf;
24380 }
24381 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024382 vim_free(tofree);
24383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 }
24385 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024386
24387 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 --no_wait_return;
24389 }
24390
24391 vim_free(sourcing_name);
24392 sourcing_name = save_sourcing_name;
24393 sourcing_lnum = save_sourcing_lnum;
24394 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024395#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024396 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024397 script_prof_restore(&wait_start);
24398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399
24400 if (p_verbose >= 12 && sourcing_name != NULL)
24401 {
24402 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024403 verbose_enter_scroll();
24404
Bram Moolenaar555b2802005-05-19 21:08:39 +000024405 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024407
24408 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 --no_wait_return;
24410 }
24411
24412 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024413 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024415
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024416 /* If the a:000 list and the l: and a: dicts are not referenced we can
24417 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024418 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24419 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24420 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24421 {
24422 free_funccal(fc, FALSE);
24423 }
24424 else
24425 {
24426 hashitem_T *hi;
24427 listitem_T *li;
24428 int todo;
24429
24430 /* "fc" is still in use. This can happen when returning "a:000" or
24431 * assigning "l:" to a global variable.
24432 * Link "fc" in the list for garbage collection later. */
24433 fc->caller = previous_funccal;
24434 previous_funccal = fc;
24435
24436 /* Make a copy of the a: variables, since we didn't do that above. */
24437 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24438 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24439 {
24440 if (!HASHITEM_EMPTY(hi))
24441 {
24442 --todo;
24443 v = HI2DI(hi);
24444 copy_tv(&v->di_tv, &v->di_tv);
24445 }
24446 }
24447
24448 /* Make a copy of the a:000 items, since we didn't do that above. */
24449 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24450 copy_tv(&li->li_tv, &li->li_tv);
24451 }
24452}
24453
24454/*
24455 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024456 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024457 */
24458 static int
24459can_free_funccal(fc, copyID)
24460 funccall_T *fc;
24461 int copyID;
24462{
24463 return (fc->l_varlist.lv_copyID != copyID
24464 && fc->l_vars.dv_copyID != copyID
24465 && fc->l_avars.dv_copyID != copyID);
24466}
24467
24468/*
24469 * Free "fc" and what it contains.
24470 */
24471 static void
24472free_funccal(fc, free_val)
24473 funccall_T *fc;
24474 int free_val; /* a: vars were allocated */
24475{
24476 listitem_T *li;
24477
24478 /* The a: variables typevals may not have been allocated, only free the
24479 * allocated variables. */
24480 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24481
24482 /* free all l: variables */
24483 vars_clear(&fc->l_vars.dv_hashtab);
24484
24485 /* Free the a:000 variables if they were allocated. */
24486 if (free_val)
24487 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24488 clear_tv(&li->li_tv);
24489
24490 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491}
24492
24493/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024494 * Add a number variable "name" to dict "dp" with value "nr".
24495 */
24496 static void
24497add_nr_var(dp, v, name, nr)
24498 dict_T *dp;
24499 dictitem_T *v;
24500 char *name;
24501 varnumber_T nr;
24502{
24503 STRCPY(v->di_key, name);
24504 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24505 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24506 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024507 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024508 v->di_tv.vval.v_number = nr;
24509}
24510
24511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 * ":return [expr]"
24513 */
24514 void
24515ex_return(eap)
24516 exarg_T *eap;
24517{
24518 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 int returning = FALSE;
24521
24522 if (current_funccal == NULL)
24523 {
24524 EMSG(_("E133: :return not inside a function"));
24525 return;
24526 }
24527
24528 if (eap->skip)
24529 ++emsg_skip;
24530
24531 eap->nextcmd = NULL;
24532 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024533 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 {
24535 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024536 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024538 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 }
24540 /* It's safer to return also on error. */
24541 else if (!eap->skip)
24542 {
24543 /*
24544 * Return unless the expression evaluation has been cancelled due to an
24545 * aborting error, an interrupt, or an exception.
24546 */
24547 if (!aborting())
24548 returning = do_return(eap, FALSE, TRUE, NULL);
24549 }
24550
24551 /* When skipping or the return gets pending, advance to the next command
24552 * in this line (!returning). Otherwise, ignore the rest of the line.
24553 * Following lines will be ignored by get_func_line(). */
24554 if (returning)
24555 eap->nextcmd = NULL;
24556 else if (eap->nextcmd == NULL) /* no argument */
24557 eap->nextcmd = check_nextcmd(arg);
24558
24559 if (eap->skip)
24560 --emsg_skip;
24561}
24562
24563/*
24564 * Return from a function. Possibly makes the return pending. Also called
24565 * for a pending return at the ":endtry" or after returning from an extra
24566 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024568 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 * FALSE when the return gets pending.
24570 */
24571 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024572do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 exarg_T *eap;
24574 int reanimate;
24575 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024576 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577{
24578 int idx;
24579 struct condstack *cstack = eap->cstack;
24580
24581 if (reanimate)
24582 /* Undo the return. */
24583 current_funccal->returned = FALSE;
24584
24585 /*
24586 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24587 * not in its finally clause (which then is to be executed next) is found.
24588 * In this case, make the ":return" pending for execution at the ":endtry".
24589 * Otherwise, return normally.
24590 */
24591 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24592 if (idx >= 0)
24593 {
24594 cstack->cs_pending[idx] = CSTP_RETURN;
24595
24596 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024597 /* A pending return again gets pending. "rettv" points to an
24598 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024600 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 else
24602 {
24603 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024604 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024606 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024608 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 {
24610 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024611 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024612 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 else
24614 EMSG(_(e_outofmem));
24615 }
24616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024617 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618
24619 if (reanimate)
24620 {
24621 /* The pending return value could be overwritten by a ":return"
24622 * without argument in a finally clause; reset the default
24623 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024624 current_funccal->rettv->v_type = VAR_NUMBER;
24625 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 }
24627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024628 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 }
24630 else
24631 {
24632 current_funccal->returned = TRUE;
24633
24634 /* If the return is carried out now, store the return value. For
24635 * a return immediately after reanimation, the value is already
24636 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024637 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024639 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024640 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024642 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 }
24644 }
24645
24646 return idx < 0;
24647}
24648
24649/*
24650 * Free the variable with a pending return value.
24651 */
24652 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024653discard_pending_return(rettv)
24654 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655{
Bram Moolenaar33570922005-01-25 22:26:29 +000024656 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657}
24658
24659/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024660 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 * is an allocated string. Used by report_pending() for verbose messages.
24662 */
24663 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024664get_return_cmd(rettv)
24665 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024667 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024668 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024669 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024671 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024672 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024673 if (s == NULL)
24674 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024675
24676 STRCPY(IObuff, ":return ");
24677 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24678 if (STRLEN(s) + 8 >= IOSIZE)
24679 STRCPY(IObuff + IOSIZE - 4, "...");
24680 vim_free(tofree);
24681 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682}
24683
24684/*
24685 * Get next function line.
24686 * Called by do_cmdline() to get the next line.
24687 * Returns allocated string, or NULL for end of function.
24688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 char_u *
24690get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024691 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024693 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694{
Bram Moolenaar33570922005-01-25 22:26:29 +000024695 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024696 ufunc_T *fp = fcp->func;
24697 char_u *retval;
24698 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699
24700 /* If breakpoints have been added/deleted need to check for it. */
24701 if (fcp->dbg_tick != debug_tick)
24702 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024703 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 sourcing_lnum);
24705 fcp->dbg_tick = debug_tick;
24706 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024708 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024709 func_line_end(cookie);
24710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024713 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24714 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 retval = NULL;
24716 else
24717 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024718 /* Skip NULL lines (continuation lines). */
24719 while (fcp->linenr < gap->ga_len
24720 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24721 ++fcp->linenr;
24722 if (fcp->linenr >= gap->ga_len)
24723 retval = NULL;
24724 else
24725 {
24726 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24727 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024728#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024729 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024730 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024731#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 }
24734
24735 /* Did we encounter a breakpoint? */
24736 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24737 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024738 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024740 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 sourcing_lnum);
24742 fcp->dbg_tick = debug_tick;
24743 }
24744
24745 return retval;
24746}
24747
Bram Moolenaar05159a02005-02-26 23:04:13 +000024748#if defined(FEAT_PROFILE) || defined(PROTO)
24749/*
24750 * Called when starting to read a function line.
24751 * "sourcing_lnum" must be correct!
24752 * When skipping lines it may not actually be executed, but we won't find out
24753 * until later and we need to store the time now.
24754 */
24755 void
24756func_line_start(cookie)
24757 void *cookie;
24758{
24759 funccall_T *fcp = (funccall_T *)cookie;
24760 ufunc_T *fp = fcp->func;
24761
24762 if (fp->uf_profiling && sourcing_lnum >= 1
24763 && sourcing_lnum <= fp->uf_lines.ga_len)
24764 {
24765 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024766 /* Skip continuation lines. */
24767 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24768 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024769 fp->uf_tml_execed = FALSE;
24770 profile_start(&fp->uf_tml_start);
24771 profile_zero(&fp->uf_tml_children);
24772 profile_get_wait(&fp->uf_tml_wait);
24773 }
24774}
24775
24776/*
24777 * Called when actually executing a function line.
24778 */
24779 void
24780func_line_exec(cookie)
24781 void *cookie;
24782{
24783 funccall_T *fcp = (funccall_T *)cookie;
24784 ufunc_T *fp = fcp->func;
24785
24786 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24787 fp->uf_tml_execed = TRUE;
24788}
24789
24790/*
24791 * Called when done with a function line.
24792 */
24793 void
24794func_line_end(cookie)
24795 void *cookie;
24796{
24797 funccall_T *fcp = (funccall_T *)cookie;
24798 ufunc_T *fp = fcp->func;
24799
24800 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24801 {
24802 if (fp->uf_tml_execed)
24803 {
24804 ++fp->uf_tml_count[fp->uf_tml_idx];
24805 profile_end(&fp->uf_tml_start);
24806 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024807 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024808 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24809 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024810 }
24811 fp->uf_tml_idx = -1;
24812 }
24813}
24814#endif
24815
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816/*
24817 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024818 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819 */
24820 int
24821func_has_ended(cookie)
24822 void *cookie;
24823{
Bram Moolenaar33570922005-01-25 22:26:29 +000024824 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825
24826 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24827 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024828 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024829 || fcp->returned);
24830}
24831
24832/*
24833 * return TRUE if cookie indicates a function which "abort"s on errors.
24834 */
24835 int
24836func_has_abort(cookie)
24837 void *cookie;
24838{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024839 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840}
24841
24842#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24843typedef enum
24844{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024845 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24846 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24847 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848} var_flavour_T;
24849
24850static var_flavour_T var_flavour __ARGS((char_u *varname));
24851
24852 static var_flavour_T
24853var_flavour(varname)
24854 char_u *varname;
24855{
24856 char_u *p = varname;
24857
24858 if (ASCII_ISUPPER(*p))
24859 {
24860 while (*(++p))
24861 if (ASCII_ISLOWER(*p))
24862 return VAR_FLAVOUR_SESSION;
24863 return VAR_FLAVOUR_VIMINFO;
24864 }
24865 else
24866 return VAR_FLAVOUR_DEFAULT;
24867}
24868#endif
24869
24870#if defined(FEAT_VIMINFO) || defined(PROTO)
24871/*
24872 * Restore global vars that start with a capital from the viminfo file
24873 */
24874 int
24875read_viminfo_varlist(virp, writing)
24876 vir_T *virp;
24877 int writing;
24878{
24879 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024880 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024881 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882
24883 if (!writing && (find_viminfo_parameter('!') != NULL))
24884 {
24885 tab = vim_strchr(virp->vir_line + 1, '\t');
24886 if (tab != NULL)
24887 {
24888 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024889 switch (*tab)
24890 {
24891 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024892#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024893 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024894#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024895 case 'D': type = VAR_DICT; break;
24896 case 'L': type = VAR_LIST; break;
24897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898
24899 tab = vim_strchr(tab, '\t');
24900 if (tab != NULL)
24901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024902 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024903 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024904 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024906#ifdef FEAT_FLOAT
24907 else if (type == VAR_FLOAT)
24908 (void)string2float(tab + 1, &tv.vval.v_float);
24909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024911 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024912 if (type == VAR_DICT || type == VAR_LIST)
24913 {
24914 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24915
24916 if (etv == NULL)
24917 /* Failed to parse back the dict or list, use it as a
24918 * string. */
24919 tv.v_type = VAR_STRING;
24920 else
24921 {
24922 vim_free(tv.vval.v_string);
24923 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024924 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024925 }
24926 }
24927
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024928 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024929
24930 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024931 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024932 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24933 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 }
24935 }
24936 }
24937
24938 return viminfo_readline(virp);
24939}
24940
24941/*
24942 * Write global vars that start with a capital to the viminfo file
24943 */
24944 void
24945write_viminfo_varlist(fp)
24946 FILE *fp;
24947{
Bram Moolenaar33570922005-01-25 22:26:29 +000024948 hashitem_T *hi;
24949 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024950 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024951 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024952 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024953 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024954 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955
24956 if (find_viminfo_parameter('!') == NULL)
24957 return;
24958
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024959 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024961 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024962 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024964 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024966 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024967 this_var = HI2DI(hi);
24968 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024970 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024971 {
24972 case VAR_STRING: s = "STR"; break;
24973 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024974#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024975 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024976#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024977 case VAR_DICT: s = "DIC"; break;
24978 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024979 default: continue;
24980 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024981 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024982 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024983 if (p != NULL)
24984 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024985 vim_free(tofree);
24986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 }
24988 }
24989}
24990#endif
24991
24992#if defined(FEAT_SESSION) || defined(PROTO)
24993 int
24994store_session_globals(fd)
24995 FILE *fd;
24996{
Bram Moolenaar33570922005-01-25 22:26:29 +000024997 hashitem_T *hi;
24998 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024999 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 char_u *p, *t;
25001
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025002 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025003 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025005 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025007 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025008 this_var = HI2DI(hi);
25009 if ((this_var->di_tv.v_type == VAR_NUMBER
25010 || this_var->di_tv.v_type == VAR_STRING)
25011 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025012 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025013 /* Escape special characters with a backslash. Turn a LF and
25014 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025015 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025016 (char_u *)"\\\"\n\r");
25017 if (p == NULL) /* out of memory */
25018 break;
25019 for (t = p; *t != NUL; ++t)
25020 if (*t == '\n')
25021 *t = 'n';
25022 else if (*t == '\r')
25023 *t = 'r';
25024 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025025 this_var->di_key,
25026 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25027 : ' ',
25028 p,
25029 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25030 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025031 || put_eol(fd) == FAIL)
25032 {
25033 vim_free(p);
25034 return FAIL;
25035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036 vim_free(p);
25037 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025038#ifdef FEAT_FLOAT
25039 else if (this_var->di_tv.v_type == VAR_FLOAT
25040 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25041 {
25042 float_T f = this_var->di_tv.vval.v_float;
25043 int sign = ' ';
25044
25045 if (f < 0)
25046 {
25047 f = -f;
25048 sign = '-';
25049 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025050 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025051 this_var->di_key, sign, f) < 0)
25052 || put_eol(fd) == FAIL)
25053 return FAIL;
25054 }
25055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 }
25057 }
25058 return OK;
25059}
25060#endif
25061
Bram Moolenaar661b1822005-07-28 22:36:45 +000025062/*
25063 * Display script name where an item was last set.
25064 * Should only be invoked when 'verbose' is non-zero.
25065 */
25066 void
25067last_set_msg(scriptID)
25068 scid_T scriptID;
25069{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025070 char_u *p;
25071
Bram Moolenaar661b1822005-07-28 22:36:45 +000025072 if (scriptID != 0)
25073 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025074 p = home_replace_save(NULL, get_scriptname(scriptID));
25075 if (p != NULL)
25076 {
25077 verbose_enter();
25078 MSG_PUTS(_("\n\tLast set from "));
25079 MSG_PUTS(p);
25080 vim_free(p);
25081 verbose_leave();
25082 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025083 }
25084}
25085
Bram Moolenaard812df62008-11-09 12:46:09 +000025086/*
25087 * List v:oldfiles in a nice way.
25088 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025089 void
25090ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025091 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025092{
25093 list_T *l = vimvars[VV_OLDFILES].vv_list;
25094 listitem_T *li;
25095 int nr = 0;
25096
25097 if (l == NULL)
25098 msg((char_u *)_("No old files"));
25099 else
25100 {
25101 msg_start();
25102 msg_scroll = TRUE;
25103 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25104 {
25105 msg_outnum((long)++nr);
25106 MSG_PUTS(": ");
25107 msg_outtrans(get_tv_string(&li->li_tv));
25108 msg_putchar('\n');
25109 out_flush(); /* output one line at a time */
25110 ui_breakcheck();
25111 }
25112 /* Assume "got_int" was set to truncate the listing. */
25113 got_int = FALSE;
25114
25115#ifdef FEAT_BROWSE_CMD
25116 if (cmdmod.browse)
25117 {
25118 quit_more = FALSE;
25119 nr = prompt_for_number(FALSE);
25120 msg_starthere();
25121 if (nr > 0)
25122 {
25123 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25124 (long)nr);
25125
25126 if (p != NULL)
25127 {
25128 p = expand_env_save(p);
25129 eap->arg = p;
25130 eap->cmdidx = CMD_edit;
25131 cmdmod.browse = FALSE;
25132 do_exedit(eap, NULL);
25133 vim_free(p);
25134 }
25135 }
25136 }
25137#endif
25138 }
25139}
25140
Bram Moolenaar53744302015-07-17 17:38:22 +020025141/* reset v:option_new, v:option_old and v:option_type */
25142 void
25143reset_v_option_vars()
25144{
25145 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25146 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25147 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25148}
25149
25150
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151#endif /* FEAT_EVAL */
25152
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025154#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155
25156#ifdef WIN3264
25157/*
25158 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25159 */
25160static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25161static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
25162static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
25163
25164/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025165 * Get the short path (8.3) for the filename in "fnamep".
25166 * Only works for a valid file name.
25167 * When the path gets longer "fnamep" is changed and the allocated buffer
25168 * is put in "bufp".
25169 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25170 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 */
25172 static int
25173get_short_pathname(fnamep, bufp, fnamelen)
25174 char_u **fnamep;
25175 char_u **bufp;
25176 int *fnamelen;
25177{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025178 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 char_u *newbuf;
25180
25181 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182 l = GetShortPathName(*fnamep, *fnamep, len);
25183 if (l > len - 1)
25184 {
25185 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025186 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 newbuf = vim_strnsave(*fnamep, l);
25188 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190
25191 vim_free(*bufp);
25192 *fnamep = *bufp = newbuf;
25193
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025194 /* Really should always succeed, as the buffer is big enough. */
25195 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 }
25197
25198 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025199 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200}
25201
25202/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025203 * Get the short path (8.3) for the filename in "fname". The converted
25204 * path is returned in "bufp".
25205 *
25206 * Some of the directories specified in "fname" may not exist. This function
25207 * will shorten the existing directories at the beginning of the path and then
25208 * append the remaining non-existing path.
25209 *
25210 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025211 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025212 * bufp - Pointer to an allocated buffer for the filename.
25213 * fnamelen - Length of the filename pointed to by fname
25214 *
25215 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216 */
25217 static int
25218shortpath_for_invalid_fname(fname, bufp, fnamelen)
25219 char_u **fname;
25220 char_u **bufp;
25221 int *fnamelen;
25222{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025223 char_u *short_fname, *save_fname, *pbuf_unused;
25224 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025226 int old_len, len;
25227 int new_len, sfx_len;
25228 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229
25230 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025231 old_len = *fnamelen;
25232 save_fname = vim_strnsave(*fname, old_len);
25233 pbuf_unused = NULL;
25234 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025236 endp = save_fname + old_len - 1; /* Find the end of the copy */
25237 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025239 /*
25240 * Try shortening the supplied path till it succeeds by removing one
25241 * directory at a time from the tail of the path.
25242 */
25243 len = 0;
25244 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025246 /* go back one path-separator */
25247 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25248 --endp;
25249 if (endp <= save_fname)
25250 break; /* processed the complete path */
25251
25252 /*
25253 * Replace the path separator with a NUL and try to shorten the
25254 * resulting path.
25255 */
25256 ch = *endp;
25257 *endp = 0;
25258 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025259 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025260 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25261 {
25262 retval = FAIL;
25263 goto theend;
25264 }
25265 *endp = ch; /* preserve the string */
25266
25267 if (len > 0)
25268 break; /* successfully shortened the path */
25269
25270 /* failed to shorten the path. Skip the path separator */
25271 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 }
25273
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025274 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025276 /*
25277 * Succeeded in shortening the path. Now concatenate the shortened
25278 * path with the remaining path at the tail.
25279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025281 /* Compute the length of the new path. */
25282 sfx_len = (int)(save_endp - endp) + 1;
25283 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025285 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025287 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025289 /* There is not enough space in the currently allocated string,
25290 * copy it to a buffer big enough. */
25291 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025293 {
25294 retval = FAIL;
25295 goto theend;
25296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 }
25298 else
25299 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025300 /* Transfer short_fname to the main buffer (it's big enough),
25301 * unless get_short_pathname() did its work in-place. */
25302 *fname = *bufp = save_fname;
25303 if (short_fname != save_fname)
25304 vim_strncpy(save_fname, short_fname, len);
25305 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025307
25308 /* concat the not-shortened part of the path */
25309 vim_strncpy(*fname + len, endp, sfx_len);
25310 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025312
25313theend:
25314 vim_free(pbuf_unused);
25315 vim_free(save_fname);
25316
25317 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318}
25319
25320/*
25321 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025322 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 */
25324 static int
25325shortpath_for_partial(fnamep, bufp, fnamelen)
25326 char_u **fnamep;
25327 char_u **bufp;
25328 int *fnamelen;
25329{
25330 int sepcount, len, tflen;
25331 char_u *p;
25332 char_u *pbuf, *tfname;
25333 int hasTilde;
25334
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025335 /* Count up the path separators from the RHS.. so we know which part
25336 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025338 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 if (vim_ispathsep(*p))
25340 ++sepcount;
25341
25342 /* Need full path first (use expand_env() to remove a "~/") */
25343 hasTilde = (**fnamep == '~');
25344 if (hasTilde)
25345 pbuf = tfname = expand_env_save(*fnamep);
25346 else
25347 pbuf = tfname = FullName_save(*fnamep, FALSE);
25348
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025349 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025351 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353
25354 if (len == 0)
25355 {
25356 /* Don't have a valid filename, so shorten the rest of the
25357 * path if we can. This CAN give us invalid 8.3 filenames, but
25358 * there's not a lot of point in guessing what it might be.
25359 */
25360 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025361 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 }
25364
25365 /* Count the paths backward to find the beginning of the desired string. */
25366 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025367 {
25368#ifdef FEAT_MBYTE
25369 if (has_mbyte)
25370 p -= mb_head_off(tfname, p);
25371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 if (vim_ispathsep(*p))
25373 {
25374 if (sepcount == 0 || (hasTilde && sepcount == 1))
25375 break;
25376 else
25377 sepcount --;
25378 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 if (hasTilde)
25381 {
25382 --p;
25383 if (p >= tfname)
25384 *p = '~';
25385 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025386 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 }
25388 else
25389 ++p;
25390
25391 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25392 vim_free(*bufp);
25393 *fnamelen = (int)STRLEN(p);
25394 *bufp = pbuf;
25395 *fnamep = p;
25396
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025397 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398}
25399#endif /* WIN3264 */
25400
25401/*
25402 * Adjust a filename, according to a string of modifiers.
25403 * *fnamep must be NUL terminated when called. When returning, the length is
25404 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025405 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 * When there is an error, *fnamep is set to NULL.
25407 */
25408 int
25409modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25410 char_u *src; /* string with modifiers */
25411 int *usedlen; /* characters after src that are used */
25412 char_u **fnamep; /* file name so far */
25413 char_u **bufp; /* buffer for allocated file name or NULL */
25414 int *fnamelen; /* length of fnamep */
25415{
25416 int valid = 0;
25417 char_u *tail;
25418 char_u *s, *p, *pbuf;
25419 char_u dirname[MAXPATHL];
25420 int c;
25421 int has_fullname = 0;
25422#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025423 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 int has_shortname = 0;
25425#endif
25426
25427repeat:
25428 /* ":p" - full path/file_name */
25429 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25430 {
25431 has_fullname = 1;
25432
25433 valid |= VALID_PATH;
25434 *usedlen += 2;
25435
25436 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25437 if ((*fnamep)[0] == '~'
25438#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25439 && ((*fnamep)[1] == '/'
25440# ifdef BACKSLASH_IN_FILENAME
25441 || (*fnamep)[1] == '\\'
25442# endif
25443 || (*fnamep)[1] == NUL)
25444
25445#endif
25446 )
25447 {
25448 *fnamep = expand_env_save(*fnamep);
25449 vim_free(*bufp); /* free any allocated file name */
25450 *bufp = *fnamep;
25451 if (*fnamep == NULL)
25452 return -1;
25453 }
25454
25455 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025456 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 {
25458 if (vim_ispathsep(*p)
25459 && p[1] == '.'
25460 && (p[2] == NUL
25461 || vim_ispathsep(p[2])
25462 || (p[2] == '.'
25463 && (p[3] == NUL || vim_ispathsep(p[3])))))
25464 break;
25465 }
25466
25467 /* FullName_save() is slow, don't use it when not needed. */
25468 if (*p != NUL || !vim_isAbsName(*fnamep))
25469 {
25470 *fnamep = FullName_save(*fnamep, *p != NUL);
25471 vim_free(*bufp); /* free any allocated file name */
25472 *bufp = *fnamep;
25473 if (*fnamep == NULL)
25474 return -1;
25475 }
25476
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025477#ifdef WIN3264
25478# if _WIN32_WINNT >= 0x0500
25479 if (vim_strchr(*fnamep, '~') != NULL)
25480 {
25481 /* Expand 8.3 filename to full path. Needed to make sure the same
25482 * file does not have two different names.
25483 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25484 p = alloc(_MAX_PATH + 1);
25485 if (p != NULL)
25486 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010025487 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025488 {
25489 vim_free(*bufp);
25490 *bufp = *fnamep = p;
25491 }
25492 else
25493 vim_free(p);
25494 }
25495 }
25496# endif
25497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 /* Append a path separator to a directory. */
25499 if (mch_isdir(*fnamep))
25500 {
25501 /* Make room for one or two extra characters. */
25502 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25503 vim_free(*bufp); /* free any allocated file name */
25504 *bufp = *fnamep;
25505 if (*fnamep == NULL)
25506 return -1;
25507 add_pathsep(*fnamep);
25508 }
25509 }
25510
25511 /* ":." - path relative to the current directory */
25512 /* ":~" - path relative to the home directory */
25513 /* ":8" - shortname path - postponed till after */
25514 while (src[*usedlen] == ':'
25515 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25516 {
25517 *usedlen += 2;
25518 if (c == '8')
25519 {
25520#ifdef WIN3264
25521 has_shortname = 1; /* Postpone this. */
25522#endif
25523 continue;
25524 }
25525 pbuf = NULL;
25526 /* Need full path first (use expand_env() to remove a "~/") */
25527 if (!has_fullname)
25528 {
25529 if (c == '.' && **fnamep == '~')
25530 p = pbuf = expand_env_save(*fnamep);
25531 else
25532 p = pbuf = FullName_save(*fnamep, FALSE);
25533 }
25534 else
25535 p = *fnamep;
25536
25537 has_fullname = 0;
25538
25539 if (p != NULL)
25540 {
25541 if (c == '.')
25542 {
25543 mch_dirname(dirname, MAXPATHL);
25544 s = shorten_fname(p, dirname);
25545 if (s != NULL)
25546 {
25547 *fnamep = s;
25548 if (pbuf != NULL)
25549 {
25550 vim_free(*bufp); /* free any allocated file name */
25551 *bufp = pbuf;
25552 pbuf = NULL;
25553 }
25554 }
25555 }
25556 else
25557 {
25558 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25559 /* Only replace it when it starts with '~' */
25560 if (*dirname == '~')
25561 {
25562 s = vim_strsave(dirname);
25563 if (s != NULL)
25564 {
25565 *fnamep = s;
25566 vim_free(*bufp);
25567 *bufp = s;
25568 }
25569 }
25570 }
25571 vim_free(pbuf);
25572 }
25573 }
25574
25575 tail = gettail(*fnamep);
25576 *fnamelen = (int)STRLEN(*fnamep);
25577
25578 /* ":h" - head, remove "/file_name", can be repeated */
25579 /* Don't remove the first "/" or "c:\" */
25580 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25581 {
25582 valid |= VALID_HEAD;
25583 *usedlen += 2;
25584 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025585 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025586 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 *fnamelen = (int)(tail - *fnamep);
25588#ifdef VMS
25589 if (*fnamelen > 0)
25590 *fnamelen += 1; /* the path separator is part of the path */
25591#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025592 if (*fnamelen == 0)
25593 {
25594 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25595 p = vim_strsave((char_u *)".");
25596 if (p == NULL)
25597 return -1;
25598 vim_free(*bufp);
25599 *bufp = *fnamep = tail = p;
25600 *fnamelen = 1;
25601 }
25602 else
25603 {
25604 while (tail > s && !after_pathsep(s, tail))
25605 mb_ptr_back(*fnamep, tail);
25606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 }
25608
25609 /* ":8" - shortname */
25610 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25611 {
25612 *usedlen += 2;
25613#ifdef WIN3264
25614 has_shortname = 1;
25615#endif
25616 }
25617
25618#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025619 /*
25620 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 */
25622 if (has_shortname)
25623 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025624 /* Copy the string if it is shortened by :h and when it wasn't copied
25625 * yet, because we are going to change it in place. Avoids changing
25626 * the buffer name for "%:8". */
25627 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628 {
25629 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025630 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631 return -1;
25632 vim_free(*bufp);
25633 *bufp = *fnamep = p;
25634 }
25635
25636 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025637 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 if (!has_fullname && !vim_isAbsName(*fnamep))
25639 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025640 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 return -1;
25642 }
25643 else
25644 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025645 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646
Bram Moolenaardc935552011-08-17 15:23:23 +020025647 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025649 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 return -1;
25651
25652 if (l == 0)
25653 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025654 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025656 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 return -1;
25658 }
25659 *fnamelen = l;
25660 }
25661 }
25662#endif /* WIN3264 */
25663
25664 /* ":t" - tail, just the basename */
25665 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25666 {
25667 *usedlen += 2;
25668 *fnamelen -= (int)(tail - *fnamep);
25669 *fnamep = tail;
25670 }
25671
25672 /* ":e" - extension, can be repeated */
25673 /* ":r" - root, without extension, can be repeated */
25674 while (src[*usedlen] == ':'
25675 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25676 {
25677 /* find a '.' in the tail:
25678 * - for second :e: before the current fname
25679 * - otherwise: The last '.'
25680 */
25681 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25682 s = *fnamep - 2;
25683 else
25684 s = *fnamep + *fnamelen - 1;
25685 for ( ; s > tail; --s)
25686 if (s[0] == '.')
25687 break;
25688 if (src[*usedlen + 1] == 'e') /* :e */
25689 {
25690 if (s > tail)
25691 {
25692 *fnamelen += (int)(*fnamep - (s + 1));
25693 *fnamep = s + 1;
25694#ifdef VMS
25695 /* cut version from the extension */
25696 s = *fnamep + *fnamelen - 1;
25697 for ( ; s > *fnamep; --s)
25698 if (s[0] == ';')
25699 break;
25700 if (s > *fnamep)
25701 *fnamelen = s - *fnamep;
25702#endif
25703 }
25704 else if (*fnamep <= tail)
25705 *fnamelen = 0;
25706 }
25707 else /* :r */
25708 {
25709 if (s > tail) /* remove one extension */
25710 *fnamelen = (int)(s - *fnamep);
25711 }
25712 *usedlen += 2;
25713 }
25714
25715 /* ":s?pat?foo?" - substitute */
25716 /* ":gs?pat?foo?" - global substitute */
25717 if (src[*usedlen] == ':'
25718 && (src[*usedlen + 1] == 's'
25719 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25720 {
25721 char_u *str;
25722 char_u *pat;
25723 char_u *sub;
25724 int sep;
25725 char_u *flags;
25726 int didit = FALSE;
25727
25728 flags = (char_u *)"";
25729 s = src + *usedlen + 2;
25730 if (src[*usedlen + 1] == 'g')
25731 {
25732 flags = (char_u *)"g";
25733 ++s;
25734 }
25735
25736 sep = *s++;
25737 if (sep)
25738 {
25739 /* find end of pattern */
25740 p = vim_strchr(s, sep);
25741 if (p != NULL)
25742 {
25743 pat = vim_strnsave(s, (int)(p - s));
25744 if (pat != NULL)
25745 {
25746 s = p + 1;
25747 /* find end of substitution */
25748 p = vim_strchr(s, sep);
25749 if (p != NULL)
25750 {
25751 sub = vim_strnsave(s, (int)(p - s));
25752 str = vim_strnsave(*fnamep, *fnamelen);
25753 if (sub != NULL && str != NULL)
25754 {
25755 *usedlen = (int)(p + 1 - src);
25756 s = do_string_sub(str, pat, sub, flags);
25757 if (s != NULL)
25758 {
25759 *fnamep = s;
25760 *fnamelen = (int)STRLEN(s);
25761 vim_free(*bufp);
25762 *bufp = s;
25763 didit = TRUE;
25764 }
25765 }
25766 vim_free(sub);
25767 vim_free(str);
25768 }
25769 vim_free(pat);
25770 }
25771 }
25772 /* after using ":s", repeat all the modifiers */
25773 if (didit)
25774 goto repeat;
25775 }
25776 }
25777
Bram Moolenaar26df0922014-02-23 23:39:13 +010025778 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25779 {
25780 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25781 if (p == NULL)
25782 return -1;
25783 vim_free(*bufp);
25784 *bufp = *fnamep = p;
25785 *fnamelen = (int)STRLEN(p);
25786 *usedlen += 2;
25787 }
25788
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 return valid;
25790}
25791
25792/*
25793 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25794 * "flags" can be "g" to do a global substitute.
25795 * Returns an allocated string, NULL for error.
25796 */
25797 char_u *
25798do_string_sub(str, pat, sub, flags)
25799 char_u *str;
25800 char_u *pat;
25801 char_u *sub;
25802 char_u *flags;
25803{
25804 int sublen;
25805 regmatch_T regmatch;
25806 int i;
25807 int do_all;
25808 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025809 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 garray_T ga;
25811 char_u *ret;
25812 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025813 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814
25815 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25816 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025817 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
25819 ga_init2(&ga, 1, 200);
25820
25821 do_all = (flags[0] == 'g');
25822
25823 regmatch.rm_ic = p_ic;
25824 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25825 if (regmatch.regprog != NULL)
25826 {
25827 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025828 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25830 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025831 /* Skip empty match except for first match. */
25832 if (regmatch.startp[0] == regmatch.endp[0])
25833 {
25834 if (zero_width == regmatch.startp[0])
25835 {
25836 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025837 i = MB_PTR2LEN(tail);
25838 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25839 (size_t)i);
25840 ga.ga_len += i;
25841 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025842 continue;
25843 }
25844 zero_width = regmatch.startp[0];
25845 }
25846
Bram Moolenaar071d4272004-06-13 20:20:40 +000025847 /*
25848 * Get some space for a temporary buffer to do the substitution
25849 * into. It will contain:
25850 * - The text up to where the match is.
25851 * - The substituted text.
25852 * - The text after the match.
25853 */
25854 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025855 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25857 {
25858 ga_clear(&ga);
25859 break;
25860 }
25861
25862 /* copy the text up to where the match is */
25863 i = (int)(regmatch.startp[0] - tail);
25864 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25865 /* add the substituted text */
25866 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25867 + ga.ga_len + i, TRUE, TRUE, FALSE);
25868 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025869 tail = regmatch.endp[0];
25870 if (*tail == NUL)
25871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 if (!do_all)
25873 break;
25874 }
25875
25876 if (ga.ga_data != NULL)
25877 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25878
Bram Moolenaar473de612013-06-08 18:19:48 +020025879 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 }
25881
25882 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25883 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025884 if (p_cpo == empty_option)
25885 p_cpo = save_cpo;
25886 else
25887 /* Darn, evaluating {sub} expression changed the value. */
25888 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889
25890 return ret;
25891}
25892
25893#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */